From b4221e0a388d8921287eb388fe80221fe5344c10 Mon Sep 17 00:00:00 2001 From: Joachim Klein Date: Mon, 28 Aug 2017 12:45:47 +0200 Subject: [PATCH] param.BigRational: add toApproximateString() method Returns a String with a floating point representation of the rational number. If the conversion is imprecise, prefixed by ~. --- prism/src/param/BigRational.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/prism/src/param/BigRational.java b/prism/src/param/BigRational.java index 459d32d1..daa0e58f 100644 --- a/prism/src/param/BigRational.java +++ b/prism/src/param/BigRational.java @@ -824,6 +824,22 @@ public final class BigRational implements Comparable throw new PrismLangException("Conversion from BigRational to Boolean not possible, invalid value: " + this); } + /** + * Return an approximate String representation (via conversion to double). + * If the conversion is imprecise, the result string is prefixed by '~'. + */ + public String toApproximateString() + { + String result = Double.toString(doubleValue()); + if (new BigRational(result).equals(this)) { + // round-trip did not lose precision + return result; + } + // only approximate + return "~" + result; + + } + /** * Returns the int representation of this value, * if this value is an integer and if the integer can