Class BigRational
java.lang.Object
solvers.dataStructures.BigRational
- All Implemented Interfaces:
Comparable<BigRational>
This class is a representation of rational numbers. Essentially, it uses two
BigIntegers to represent any rational number. It also contains the necessary
methods for the basic arithmetic operations.
- Author:
- Michail Makaronidis, 2010
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BigRationalThis is a variable equal to minus one.static final BigRationalThis is a variable equal to one.static final BigRationalThis is a variable equal to zero. -
Constructor Summary
ConstructorsConstructorDescriptionThe constructor creates and initialises a new BigRational object with the default value of zero.BigRational(double d) The constructor creates and initialises a new BigRational object and sets it equal to a double.BigRational(int numerator) The constructor creates and initialises a new BigRational object where the rational is an integer (no denominator is needed).BigRational(int numerator, int denominator) The constructor creates and initialises a new BigRational objectThe constructor creates and initialises a new BigRational object from a string, e.g., "-343/1273".The constructor creates and initialises a new BigRational object and sets it equal to a BigDecimal.BigRational(BigDecimal numerator, BigDecimal denominator) The constructor creates and initialises a new BigRational objectThe constructor creates and initialises a new BigRational object and sets it equal to a BigInteger.BigRational(BigInteger numerator, BigInteger denominator) The constructor creates and initialises a new BigRational object -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns a BigRational object equal to the absolute value of this BigRational.add(BigRational b) Adds this number (a) to another (b)doubleApproximates the current rational number as a double.Returns the number as a BigDecimal.intCompares the BigRational with another.copy()Returns a copy of the current BigRational.Divides this number (a) by another (b)booleanDecides if this BigRational is equal to another.booleanChecks whether this number is greater than or equal to another.booleanChecks whether this number is greater than another.inthashCode()Returns a hashCode consistent with equals() and compareTo().booleanReturns whether the number can be expressed as a BigDecimal.booleanDecides whether the BigRational represents an integer number.booleanDecides if this BigRational is equal to minus one.booleanDecides if this BigRational is negative.booleanisOne()Decides if this BigRational is equal to one.booleanDecides if this BigRational is positive.booleanChecks whether the current BigRational is flagged as indeterminable.booleanisZero()Decides if this BigRational is equal to zero.doublelog()Returns the logarithm of this BigRational numbervoidFlags the current BigRational as determinable (default).voidFlags the current BigRational as indeterminable.mod(BigInteger m) Returns (this mod m), allowing for negative numbers or zeros as results.Returns (this mod m), allowing for positive numbers or zeros as results.Multiplies this number (a) with another (b)Multiplies this number (a) with another (b)negate()Negates this number (a)pow(int n) Computes this number raised to the power of anotherReturn the reciprocal (1/a) of this number (a)booleanChecks whether this number is smaller than or equal to another.booleanChecks whether this number is smaller than another.Subtracts a number (b) from this one (a)toString()Return string representation of the BigRational
-
Field Details
-
ZERO
This is a variable equal to zero. -
ONE
This is a variable equal to one. -
MINUS_ONE
This is a variable equal to minus one.
-
-
Constructor Details
-
BigRational
public BigRational(int numerator, int denominator) The constructor creates and initialises a new BigRational object- Parameters:
numerator- The numerator of the rational numberdenominator- The denominator of the rational number
-
BigRational
public BigRational(int numerator) The constructor creates and initialises a new BigRational object where the rational is an integer (no denominator is needed).- Parameters:
numerator- The number
-
BigRational
The constructor creates and initialises a new BigRational object from a string, e.g., "-343/1273".- Parameters:
s- The string representing the number
-
BigRational
The constructor creates and initialises a new BigRational object- Parameters:
numerator- The numerator of the rational numberdenominator- The denominator of the rational number
-
BigRational
The constructor creates and initialises a new BigRational object- Parameters:
numerator- The numerator of the rational numberdenominator- The denominator of the rational number
-
BigRational
The constructor creates and initialises a new BigRational object and sets it equal to a BigDecimal. No denominator is needed as input, it is added automatically.- Parameters:
a- The number
-
BigRational
The constructor creates and initialises a new BigRational object and sets it equal to a BigInteger. No denominator is needed as input, it is added automatically.- Parameters:
a- The number
-
BigRational
public BigRational(double d) The constructor creates and initialises a new BigRational object and sets it equal to a double. No denominator is needed as input, it is added automatically.- Parameters:
d- The number
-
BigRational
public BigRational()The constructor creates and initialises a new BigRational object with the default value of zero.
-
-
Method Details
-
copy
Returns a copy of the current BigRational. Amended by Jack, to copy undefined as well- Returns:
- The BigRational object copy
-
toString
-
compareTo
Compares the BigRational with another.- Specified by:
compareToin interfaceComparable<BigRational>- Parameters:
b- The other number- Returns:
- Returns { -1, 0, + 1 } if a < b, a = b, or a > b
-
greaterThan
Checks whether this number is greater than another.- Parameters:
b- The other number- Returns:
- True if this number is greater than the other, else false.
-
smallerThan
Checks whether this number is smaller than another.- Parameters:
b- The other number- Returns:
- True if this number is smaller than the other, else false.
-
greaterOrEqualThan
Checks whether this number is greater than or equal to another.- Parameters:
b- The other number- Returns:
- True if this number is greater than or equal to the other, else false.
-
smallerOrEqualThan
Checks whether this number is smaller than or equal to another.- Parameters:
b- The other number- Returns:
- True if this number is smaller than or equal to the other, else false.
-
isZero
public boolean isZero()Decides if this BigRational is equal to zero.- Returns:
- True if equal to zero, false otherwise
-
isOne
public boolean isOne()Decides if this BigRational is equal to one.- Returns:
- True if equal to one, false otherwise
-
isMinusOne
public boolean isMinusOne()Decides if this BigRational is equal to minus one.- Returns:
- True if equal to minus one, false otherwise
-
isPositive
public boolean isPositive()Decides if this BigRational is positive.- Returns:
- True if positive, false otherwise
-
isNegative
public boolean isNegative()Decides if this BigRational is negative.- Returns:
- True if negative, false otherwise
-
abs
Returns a BigRational object equal to the absolute value of this BigRational.- Returns:
- The absolute value BigRational
-
equals
-
hashCode
-
multiply
Multiplies this number (a) with another (b)- Parameters:
b- The number b- Returns:
- The number a*b
-
multiply
Multiplies this number (a) with another (b)- Parameters:
b- The number b- Returns:
- The number a*b
-
isInteger
public boolean isInteger()Decides whether the BigRational represents an integer number.- Returns:
- True if it represents an integer, no otherwise
-
add
Adds this number (a) to another (b)- Parameters:
b- The number b- Returns:
- The number a+b
-
negate
-
subtract
Subtracts a number (b) from this one (a)- Parameters:
b- The number b- Returns:
- The number a-b
-
reciprocal
Return the reciprocal (1/a) of this number (a)- Returns:
- The number 1/a
-
divide
Divides this number (a) by another (b)- Parameters:
b- The number b- Returns:
- The number a/b
-
pow
Computes this number raised to the power of another- Parameters:
n- The exponent- Returns:
- The result of the exponentiation
-
asBigDecimal
Returns the number as a BigDecimal. This can only work if the division of the numerator by the denominator does not have an infinite decimal expansion. This can be ensured by checking isBigDecimal() first.- Returns:
- The division result
-
isBigDecimal
public boolean isBigDecimal()Returns whether the number can be expressed as a BigDecimal. This can only happen if the division of the numerator by the denominator does not have an infinite decimal expansion.- Returns:
- True if it can be expressed as a BigDecimal, false otherwise.
-
approximateAsDouble
public double approximateAsDouble()Approximates the current rational number as a double. Leads to loss of precision both in decimal digits and (possibly) in magnitude (if this > MAX_DOUBLE), therefore it is used only for printing purposes and not for calculations.- Returns:
- The double approximating this rational number.
-
makeUndefined
public void makeUndefined()Flags the current BigRational as indeterminable. -
makeDefined
public void makeDefined()Flags the current BigRational as determinable (default). -
isUndefined
public boolean isUndefined()Checks whether the current BigRational is flagged as indeterminable.- Returns:
- True if it is indeterminable, false otherwise.
-
mod
Returns (this mod m), allowing for negative numbers or zeros as results.- Parameters:
m- The divisor- Returns:
- The result of the modulo operation
-
modNormal
Returns (this mod m), allowing for positive numbers or zeros as results.- Parameters:
m- The divisor- Returns:
- The result of the modulo operation
-
log
public double log()Returns the logarithm of this BigRational number- Returns:
- The logarithm of this BigRational number
-