Rational
Date: 2020-06-16

org.jpl7

## Class Rational

- java.lang.Object
- - org.jpl7.Term
  - - org.jpl7.Rational

-     public class Rational
      extends Term

  Rational is a specialised Term representing a Prolog rational value.

       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) 2020 Sebastian Sardina

  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:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  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.

  See Also:
    `Term`, `Compound`

- - ### Field Summary

    Fields
    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 Summary

    Constructors
    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

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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"

    - ### Methods inherited from class org.jpl7.Term

      `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`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### numerator

          protected final 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"

    - #### denominator

          protected final long denominator

  - ### Constructor Detail

    - #### Rational

          public Rational(long numerator,
                          long denominator)

      Creates a rational in canonical form
      Parameters:
        `numerator` - a long integer
        `denominator` - a long integer

    - #### Rational

          public Rational(java.lang.String rat)

      Creates a rational in canonical form
      Parameters:
        `rat` - The rational number in format NrM

  - ### Method Detail

    - #### args

          public Term[] args()

      Description copied from class: `Term`
      The arguments of this 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

      Overrides:
        `args` in class `Term`
      Returns:
        the arguments of a Compound

    - #### equals

          public boolean equals(java.lang.Object o)

      Two Rationals instances are equal if their numerator and denominator match (remember Rationals are stored in canonical form)
      Overrides:
        `equals` in class `java.lang.Object`
      Parameters:
        `o` - The Object to compare (not necessarily an Rational)
      Returns:
        true if the Object satisfies the above condition

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `java.lang.Object`

    - #### getNumerator

          public final long getNumerator()

    - #### getDenominator

          public final long getDenominator()

    - #### hasFunctor

          public final boolean hasFunctor(long val,
                                          int arity)

      whether this Rational's functor has (long) 'name' and 'arity' (c.f. traditional functor/3)
      Overrides:
        `hasFunctor` in class `Term`
      Parameters:
        `val` - a possible name for the functor of a term
        `arity` - an arity 0+
      Returns:
        whether this Rational's functor has (long) 'name' and 'arity'

    - #### intValue

          public final int intValue()

      Returns the value of this Rational as an int if possible, else throws a JPLException
      Overrides:
        `intValue` in class `Term`
      Returns:
        the int value of this Rational
      Throws:
        `JPLException` - if the value of this Rational is too great to be represented as a Java int

    - #### longValue

          public final long longValue()

      Returns the value of this org.jpl7.Rational as a long
      Overrides:
        `longValue` in class `Term`
      Returns:
        the value of this org.jpl7.Rational as a long

    - #### floatValue

          public final float floatValue()

      Returns the value of this Rational converted to a float
      Overrides:
        `floatValue` in class `Term`
      Returns:
        the value of this Rational converted to a float

    - #### doubleValue

          public final double doubleValue()

      Returns the value of this Rational converted to a double
      Overrides:
        `doubleValue` in class `Term`
      Returns:
        the value of this Rational converted to a double

    - #### put

          protected final 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.
      Specified by:
        `put` in class `Term`
      Parameters:
        `varnames_to_vars` - A Map from variable names to Prolog variables.
        `term` - A (previously created) term_t which is to be set to a Prolog integer

    - #### toString

          public java.lang.String toString()

      a Prolog source text representation of this Rational's value
      Overrides:
        `toString` in class `java.lang.Object`
      Returns:
        a Prolog source text representation of this Rational's value

    - #### type

          public final int type()

      the type of this term, as "Prolog.RATIONAL"
      Specified by:
        `type` in class `Term`
      Returns:
        the type of this term, as "Prolog.JPLException"

    - #### typeName

          public java.lang.String typeName()

      the name of the type of this term, as "Rational"
      Specified by:
        `typeName` in class `Term`
      Returns:
        the name of the type of this term, as "Rational"