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 BigFractionEXPstatic BigFractionHALFstatic BigFractionLOG_10static BigFractionLOG_PIstatic BigFractionMINUS_ONEstatic BigFractionONEstatic BigFractionPIstatic java.math.BigIntegerTWOhttps://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/math/BigInteger.java#l1182 The BigInteger constant two.static BigFractionZERO
-
Constructor Summary
Constructors Constructor Description BigFraction(long numerator)Creates aBigFractionrepresenting the given integerBigFraction(long numerator, long denominator)Creates aBigFractionwith the given numerator and denominatorBigFraction(java.math.BigInteger numerator)Creates aBigFractionrepresenting the given integerBigFraction(java.math.BigInteger numerator, java.math.BigInteger denominator)Creates aBigFractionwith the given numerator and denominator
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigFractionabs()Returns the absolute value of this fractionBigFractionadd(long other)Adds this fraction to the given integerBigFractionadd(BigFraction other)Adds this fraction to the other fractionBigFractionadd(java.math.BigInteger other)Adds this fraction to the given integerjava.math.BigIntegerceil()Returns the smallest integerksuch thatk >= thisintcompareTo(BigFraction other)intcompareTo(java.math.BigInteger other)BigFractiondivide(long other)Divides this fraction by the given integerBigFractiondivide(BigFraction other)Divides this fraction by the other fractionBigFractiondivide(java.math.BigInteger other)Divides this fraction by the given integerbooleanequals(java.lang.Object other)BigFractionexp()Returns the exponential value of this fraction Using simple serie expansionjava.math.BigIntegerfloor()Returns the largest integerksuch thatk <= thisjava.math.BigIntegergetDenominator()Returns the denominator of this fractionjava.math.BigIntegergetNumerator()Returns the numerator of this fractioninthashCode()BigFractionlog()Returns the logarithm value of this fraction Using simple series expansionBigFractionmultiply(long other)Multiplies this fraction with the given integerBigFractionmultiply(BigFraction other)Multiplies this fraction with the other fractionBigFractionmultiply(java.math.BigInteger other)Multiplies this fraction with the given integerBigFractionnegate()Returns -thisstatic BigFractionparse(StringParser parser)Parses a fraction from a string parserstatic BigFractionparse(java.lang.String str)Parses a string into a fraction.BigFractionreciprocal()Returnsthis-1java.math.BigIntegerround()Returns the closest integer to this fraction.intsignum()Returns the sign of this fractionBigFractionsubtract(long other)Subtracts the given integer from this fractionBigFractionsubtract(BigFraction other)Subtracts the other fraction from this fractionBigFractionsubtract(java.math.BigInteger other)Subtracts the given integer from this fractionjava.math.BigDecimaltoBigDecimal(java.math.MathContext mc)Converts this fraction to aBigDecimal, rounding with the givenMathContextwhere necessarydoubletoDouble()Converts this fraction to adouble, rounding where necessaryjava.lang.StringtoString()
-
-
-
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 aBigFractionwith the given numerator and denominator- Parameters:
numerator- The numeratordenominator- The denominator- Throws:
java.lang.ArithmeticException- Ifdenominatoris zero
-
BigFraction
public BigFraction(long numerator, long denominator)Creates aBigFractionwith the given numerator and denominator- Parameters:
numerator- The numeratordenominator- The denominator- Throws:
java.lang.ArithmeticException- Ifdenominatoris zero
-
BigFraction
public BigFraction(java.math.BigInteger numerator)
Creates aBigFractionrepresenting the given integer- Parameters:
numerator- The integer to represent
-
BigFraction
public BigFraction(long numerator)
Creates aBigFractionrepresenting 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 givenMathContextwhere necessary- Parameters:
mc- The math context for rounding- Returns:
- A
BigDecimalapproximation of this fraction
-
toDouble
public double toDouble()
Converts this fraction to adouble, rounding where necessary- Returns:
- A
doubleapproximation 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- Ifotheris 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- Ifotheris 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- Ifotheris 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 integerksuch thatk <= this- Returns:
- The floor of this fraction
-
ceil
public java.math.BigInteger ceil()
Returns the smallest integerksuch 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:
compareToin interfacejava.lang.Comparable<BigFraction>
-
compareTo
public int compareTo(java.math.BigInteger other)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-