Class BigFraction
- java.lang.Object
-
- java.lang.Number
-
- org.apache.commons.numbers.fraction.BigFraction
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<BigFraction>
,Addition<BigFraction>
,Multiplication<BigFraction>
,NativeOperators<BigFraction>
public final class BigFraction extends java.lang.Number implements java.lang.Comparable<BigFraction>, NativeOperators<BigFraction>, java.io.Serializable
Representation of a rational number using arbitrary precision.The number is expressed as the quotient
p/q
of twoBigInteger
s, a numeratorp
and a non-zero denominatorq
.This class is immutable. Rational number
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_MAX_ITERATIONS
The default iterations used for convergence.private java.math.BigInteger
denominator
The denominator of this fraction reduced to lowest terms.private static java.lang.String
NOT_FINITE
Message for non-finite input double argument to factory constructors.private java.math.BigInteger
numerator
The numerator of this fraction reduced to lowest terms.static BigFraction
ONE
A fraction representing "1".private static long
OVERFLOW
The overflow limit for conversion from a double (2^31).private static long
serialVersionUID
Serializable version identifier.static BigFraction
ZERO
A fraction representing "0".
-
Constructor Summary
Constructors Modifier Constructor Description private
BigFraction(java.math.BigInteger num)
Private constructor: Instances are created using factory methods.private
BigFraction(java.math.BigInteger num, java.math.BigInteger den)
Private constructor: Instances are created using factory methods.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigFraction
abs()
Returns the absolute value of this fraction.BigFraction
add(int value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.BigFraction
add(long value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.BigFraction
add(java.math.BigInteger value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.BigFraction
add(BigFraction value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.java.math.BigDecimal
bigDecimalValue()
Returns theBigDecimal
representation of this fraction.java.math.BigDecimal
bigDecimalValue(int scale, java.math.RoundingMode roundingMode)
Returns theBigDecimal
representation of this fraction.java.math.BigDecimal
bigDecimalValue(java.math.RoundingMode roundingMode)
Returns theBigDecimal
representation of this fraction.int
compareTo(BigFraction other)
Compares this object with the specified object for order using the signed magnitude.BigFraction
divide(int value)
Divide this fraction by the passedvalue
, returning the result in reduced form.BigFraction
divide(long value)
Divide this fraction by the passedvalue
, returning the result in reduced form.BigFraction
divide(java.math.BigInteger value)
Divide this fraction by the passedvalue
, returning the result in reduced form.BigFraction
divide(BigFraction value)
Divide this fraction by the passedvalue
, returning the result in reduced form.double
doubleValue()
Returns thedouble
value closest to this fraction.boolean
equals(java.lang.Object other)
Test for equality with another object.float
floatValue()
Returns thefloat
value closest to this fraction.static BigFraction
from(double value)
Create a fraction given the double value.static BigFraction
from(double value, double epsilon, int maxIterations)
Create a fraction given the double value and maximum error allowed.private static BigFraction
from(double value, double epsilon, int maxDenominator, int maxIterations)
Create a fraction given the double value and either the maximum error allowed or the maximum number of denominator digits.static BigFraction
from(double value, int maxDenominator)
Create a fraction given the double value and maximum denominator.java.math.BigInteger
getDenominator()
Access the denominator as aBigInteger
.int
getDenominatorAsInt()
Access the denominator as anint
.long
getDenominatorAsLong()
Access the denominator as along
.java.math.BigInteger
getNumerator()
Access the numerator as aBigInteger
.int
getNumeratorAsInt()
Access the numerator as anint
.long
getNumeratorAsLong()
Access the numerator as along
.int
hashCode()
int
intValue()
Returns the whole number part of the fraction.boolean
isOne()
Check if this is a neutral element of multiplication, i.e.boolean
isZero()
Check if this is a neutral element of addition, i.e.long
longValue()
Returns the whole number part of the fraction.BigFraction
multiply(int value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.BigFraction
multiply(long value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.BigFraction
multiply(java.math.BigInteger value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.BigFraction
multiply(BigFraction value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.BigFraction
negate()
Additive inverse.static BigFraction
of(int num)
Create a fraction given the numerator.static BigFraction
of(int num, int den)
Create a fraction given the numerator and denominator.static BigFraction
of(long num)
Create a fraction given the numerator.static BigFraction
of(long num, long den)
Create a fraction given the numerator and denominator.static BigFraction
of(java.math.BigInteger num)
Create a fraction given the numerator.static BigFraction
of(java.math.BigInteger num, java.math.BigInteger den)
Create a fraction given the numerator and denominator.BigFraction
one()
Identity element.static BigFraction
parse(java.lang.String s)
Returns aBigFraction
instance representing the specified strings
.BigFraction
pow(int exponent)
Returns aBigFraction
whose value isthisexponent
, returning the result in reduced form.BigFraction
reciprocal()
Multiplicative inverse.private static java.math.BigInteger
roundAndRightShift(java.math.BigInteger value, int bits, boolean hasFractionalBits)
Rounds an integer to the specified power of two (i.e.int
signum()
Retrieves the sign of this fraction.BigFraction
subtract(int value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.BigFraction
subtract(long value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.BigFraction
subtract(java.math.BigInteger value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.BigFraction
subtract(BigFraction value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.private long
toFloatingPointBits(int exponentLength, int significandLength)
Calculates the sign bit, the biased exponent and the significand for a binary floating-point representation of thisBigFraction
according to the IEEE 754 standard, and encodes these values into along
variable.java.lang.String
toString()
Returns theString
representing this fraction.BigFraction
zero()
Identity element.
-
-
-
Field Detail
-
ZERO
public static final BigFraction ZERO
A fraction representing "0".
-
ONE
public static final BigFraction ONE
A fraction representing "1".
-
serialVersionUID
private static final long serialVersionUID
Serializable version identifier.- See Also:
- Constant Field Values
-
DEFAULT_MAX_ITERATIONS
private static final int DEFAULT_MAX_ITERATIONS
The default iterations used for convergence.- See Also:
- Constant Field Values
-
NOT_FINITE
private static final java.lang.String NOT_FINITE
Message for non-finite input double argument to factory constructors.- See Also:
- Constant Field Values
-
OVERFLOW
private static final long OVERFLOW
The overflow limit for conversion from a double (2^31).- See Also:
- Constant Field Values
-
numerator
private final java.math.BigInteger numerator
The numerator of this fraction reduced to lowest terms.
-
denominator
private final java.math.BigInteger denominator
The denominator of this fraction reduced to lowest terms.
-
-
Constructor Detail
-
BigFraction
private BigFraction(java.math.BigInteger num, java.math.BigInteger den)
Private constructor: Instances are created using factory methods.This constructor should only be invoked when the fraction is known to be non-zero; otherwise use
ZERO
. This avoids creating the zero representation0 / -1
.- Parameters:
num
- Numerator, must not benull
.den
- Denominator, must not benull
.- Throws:
java.lang.ArithmeticException
- if the denominator is zero.
-
BigFraction
private BigFraction(java.math.BigInteger num)
Private constructor: Instances are created using factory methods.This sets the denominator to 1.
- Parameters:
num
- Numerator (must not be null).
-
-
Method Detail
-
from
private static BigFraction from(double value, double epsilon, int maxDenominator, int maxIterations)
Create a fraction given the double value and either the maximum error allowed or the maximum number of denominator digits.NOTE: This method is called with:
- EITHER a valid epsilon value and the maxDenominator set to Integer.MAX_VALUE (that way the maxDenominator has no effect)
- OR a valid maxDenominator value and the epsilon value set to zero (that way epsilon only has effect if there is an exact match before the maxDenominator value is reached).
It has been done this way so that the same code can be reused for both scenarios. However this could be confusing to users if it were part of the public API and this method should therefore remain PRIVATE.
See JIRA issue ticket MATH-181 for more details: https://issues.apache.org/jira/browse/MATH-181
- Parameters:
value
- Value to convert to a fraction.epsilon
- Maximum error allowed. The resulting fraction is withinepsilon
ofvalue
, in absolute terms.maxDenominator
- Maximum denominator value allowed.maxIterations
- Maximum number of convergents.- Returns:
- a new instance.
- Throws:
java.lang.IllegalArgumentException
- if the givenvalue
is NaN or infinite.java.lang.ArithmeticException
- if the continued fraction failed to converge.
-
from
public static BigFraction from(double value)
Create a fraction given the double value.This factory method behaves differently to the method
from(double, double, int)
. It converts the double value exactly, considering its internal bits representation. This works for all values except NaN and infinities and does not requires any loop or convergence threshold.Since this conversion is exact and since double numbers are sometimes approximated, the fraction created may seem strange in some cases. For example, calling
from(1.0 / 3.0)
does not create the fraction \( \frac{1}{3} \), but the fraction \( \frac{6004799503160661}{18014398509481984} \) because the double number passed to the method is not exactly \( \frac{1}{3} \) (which cannot be represented exactly in IEEE754).- Parameters:
value
- Value to convert to a fraction.- Returns:
- a new instance.
- Throws:
java.lang.IllegalArgumentException
- if the givenvalue
is NaN or infinite.- See Also:
from(double,double,int)
-
from
public static BigFraction from(double value, double epsilon, int maxIterations)
Create a fraction given the double value and maximum error allowed.References:
- Continued Fraction equations (11) and (22)-(26)
- Parameters:
value
- Value to convert to a fraction.epsilon
- Maximum error allowed. The resulting fraction is withinepsilon
ofvalue
, in absolute terms.maxIterations
- Maximum number of convergents.- Returns:
- a new instance.
- Throws:
java.lang.IllegalArgumentException
- if the givenvalue
is NaN or infinite;epsilon
is not positive; ormaxIterations < 1
.java.lang.ArithmeticException
- if the continued fraction failed to converge.
-
from
public static BigFraction from(double value, int maxDenominator)
Create a fraction given the double value and maximum denominator.References:
- Continued Fraction equations (11) and (22)-(26)
Note: The magnitude of the
maxDenominator
is used allowing use ofInteger.MIN_VALUE
for a supported maximum denominator of 231.- Parameters:
value
- Value to convert to a fraction.maxDenominator
- Maximum allowed value for denominator.- Returns:
- a new instance.
- Throws:
java.lang.IllegalArgumentException
- if the givenvalue
is NaN or infinite ormaxDenominator
is zero.java.lang.ArithmeticException
- if the continued fraction failed to converge.
-
of
public static BigFraction of(int num)
Create a fraction given the numerator. The denominator is1
.- Parameters:
num
- the numerator.- Returns:
- a new instance.
-
of
public static BigFraction of(long num)
Create a fraction given the numerator. The denominator is1
.- Parameters:
num
- Numerator.- Returns:
- a new instance.
-
of
public static BigFraction of(java.math.BigInteger num)
Create a fraction given the numerator. The denominator is1
.- Parameters:
num
- Numerator.- Returns:
- a new instance.
- Throws:
java.lang.NullPointerException
- if numerator is null.
-
of
public static BigFraction of(int num, int den)
Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.- Parameters:
num
- Numerator.den
- Denominator.- Returns:
- a new instance.
- Throws:
java.lang.ArithmeticException
- ifden
is zero.
-
of
public static BigFraction of(long num, long den)
Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.- Parameters:
num
- Numerator.den
- Denominator.- Returns:
- a new instance.
- Throws:
java.lang.ArithmeticException
- ifden
is zero.
-
of
public static BigFraction of(java.math.BigInteger num, java.math.BigInteger den)
Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.- Parameters:
num
- Numerator.den
- Denominator.- Returns:
- a new instance.
- Throws:
java.lang.NullPointerException
- if numerator or denominator are null.java.lang.ArithmeticException
- if the denominator is zero.
-
parse
public static BigFraction parse(java.lang.String s)
Returns aBigFraction
instance representing the specified strings
.If
s
isnull
, then aNullPointerException
is thrown.The string must be in a format compatible with that produced by
BigFraction.toString()
. The format expects an integer optionally followed by a'/'
character and and second integer. Leading and trailing spaces are allowed around each numeric part. Each numeric part is parsed usingBigInteger(String)
. The parts are interpreted as the numerator and optional denominator of the fraction. If absent the denominator is assumed to be "1".Examples of valid strings and the equivalent
BigFraction
are shown below:"0" = BigFraction.of(0) "42" = BigFraction.of(42) "0 / 1" = BigFraction.of(0, 1) "1 / 3" = BigFraction.of(1, 3) "-4 / 13" = BigFraction.of(-4, 13)
Note: The fraction is returned in reduced form and the numerator and denominator may not match the values in the input string. For this reason the result of
BigFraction.parse(s).toString().equals(s)
may not betrue
.- Parameters:
s
- String representation.- Returns:
- an instance.
- Throws:
java.lang.NullPointerException
- if the string is null.java.lang.NumberFormatException
- if the string does not contain a parsable fraction.- See Also:
BigInteger(String)
,toString()
-
zero
public BigFraction zero()
Description copied from interface:Addition
Identity element.- Specified by:
zero
in interfaceAddition<BigFraction>
- Returns:
- the field element such that for all
a
,zero().add(a).equals(a)
istrue
.
-
isZero
public boolean isZero()
Check if this is a neutral element of addition, i.e.this.add(a)
returnsa
or an element representing the same value asa
.The default implementation calls
equals(zero())
. Implementations may want to employ more a efficient method. This may even be required if an implementation has multiple representations ofzero
and itsequals
method differentiates between them.- Specified by:
isZero
in interfaceAddition<BigFraction>
- Returns:
true
ifthis
is a neutral element of addition.- See Also:
Addition.zero()
-
one
public BigFraction one()
Description copied from interface:Multiplication
Identity element.- Specified by:
one
in interfaceMultiplication<BigFraction>
- Returns:
- the field element such that for all
a
,one().multiply(a).equals(a)
istrue
.
-
isOne
public boolean isOne()
Check if this is a neutral element of multiplication, i.e.this.multiply(a)
returnsa
or an element representing the same value asa
.The default implementation calls
equals(one())
. Implementations may want to employ more a efficient method. This may even be required if an implementation has multiple representations ofone
and itsequals
method differentiates between them.- Specified by:
isOne
in interfaceMultiplication<BigFraction>
- Returns:
true
ifthis
is a neutral element of multiplication.- See Also:
Multiplication.one()
-
getNumerator
public java.math.BigInteger getNumerator()
Access the numerator as aBigInteger
.- Returns:
- the numerator as a
BigInteger
.
-
getNumeratorAsInt
public int getNumeratorAsInt()
Access the numerator as anint
.- Returns:
- the numerator as an
int
.
-
getNumeratorAsLong
public long getNumeratorAsLong()
Access the numerator as along
.- Returns:
- the numerator as a
long
.
-
getDenominator
public java.math.BigInteger getDenominator()
Access the denominator as aBigInteger
.- Returns:
- the denominator as a
BigInteger
.
-
getDenominatorAsInt
public int getDenominatorAsInt()
Access the denominator as anint
.- Returns:
- the denominator as an
int
.
-
getDenominatorAsLong
public long getDenominatorAsLong()
Access the denominator as along
.- Returns:
- the denominator as a
long
.
-
signum
public int signum()
Retrieves the sign of this fraction.- Returns:
- -1 if the value is strictly negative, 1 if it is strictly positive, 0 if it is 0.
-
abs
public BigFraction abs()
Returns the absolute value of this fraction.- Returns:
- the absolute value.
-
negate
public BigFraction negate()
Description copied from interface:Addition
Additive inverse.- Specified by:
negate
in interfaceAddition<BigFraction>
- Returns:
-this
.
-
reciprocal
public BigFraction reciprocal()
Multiplicative inverse.Raises an exception if the fraction is equal to zero.
- Specified by:
reciprocal
in interfaceMultiplication<BigFraction>
- Returns:
this-1
.- Throws:
java.lang.ArithmeticException
- if the current numerator iszero
-
doubleValue
public double doubleValue()
Returns thedouble
value closest to this fraction.- Specified by:
doubleValue
in classjava.lang.Number
- Returns:
- the fraction as a
double
.
-
floatValue
public float floatValue()
Returns thefloat
value closest to this fraction.- Specified by:
floatValue
in classjava.lang.Number
- Returns:
- the fraction as a
double
.
-
intValue
public int intValue()
Returns the whole number part of the fraction.- Specified by:
intValue
in classjava.lang.Number
- Returns:
- the largest
int
value that is not larger than this fraction.
-
longValue
public long longValue()
Returns the whole number part of the fraction.- Specified by:
longValue
in classjava.lang.Number
- Returns:
- the largest
long
value that is not larger than this fraction.
-
bigDecimalValue
public java.math.BigDecimal bigDecimalValue()
Returns theBigDecimal
representation of this fraction. This calculates the fraction as numerator divided by denominator.- Returns:
- the fraction as a
BigDecimal
. - Throws:
java.lang.ArithmeticException
- if the exact quotient does not have a terminating decimal expansion.- See Also:
BigDecimal
-
bigDecimalValue
public java.math.BigDecimal bigDecimalValue(java.math.RoundingMode roundingMode)
Returns theBigDecimal
representation of this fraction. This calculates the fraction as numerator divided by denominator following the passed rounding mode.- Parameters:
roundingMode
- Rounding mode to apply.- Returns:
- the fraction as a
BigDecimal
. - See Also:
BigDecimal
-
bigDecimalValue
public java.math.BigDecimal bigDecimalValue(int scale, java.math.RoundingMode roundingMode)
Returns theBigDecimal
representation of this fraction. This calculates the fraction as numerator divided by denominator following the passed scale and rounding mode.- Parameters:
scale
- scale of theBigDecimal
quotient to be returned. seeBigDecimal
for more information.roundingMode
- Rounding mode to apply.- Returns:
- the fraction as a
BigDecimal
. - Throws:
java.lang.ArithmeticException
- ifroundingMode
==RoundingMode.UNNECESSARY
and the specified scale is insufficient to represent the result of the division exactly.- See Also:
BigDecimal
-
add
public BigFraction add(int value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.- Parameters:
value
- Value to add.- Returns:
this + value
.
-
add
public BigFraction add(long value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.- Parameters:
value
- Value to add.- Returns:
this + value
.
-
add
public BigFraction add(java.math.BigInteger value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.- Parameters:
value
- Value to add.- Returns:
this + value
.
-
add
public BigFraction add(BigFraction value)
Adds the specifiedvalue
to this fraction, returning the result in reduced form.- Specified by:
add
in interfaceAddition<BigFraction>
- Parameters:
value
- Value to add.- Returns:
this + value
.
-
subtract
public BigFraction subtract(int value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.- Parameters:
value
- Value to subtract.- Returns:
this - value
.
-
subtract
public BigFraction subtract(long value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.- Parameters:
value
- Value to subtract.- Returns:
this - value
.
-
subtract
public BigFraction subtract(java.math.BigInteger value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.- Parameters:
value
- Value to subtract.- Returns:
this - value
.
-
subtract
public BigFraction subtract(BigFraction value)
Subtracts the specifiedvalue
from this fraction, returning the result in reduced form.- Specified by:
subtract
in interfaceNativeOperators<BigFraction>
- Parameters:
value
- Value to subtract.- Returns:
this - value
.
-
multiply
public BigFraction multiply(int value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.- Specified by:
multiply
in interfaceNativeOperators<BigFraction>
- Parameters:
value
- Value to multiply by.- Returns:
this * value
.
-
multiply
public BigFraction multiply(long value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.- Parameters:
value
- Value to multiply by.- Returns:
this * value
.
-
multiply
public BigFraction multiply(java.math.BigInteger value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.- Parameters:
value
- Value to multiply by.- Returns:
this * value
.
-
multiply
public BigFraction multiply(BigFraction value)
Multiply this fraction by the passedvalue
, returning the result in reduced form.- Specified by:
multiply
in interfaceMultiplication<BigFraction>
- Parameters:
value
- Value to multiply by.- Returns:
this * value
.
-
divide
public BigFraction divide(int value)
Divide this fraction by the passedvalue
, returning the result in reduced form.- Parameters:
value
- Value to divide by- Returns:
this / value
.- Throws:
java.lang.ArithmeticException
- if the value to divide by is zero
-
divide
public BigFraction divide(long value)
Divide this fraction by the passedvalue
, returning the result in reduced form.- Parameters:
value
- Value to divide by- Returns:
this / value
.- Throws:
java.lang.ArithmeticException
- if the value to divide by is zero
-
divide
public BigFraction divide(java.math.BigInteger value)
Divide this fraction by the passedvalue
, returning the result in reduced form.- Parameters:
value
- Value to divide by- Returns:
this / value
.- Throws:
java.lang.ArithmeticException
- if the value to divide by is zero
-
divide
public BigFraction divide(BigFraction value)
Divide this fraction by the passedvalue
, returning the result in reduced form.- Specified by:
divide
in interfaceNativeOperators<BigFraction>
- Parameters:
value
- Value to divide by- Returns:
this / value
.- Throws:
java.lang.ArithmeticException
- if the value to divide by is zero
-
pow
public BigFraction pow(int exponent)
Returns aBigFraction
whose value isthisexponent
, returning the result in reduced form.- Specified by:
pow
in interfaceNativeOperators<BigFraction>
- Parameters:
exponent
- exponent to which thisBigFraction
is to be raised.- Returns:
thisexponent
.- Throws:
java.lang.ArithmeticException
- if the intermediate result would overflow.
-
toString
public java.lang.String toString()
Returns theString
representing this fraction. Uses:"0"
ifnumerator
is zero."numerator"
ifdenominator
is one."numerator / denominator"
for all other cases.
- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of the fraction.
-
compareTo
public int compareTo(BigFraction other)
Compares this object with the specified object for order using the signed magnitude.- Specified by:
compareTo
in interfacejava.lang.Comparable<BigFraction>
- Parameters:
other
-- Returns:
-
equals
public boolean equals(java.lang.Object other)
Test for equality with another object. If the other object is aFraction
then a comparison is made of the sign and magnitude; otherwisefalse
is returned.- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
-- Returns:
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toFloatingPointBits
private long toFloatingPointBits(int exponentLength, int significandLength)
Calculates the sign bit, the biased exponent and the significand for a binary floating-point representation of thisBigFraction
according to the IEEE 754 standard, and encodes these values into along
variable. The representative bits are arranged adjacent to each other and placed at the low-order end of the returnedlong
value, with the least significant bits used for the significand, the next more significant bits for the exponent, and next more significant bit for the sign.Warning: The arguments are not validated.
- Parameters:
exponentLength
- the number of bits allowed for the exponent; must be between 1 and 32 (inclusive), and must not be greater than63 - significandLength
significandLength
- the number of bits allowed for the significand (excluding the implicit leading 1-bit in normalized numbers, e.g. 52 for a double-precision floating-point number); must be between 1 and63 - exponentLength
(inclusive)- Returns:
- the bits of an IEEE 754 binary floating-point representation of
this fraction encoded in a
long
, as described above.
-
roundAndRightShift
private static java.math.BigInteger roundAndRightShift(java.math.BigInteger value, int bits, boolean hasFractionalBits)
Rounds an integer to the specified power of two (i.e. the minimum number of low-order bits that must be zero) and performs a right-shift by this amount. The rounding mode applied is round to nearest, with ties rounding to even (meaning the prospective least significant bit must be zero). The number can optionally be treated as though it contained at least one 0-bit and one 1-bit in its fractional part, to influence the result in cases that would otherwise be a tie.- Parameters:
value
- the number to round and right-shiftbits
- the power of two to which to round; must be positivehasFractionalBits
- whether the number should be treated as though it contained a non-zero fractional part- Returns:
- a
BigInteger
as described above
-
-