org.spaceroots.mantissa.algebra
Class RationalNumber
- java.lang.Object
-
- org.spaceroots.mantissa.algebra.RationalNumber
-
- All Implemented Interfaces:
- java.io.Serializable
public class RationalNumber extends java.lang.Object implements java.io.SerializableThis class implements reduced rational numbers.Instances of this class are immutable.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static RationalNumberONEOne as a rational numer.static RationalNumberZEROZero as a rational numer.
-
Constructor Summary
Constructors Constructor and Description RationalNumber()Simple constructor.RationalNumber(java.math.BigInteger i)Simple constructor.RationalNumber(java.math.BigInteger numerator, java.math.BigInteger denominator)Simple constructor.RationalNumber(long l)Simple constructor.RationalNumber(long numerator, long denominator)Simple constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description static RationalNumberabs(RationalNumber r)Get the absolute value of a rational number.RationalNumberadd(java.math.BigInteger l)Add an integer to the instance.RationalNumberadd(long l)Add an integer to the instance.RationalNumberadd(RationalNumber r)Add a rational number to the instance.RationalNumberdivide(java.math.BigInteger l)Divide the instance by an integer.RationalNumberdivide(long l)Divide the instance by an integer.RationalNumberdivide(RationalNumber r)Divide the instance by a rational number.doubledoubleValue()Return thedoublevalue of the instance.booleanequals(java.lang.Object o)Check if the instance is equal to another rational number.java.math.BigIntegergetDenominator()Get the denominator.java.math.BigIntegergetNumerator()Get the numerator.inthashCode()Returns a hash code value for the object.RationalNumberinvert()Invert the instance.booleanisInteger()Check if the number is integer.booleanisNegative()Check if the number is negative.booleanisOne()Check if the number is one.booleanisZero()Check if the number is zero.RationalNumbermultiply(java.math.BigInteger l)Multiply the instance by an integer.RationalNumbermultiply(long l)Multiply the instance by an integer.RationalNumbermultiply(RationalNumber r)Multiply the instance by a rational number.RationalNumbernegate()Negate the instance.RationalNumbersubtract(java.math.BigInteger l)Subtract an integer from the instance.RationalNumbersubtract(long l)Subtract an integer from the instance.RationalNumbersubtract(RationalNumber r)Subtract a rational number from the instance.java.lang.StringtoString()Returns a string representation of the rational number.
-
-
-
Field Detail
-
ZERO
public static final RationalNumber ZERO
Zero as a rational numer.
-
ONE
public static final RationalNumber ONE
One as a rational numer.
-
-
Constructor Detail
-
RationalNumber
public RationalNumber()
Simple constructor. Build a null rational number
-
RationalNumber
public RationalNumber(long numerator, long denominator)Simple constructor. Build a rational number from a numerator and a denominator.- Parameters:
numerator- numerator of the rational numberdenominator- denominator of the rational number- Throws:
java.lang.ArithmeticException- if the denominator is zero
-
RationalNumber
public RationalNumber(java.math.BigInteger numerator, java.math.BigInteger denominator)Simple constructor. Build a rational number from a numerator and a denominator.- Parameters:
numerator- numerator of the rational numberdenominator- denominator of the rational number- Throws:
java.lang.ArithmeticException- if the denominator is zero
-
RationalNumber
public RationalNumber(long l)
Simple constructor. Build a rational number from a single integer- Parameters:
l- value of the rational number
-
RationalNumber
public RationalNumber(java.math.BigInteger i)
Simple constructor. Build a rational number from a single integer- Parameters:
i- value of the rational number
-
-
Method Detail
-
negate
public RationalNumber negate()
Negate the instance.- Returns:
- a new rational number, opposite to the isntance
-
add
public RationalNumber add(long l)
Add an integer to the instance.- Parameters:
l- integer to add- Returns:
- a new rational number which is the sum of the instance and l
-
add
public RationalNumber add(java.math.BigInteger l)
Add an integer to the instance.- Parameters:
l- integer to add- Returns:
- a new rational number which is the sum of the instance and l
-
add
public RationalNumber add(RationalNumber r)
Add a rational number to the instance.- Parameters:
r- rational number to add- Returns:
- a new rational number which is the sum of the instance and r
-
subtract
public RationalNumber subtract(long l)
Subtract an integer from the instance.- Parameters:
l- integer to subtract- Returns:
- a new rational number which is the difference the instance minus l
-
subtract
public RationalNumber subtract(java.math.BigInteger l)
Subtract an integer from the instance.- Parameters:
l- integer to subtract- Returns:
- a new rational number which is the difference the instance minus l
-
subtract
public RationalNumber subtract(RationalNumber r)
Subtract a rational number from the instance.- Parameters:
r- rational number to subtract- Returns:
- a new rational number which is the difference the instance minus r
-
multiply
public RationalNumber multiply(long l)
Multiply the instance by an integer.- Parameters:
l- integer to multiply by- Returns:
- a new rational number which is the produc of the instance by l
-
multiply
public RationalNumber multiply(java.math.BigInteger l)
Multiply the instance by an integer.- Parameters:
l- integer to multiply by- Returns:
- a new rational number which is the produc of the instance by l
-
multiply
public RationalNumber multiply(RationalNumber r)
Multiply the instance by a rational number.- Parameters:
r- rational number to multiply the instance with- Returns:
- a new rational number which is the product of the instance and r
-
divide
public RationalNumber divide(long l)
Divide the instance by an integer.- Parameters:
l- integer to divide by- Returns:
- a new rational number which is the quotient of the instance by l
- Throws:
java.lang.ArithmeticException- if l is zero
-
divide
public RationalNumber divide(java.math.BigInteger l)
Divide the instance by an integer.- Parameters:
l- integer to divide by- Returns:
- a new rational number which is the quotient of the instance by l
- Throws:
java.lang.ArithmeticException- if l is zero
-
divide
public RationalNumber divide(RationalNumber r)
Divide the instance by a rational number.- Parameters:
r- rational number to divide by- Returns:
- a new rational number which is the quotient of the instance by r
- Throws:
java.lang.ArithmeticException- if r is zero
-
invert
public RationalNumber invert()
Invert the instance.- Returns:
- the inverse of the instance
- Throws:
java.lang.ArithmeticException- if the instance is zero
-
getNumerator
public java.math.BigInteger getNumerator()
Get the numerator.- Returns:
- the signed numerator
-
getDenominator
public java.math.BigInteger getDenominator()
Get the denominator.- Returns:
- the denominator (always positive)
-
isZero
public boolean isZero()
Check if the number is zero.- Returns:
- true if the number is zero
-
isOne
public boolean isOne()
Check if the number is one.- Returns:
- true if the number is one
-
isInteger
public boolean isInteger()
Check if the number is integer.- Returns:
- true if the number is an integer
-
isNegative
public boolean isNegative()
Check if the number is negative.- Returns:
- true if the number is negative
-
abs
public static RationalNumber abs(RationalNumber r)
Get the absolute value of a rational number.- Parameters:
r- rational number from which we want the absolute value- Returns:
- a new rational number which is the absolute value of r
-
doubleValue
public double doubleValue()
Return thedoublevalue of the instance.- Returns:
- the double value of the instance
-
equals
public boolean equals(java.lang.Object o)
Check if the instance is equal to another rational number. Equality here is having the same value.- Overrides:
equalsin classjava.lang.Object- Returns:
- true if the object is a rational number which has the same value as the instance
-
hashCode
public int hashCode()
Returns a hash code value for the object. The hash code value is computed from the reduced numerator and denominator, hence equal rational numbers have the same hash code, as required by the method specification.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code value for this object.
-
toString
public java.lang.String toString()
Returns a string representation of the rational number. The representation is reduced: there is no common factor left between the numerator and the denominator. The '/' character and the denominator are displayed only if the denominator is not one. The sign is on the numerator.- Overrides:
toStringin classjava.lang.Object- Returns:
- string representation of the rational number
-
-
DataMelt 3.0 © DataMelt by jWork.ORG