From 70e9e56a7311eb9a2e8f8eac2d6893ddeb7ef5a7 Mon Sep 17 00:00:00 2001 From: Joachim Klein Date: Fri, 22 Jul 2016 10:35:19 +0000 Subject: [PATCH] BigRational: add toInt() and toBoolean() methods git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@11555 bbc10eb1-c90d-0410-af57-cb519fbb1720 --- prism/src/param/BigRational.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/prism/src/param/BigRational.java b/prism/src/param/BigRational.java index 8e484e20..cf1fb612 100644 --- a/prism/src/param/BigRational.java +++ b/prism/src/param/BigRational.java @@ -797,4 +797,35 @@ public final class BigRational implements Comparable { return isNaN() || isInf() || isMInf(); } + + /** + * Returns true if this value equals 1 or false if this value + * equals 0. In all other cases, a PrismLangException is thrown. + */ + public boolean toBoolean() throws PrismLangException + { + if (isOne()) + return true; + if (isZero()) + return false; + throw new PrismLangException("Conversion from BigRational to Boolean not possible, invalid value: " + this); + } + + /** + * Returns the int representation of this value, + * if this value is an integer and if the integer can + * be represented by an int variable. + * In all other cases, a PrismLangException is thrown. + */ + public int toInt() throws PrismLangException + { + if (!isInteger()) { + throw new PrismLangException("Can not convert fractional number to int"); + } + int value = getNum().intValue(); + if (!getNum().equals(new BigInteger(Integer.toString(value)))) { + throw new PrismLangException("Can not convert BigInteger to int, value out of range"); + } + return value; + } }