Browse Source

BigRational: add from(other) static constructor

Currently, supports int, long, double, boolean and other BigRational


git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@11552 bbc10eb1-c90d-0410-af57-cb519fbb1720
master
Joachim Klein 10 years ago
parent
commit
48299632a1
  1. 25
      prism/src/param/BigRational.java

25
prism/src/param/BigRational.java

@ -236,6 +236,31 @@ public final class BigRational implements Comparable<BigRational>
} }
} }
/**
* Construct a BigRational from the given object.
* Throws an IllegalArgumentException if there is no
* known conversion.
*/
public static BigRational from(Object value)
{
if (value instanceof BigRational) {
BigRational v = (BigRational)value;
return new BigRational(v.num, v.den);
} else if (value instanceof Integer) {
return new BigRational((int) value);
} else if (value instanceof Long) {
return new BigRational((long) value);
} else if (value instanceof Boolean) {
boolean v = (Boolean)value;
return new BigRational(v ? 1 : 0);
} else if (value instanceof Double) {
// TODO: ? might be imprecise, perhaps there
// is a way to get the full precision?
return new BigRational(((Double)value).toString());
}
throw new IllegalArgumentException("Can not convert from " + value.getClass() + " to BigRational");
}
// helper functions // helper functions
/** /**

Loading…
Cancel
Save