public class Rational extends Term
Rational r1 = new Rational(2,3); Rational r2 = new Rational(2r3);A rational will be stored in its canonical form with gcd(numerator, denominator = 1 (not reducible) and denominator greater than 1. If the denominator reduces to 1, a JPLException is thrown. Once constructed, the value of an Rational instance cannot be altered. An Rational can be used (and re-used) as an argument of Compounds.
Copyright (C) 2004 Paul Singleton
Copyright (C) 1998 Fred Dushin
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Modifier and Type | Field and Description |
---|---|
protected long |
denominator |
protected long |
numerator
the numerator and denominator in canonical form:
gcd(numerator, denominator = 1 (not reducible)
denominator greater than 1
rat is a String of the form "NrM"
|
Constructor and Description |
---|
Rational(long numerator,
long denominator)
Creates a rational in canonical form
|
Rational(java.lang.String rat)
Creates a rational in canonical form
|
Modifier and Type | Method and Description |
---|---|
Term[] |
args()
The arguments of this Term.
|
double |
doubleValue()
Returns the value of this Rational converted to a double
|
boolean |
equals(java.lang.Object o)
Two Rationals instances are equal if their numerator and denominator match
(remember Rationals are stored in canonical form)
|
float |
floatValue()
Returns the value of this Rational converted to a float
|
long |
getDenominator() |
long |
getNumerator() |
boolean |
hasFunctor(long val,
int arity)
whether this Rational's functor has (long) 'name' and 'arity' (c.f.
|
int |
hashCode() |
int |
intValue()
Returns the value of this Rational as an int if possible, else throws a JPLException
|
long |
longValue()
Returns the value of this org.jpl7.Rational as a long
|
protected void |
put(java.util.Map<java.lang.String,term_t> varnames_to_vars,
term_t term)
To convert an Rational into a Prolog term, we put its value into the term_t.
|
java.lang.String |
toString()
a Prolog source text representation of this Rational's value
|
int |
type()
the type of this term, as "Prolog.RATIONAL"
|
java.lang.String |
typeName()
the name of the type of this term, as "Rational"
|
arg, arity, atomListToStringArray, atomType, bigValue, getSubst, getSubsts, getTerm, getTerm, hasFunctor, hasFunctor, hasFunctor, intArrayArrayToList, intArrayToList, isAtom, isAtomOfNameType, isBig, isBigInteger, isCompound, isFloat, isInteger, isJFalse, isJNull, isJRef, isJTrue, isJVoid, isList, isList, isListNil, isListPair, isVariable, jrefToObject, listLength, listLength, listToTermArray, listToTermArray, name, object, objectToJRef, put, putParams, putParams, putParams1, putParams2, putTerm, putTerms, ref, setName, stringArrayToList, termArrayToList, textParamsToTerm, textToTerm, toString, toTermArray
protected final long numerator
protected final long denominator
public Rational(long numerator, long denominator)
numerator
- a long integerdenominator
- a long integerpublic Rational(java.lang.String rat)
rat
- The rational number in format NrMpublic Term[] args()
Term
Note that a SWI Prolog 7.x compound term can have zero arguments.
This method returns an empty Term[] from an Atom, Float, Integer or JRef, approximating the behaviour of SWI Prolog's =../2
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- The Object to compare (not necessarily an Rational)public int hashCode()
hashCode
in class java.lang.Object
public final long getNumerator()
public final long getDenominator()
public final boolean hasFunctor(long val, int arity)
hasFunctor
in class Term
val
- a possible name for the functor of a termarity
- an arity 0+public final int intValue()
intValue
in class Term
JPLException
- if the value of this Rational is too great to be represented as a Java intpublic final long longValue()
public final float floatValue()
floatValue
in class Term
public final double doubleValue()
doubleValue
in class Term
protected final void put(java.util.Map<java.lang.String,term_t> varnames_to_vars, term_t term)
public java.lang.String toString()
toString
in class java.lang.Object
public final int type()