Browse Source

param.BigRational: add toApproximateString() method

Returns a String with a floating point representation of the rational number.
If the conversion is imprecise, prefixed by ~.
master
Joachim Klein 8 years ago
committed by Dave Parker
parent
commit
b4221e0a38
  1. 16
      prism/src/param/BigRational.java

16
prism/src/param/BigRational.java

@ -824,6 +824,22 @@ public final class BigRational implements Comparable<BigRational>
throw new PrismLangException("Conversion from BigRational to Boolean not possible, invalid value: " + this); 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, * Returns the int representation of this value,
* if this value is an integer and if the integer can * if this value is an integer and if the integer can

Loading…
Cancel
Save