Class BigFraction
- java.lang.Object
-
- com.seedfinding.latticg.math.component.BigFraction
-
- All Implemented Interfaces:
java.lang.Comparable<BigFraction>
public final class BigFraction extends java.lang.Object implements java.lang.Comparable<BigFraction>
Can store any rational number.Simplification rules:
- If the numerator is zero, then the denominator is one
- The numerator holds the sign of the fraction, and the denominator is always positive
- If the numerator is not zero, then the numerator and the denominator are coprime
This class is immutable, meaning all operations return the result, and no operation can modify this fraction
-
-
Field Summary
Fields Modifier and Type Field Description static BigFraction
EXP
static BigFraction
HALF
static BigFraction
LOG_10
static BigFraction
LOG_PI
static BigFraction
MINUS_ONE
static BigFraction
ONE
static BigFraction
PI
static java.math.BigInteger
TWO
https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/math/BigInteger.java#l1182 The BigInteger constant two.static BigFraction
ZERO
-
Constructor Summary
Constructors Constructor Description BigFraction(long numerator)
Creates aBigFraction
representing the given integerBigFraction(long numerator, long denominator)
Creates aBigFraction
with the given numerator and denominatorBigFraction(java.math.BigInteger numerator)
Creates aBigFraction
representing the given integerBigFraction(java.math.BigInteger numerator, java.math.BigInteger denominator)
Creates aBigFraction
with the given numerator and denominator
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigFraction
abs()
Returns the absolute value of this fractionBigFraction
add(long other)
Adds this fraction to the given integerBigFraction
add(BigFraction other)
Adds this fraction to the other fractionBigFraction
add(java.math.BigInteger other)
Adds this fraction to the given integerjava.math.BigInteger
ceil()
Returns the smallest integerk
such thatk >= this
int
compareTo(BigFraction other)
int
compareTo(java.math.BigInteger other)
BigFraction
divide(long other)
Divides this fraction by the given integerBigFraction
divide(BigFraction other)
Divides this fraction by the other fractionBigFraction
divide(java.math.BigInteger other)
Divides this fraction by the given integerboolean
equals(java.lang.Object other)
BigFraction
exp()
Returns the exponential value of this fraction Using simple serie expansionjava.math.BigInteger
floor()
Returns the largest integerk
such thatk <= this
java.math.BigInteger
getDenominator()
Returns the denominator of this fractionjava.math.BigInteger
getNumerator()
Returns the numerator of this fractionint
hashCode()
BigFraction
log()
Returns the logarithm value of this fraction Using simple series expansionBigFraction
multiply(long other)
Multiplies this fraction with the given integerBigFraction
multiply(BigFraction other)
Multiplies this fraction with the other fractionBigFraction
multiply(java.math.BigInteger other)
Multiplies this fraction with the given integerBigFraction
negate()
Returns -this
static BigFraction
parse(StringParser parser)
Parses a fraction from a string parserstatic BigFraction
parse(java.lang.String str)
Parses a string into a fraction.BigFraction
reciprocal()
Returnsthis
-1java.math.BigInteger
round()
Returns the closest integer to this fraction.int
signum()
Returns the sign of this fractionBigFraction
subtract(long other)
Subtracts the given integer from this fractionBigFraction
subtract(BigFraction other)
Subtracts the other fraction from this fractionBigFraction
subtract(java.math.BigInteger other)
Subtracts the given integer from this fractionjava.math.BigDecimal
toBigDecimal(java.math.MathContext mc)
Converts this fraction to aBigDecimal
, rounding with the givenMathContext
where necessarydouble
toDouble()
Converts this fraction to adouble
, rounding where necessaryjava.lang.String
toString()
-
-
-
Field Detail
-
ZERO
public static final BigFraction ZERO
-
ONE
public static final BigFraction ONE
-
HALF
public static final BigFraction HALF
-
MINUS_ONE
public static final BigFraction MINUS_ONE
-
PI
public static final BigFraction PI
-
LOG_PI
public static final BigFraction LOG_PI
-
LOG_10
public static final BigFraction LOG_10
-
EXP
public static final BigFraction EXP
-
TWO
public static final java.math.BigInteger TWO
https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/math/BigInteger.java#l1182 The BigInteger constant two. (Not exported.) private static final BigInteger TWO = valueOf(2); since 9 it is public http://hg.openjdk.java.net/jdk/jdk11/file/tip/src/java.base/share/classes/java/math/BigInteger.java#l1277
-
-
Constructor Detail
-
BigFraction
public BigFraction(java.math.BigInteger numerator, java.math.BigInteger denominator)
Creates aBigFraction
with the given numerator and denominator- Parameters:
numerator
- The numeratordenominator
- The denominator- Throws:
java.lang.ArithmeticException
- Ifdenominator
is zero
-
BigFraction
public BigFraction(long numerator, long denominator)
Creates aBigFraction
with the given numerator and denominator- Parameters:
numerator
- The numeratordenominator
- The denominator- Throws:
java.lang.ArithmeticException
- Ifdenominator
is zero
-
BigFraction
public BigFraction(java.math.BigInteger numerator)
Creates aBigFraction
representing the given integer- Parameters:
numerator
- The integer to represent
-
BigFraction
public BigFraction(long numerator)
Creates aBigFraction
representing the given integer- Parameters:
numerator
- The integer to represent
-
-
Method Detail
-
parse
public static BigFraction parse(java.lang.String str)
Parses a string into a fraction.If the input string does not contain a "/", then the input is assumed to be an integer. Otherwise, the numerator and denominator are separated by a single "/" character and possibly some whitespace
- Parameters:
str
- The string to parse- Returns:
- A fraction represented by the input string
- Throws:
ParseException
- If the input string has invalid format, or the denominator is zero
-
parse
public static BigFraction parse(StringParser parser)
Parses a fraction from a string parser- Parameters:
parser
- The parser to parse from- Returns:
- A fraction from the input
- Throws:
ParseException
- If the input has invalid format, or the denominator is zero
-
getNumerator
public java.math.BigInteger getNumerator()
Returns the numerator of this fraction- Returns:
- The numerator of this fraction
-
getDenominator
public java.math.BigInteger getDenominator()
Returns the denominator of this fraction- Returns:
- The denominator of this fraction
-
toBigDecimal
public java.math.BigDecimal toBigDecimal(java.math.MathContext mc)
Converts this fraction to aBigDecimal
, rounding with the givenMathContext
where necessary- Parameters:
mc
- The math context for rounding- Returns:
- A
BigDecimal
approximation of this fraction
-
toDouble
public double toDouble()
Converts this fraction to adouble
, rounding where necessary- Returns:
- A
double
approximation of this fraction
-
add
public BigFraction add(BigFraction other)
Adds this fraction to the other fraction- Parameters:
other
- The fraction to add- Returns:
- The result of the sum
-
add
public BigFraction add(java.math.BigInteger other)
Adds this fraction to the given integer- Parameters:
other
- The integer to add- Returns:
- The result of the addition
-
add
public BigFraction add(long other)
Adds this fraction to the given integer- Parameters:
other
- The integer to add- Returns:
- The result of the addition
-
subtract
public BigFraction subtract(BigFraction other)
Subtracts the other fraction from this fraction- Parameters:
other
- The fraction to subtract- Returns:
- The result of the subtraction
-
subtract
public BigFraction subtract(java.math.BigInteger other)
Subtracts the given integer from this fraction- Parameters:
other
- The integer to subtract- Returns:
- The result of the addition
-
subtract
public BigFraction subtract(long other)
Subtracts the given integer from this fraction- Parameters:
other
- The integer to subtract- Returns:
- The result of the addition
-
multiply
public BigFraction multiply(BigFraction other)
Multiplies this fraction with the other fraction- Parameters:
other
- The fraction to multiply by- Returns:
- The result of the multiplication
-
multiply
public BigFraction multiply(java.math.BigInteger other)
Multiplies this fraction with the given integer- Parameters:
other
- The integer to multiply by- Returns:
- The result of the multiplication
-
multiply
public BigFraction multiply(long other)
Multiplies this fraction with the given integer- Parameters:
other
- The integer to multiply by- Returns:
- The result of the multiplication
-
divide
public BigFraction divide(BigFraction other)
Divides this fraction by the other fraction- Parameters:
other
- The fraction to divide by- Returns:
- The result of the division
- Throws:
java.lang.ArithmeticException
- Ifother
is zero
-
divide
public BigFraction divide(java.math.BigInteger other)
Divides this fraction by the given integer- Parameters:
other
- The integer to divide by- Returns:
- The result of the division
- Throws:
java.lang.ArithmeticException
- Ifother
is zero
-
divide
public BigFraction divide(long other)
Divides this fraction by the given integer- Parameters:
other
- The integer to divide by- Returns:
- The result of the division
- Throws:
java.lang.ArithmeticException
- Ifother
is zero
-
negate
public BigFraction negate()
Returns -this
- Returns:
- The result of the negation
-
reciprocal
public BigFraction reciprocal()
Returnsthis
-1- Returns:
- The result of the reciprocation
- Throws:
java.lang.ArithmeticException
- If this fraction is zero
-
floor
public java.math.BigInteger floor()
Returns the largest integerk
such thatk <= this
- Returns:
- The floor of this fraction
-
ceil
public java.math.BigInteger ceil()
Returns the smallest integerk
such thatk >= this
- Returns:
- The floor of this fraction
-
round
public java.math.BigInteger round()
Returns the closest integer to this fraction. In the case of there being 2 closest integers, returns the higher of the two- Returns:
- This fraction rounded to the nearest integer
-
signum
public int signum()
Returns the sign of this fraction- Returns:
- -1 if negative, 0 if zero, 1 if positive
-
abs
public BigFraction abs()
Returns the absolute value of this fraction- Returns:
- The absolute value of this fraction, always non-negative.
-
exp
public BigFraction exp()
Returns the exponential value of this fraction Using simple serie expansion- Returns:
- The exponential value of this fraction.
-
log
public BigFraction log()
Returns the logarithm value of this fraction Using simple series expansion- Returns:
- The logarithm value of this fraction.
-
compareTo
public int compareTo(BigFraction other)
- Specified by:
compareTo
in interfacejava.lang.Comparable<BigFraction>
-
compareTo
public int compareTo(java.math.BigInteger other)
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-