Class BigFloat
- All Implemented Interfaces:
Serializable, Comparable<BigFloat>
- Direct Known Subclasses:
BigFloat.SpecialBigFloat
BigDecimal which simplifies the consistent usage of the MathContext
and provides a simpler API for calculations.
Overview
Every BigFloat instance has a reference to a BigFloat.Context that specifies the MathContext to be used for all calculations and values.
The API for calculations is simplified and more consistent with the typical mathematical usage.
- Factory methods for values:
valueOf(BigFloat)valueOf(BigDecimal)valueOf(int)valueOf(long)valueOf(double)valueOf(String)pi()e()
- All standard operators:
add(x)subtract(x)multiply(x)remainder(x)pow(y)root(y)
- Calculation methods are overloaded for different value types:
add(BigFloat)add(BigDecimal)add(int)add(long)add(double)- ...
- Mathematical functions are written as they are traditionally are written:
abs(x)log(x)sin(x)min(x1, x2, ...)max(x1, x2, ...)- ...
- Support for advanced mathematical functions:
sqrt(x)log(x)exp(x)sin(x)cos(x)tan(x)- ...
- Methods to access parts of a value:
getMantissa()getExponent()getIntegralPart()getFractionalPart()
- Equals and Hashcode methods:
equals(Object)that returns whether twoBigFloatvalues are mathematically the samehashCode()consistent withequals(Object)
- Comparison methods:
isEqual(BigFloat)isLessThan(BigFloat)isLessThanOrEqual(BigFloat)isGreaterThan(BigFloat)isGreaterThanOrEqual(BigFloat)
Usage
Before doing any calculations you need to create a Context specifying the precision used for all calculations.
Context context = BigFloat.context(100); // precision of 100 digits Context anotherContext = BigFloat.context(new MathContext(10, RoundingMode.HALF_UP); // precision of 10 digits, rounding half up
The Context can then be used to create the first value of the calculation:
BigFloat value1 = context.valueOf(640320);
The BigFloat instance holds a reference to the Context. This context is then passed from calculation to calculation.
BigFloat value2 = context.valueOf(640320).pow(3).divide(24); BigFloat value3 = BigFloat.sin(value2);
The BigFloat result can be converted to other numerical types:
BigDecimal bigDecimalValue = value3.toBigDecimal(); double doubleValue = value3.toDouble(); long longValue = value3.toLong(); int intValue = value3.toInt();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classManages theMathContextand provides factory methods forBigFloatvalues.private static final classthis class handle unrepresentable value in floating-point arithmetic -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BigFloat.Contextstatic final BigFloatRepresents a value that is not a number.static final BigFloatRepresents the positive infinity.static final BigFloatRepresents the positive infinity.private static final longprivate final BigDecimal -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BigFloatReturns theBigFloatthat is theabs(this)(absolute value).static BigFloatReturns theBigFloatthat isacos(x).static BigFloatReturns theBigFloatthat isacosh(x).static BigFloatReturns theBigFloatthat isacot(x).static BigFloatReturns theBigFloatthat isacoth(x).add(double x) Returns theBigFloatthat isthis + x.add(int x) Returns theBigFloatthat isthis + x.add(long x) Returns theBigFloatthat isthis + x.Returns theBigFloatthat isthis + x.add(BigDecimal x) Returns theBigFloatthat isthis + x.static BigFloatReturns theBigFloatthat isasin(x).static BigFloatReturns theBigFloatthat isasinh(x).static BigFloatReturns theBigFloatthat isatan(x).static BigFloatReturns theBigFloatthat isatanh(x).intstatic BigFloat.Contextcontext(int precision) Creates aBigFloat.Contextwith the specified precision andRoundingMode.HALF_UProunding.static BigFloat.Contextcontext(MathContext mathContext) Creates aBigFloat.Contextwith the specifiedMathContext.static BigFloatReturns theBigFloatthat iscos(x).static BigFloatReturns theBigFloatthat iscosh(x).static BigFloatReturns theBigFloatthat iscot(x).static BigFloatReturns theBigFloatthat iscoth(x).divide(double x) Returns theBigFloatthat isthis / x.divide(int x) Returns theBigFloatthat isthis / x.divide(long x) Returns theBigFloatthat isthis / x.Returns theBigFloatthat isthis / x.divide(BigDecimal x) Returns theBigFloatthat isthis / x.booleanstatic BigFloatReturns theBigFloatthat isexp(x).Returns theBigFloat.Contextofthisvalue.Returns the exponent ofthisvalue written as mantissa * 10exponent.Returns the fractional part ofthisvalue (right of the decimal point).Returns the integral part ofthisvalue (left of the decimal point).Returns the mantissa ofthisvalue written as mantissa * 10exponent.inthashCode()static booleanbooleanbooleanReturns whetherthisvalue is mathematically equal to theothervalue.booleanisGreaterThan(BigFloat other) Returns whetherthisvalue is mathematically greater than to theothervalue.booleanisGreaterThanOrEqual(BigFloat other) Returns whetherthisvalue is mathematically greater than or equal to theothervalue.booleanbooleanReturns whetherthisvalue can be represented asint.booleanisLessThan(BigFloat other) Returns whetherthisvalue is mathematically less than to theothervalue.booleanisLessThanOrEqual(BigFloat other) Returns whetherthisvalue is mathematically less than or equal to theothervalue.booleanisNaN()booleanReturns whether thisBigFloatis negative.booleanReturns whether thisBigFloatis positive.private static booleanprotected booleanbooleanisZero()Returns whether thisBigFloatis 0.static BigFloatReturns theBigFloatthat islog(x).static BigFloatReturns theBigFloatthat islog10(x).static BigFloatReturns theBigFloatthat islog2(x).private static BigFloatlogSpecial(BigFloat val) private static BigFloat.Contextmax(BigFloat.Context left, BigFloat.Context right) static BigFloatReturns the the maximum of twoBigFloatvalues.static BigFloatReturns the the maximum of nBigFloatvalues.static BigFloatReturns the the minimum of twoBigFloatvalues.static BigFloatReturns the the minimum of nBigFloatvalues.multiply(double x) Returns theBigFloatthat isthis * x.multiply(int x) Returns theBigFloatthat isthis * x.multiply(long x) Returns theBigFloatthat isthis * x.Returns theBigFloatthat isthis * x.Returns theBigFloatthat isthis * x.static BigFloatReturns theBigFloatthat is- this.pow(double y) pow(int y) pow(long y) static BigFloatReturns theBigFloatthat ispow(x, y).pow(BigDecimal y) remainder(double x) remainder(int x) remainder(long x) root(double y) root(int y) root(long y) static BigFloatReturns theBigFloatthat isroot(x, y).root(BigDecimal y) intsignum()Returns the signum function of thisBigFloat.static BigFloatReturns theBigFloatthat issin(x).static BigFloatReturns theBigFloatthat issinh(x).static BigFloatReturns theBigFloatthat issqrt(x).subtract(double x) Returns theBigFloatthat isthis - x.subtract(int x) Returns theBigFloatthat isthis - x.subtract(long x) Returns theBigFloatthat isthis - x.Returns theBigFloatthat isthis - x.Returns theBigFloatthat isthis - x.static BigFloatReturns theBigFloatthat istan(x).static BigFloatReturns theBigFloatthat istanh(x).Returnsthisvalue as aBigDecimalvalue.doubletoDouble()Returnsthisvalue as adoublevalue.inttoInt()Returnsthisvalue as aintvalue.longtoLong()Returnsthisvalue as alongvalue.toString()protected BigFloat.SpecialBigFloat.Typetype()return special type of a value
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
NaN
-
POSITIVE_INFINITY
-
NEGATIVE_INFINITY
-
value
-
context
-
-
Constructor Details
-
BigFloat
-
-
Method Details
-
context
Creates aBigFloat.Contextwith the specified precision andRoundingMode.HALF_UProunding.- Parameters:
precision- the precision- Returns:
- the
BigFloat.Context
-
context
Creates aBigFloat.Contextwith the specifiedMathContext.- Parameters:
mathContext- theMathContext- Returns:
- the
BigFloat.Context
-
add
Returns theBigFloatthat isthis + x.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
x- the value to add- Returns:
- the resulting
BigFloat - See Also:
-
add
-
add
-
add
-
add
-
subtract
Returns theBigFloatthat isthis - x.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
x- the value to subtract- Returns:
- the resulting
BigFloat - See Also:
-
subtract
-
subtract
-
subtract
-
subtract
-
multiply
Returns theBigFloatthat isthis * x.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
x- the value to multiply- Returns:
- the resulting
BigFloat - See Also:
-
multiply
-
multiply
-
multiply
-
multiply
-
divide
Returns theBigFloatthat isthis / x.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
x- the value to divide with- Returns:
- the resulting
BigFloat - See Also:
-
divide
-
divide
-
divide
-
divide
-
remainder
Returns theBigFloatthat is the remainder when dividingthisbyx.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
x- the value to divide with- Returns:
- the resulting
BigFloat - See Also:
-
remainder
- Parameters:
x- the value to divide with- Returns:
- the resulting
BigFloat - See Also:
-
remainder
-
remainder
-
remainder
-
pow
Returns theBigFloatthat isthisto the power ofy.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
y- the value of the power- Returns:
- the resulting
BigFloat - See Also:
-
pow
- Parameters:
y- the value of the power- Returns:
- the resulting
BigFloat - See Also:
-
pow
-
pow
-
pow
-
root
Returns theBigFloatthat is theyth root ofthis.If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision.- Parameters:
y- the value of the root- Returns:
- the resulting
BigFloat - See Also:
-
root
- Parameters:
y- the value of the root- Returns:
- the resulting
BigFloat - See Also:
-
root
-
root
-
root
-
hashCode
-
equals
-
signum
public int signum()Returns the signum function of thisBigFloat.- Returns:
- -1, 0, or 1 as the value of this
BigDecimalis negative, zero, or positive.
-
isNegative
public boolean isNegative()Returns whether thisBigFloatis negative.- Returns:
trueif negative,falseif 0 or positive
-
isZero
public boolean isZero()Returns whether thisBigFloatis 0.- Returns:
trueif 0,falseif negative or positive
-
isPositive
public boolean isPositive()Returns whether thisBigFloatis positive.- Returns:
trueif positive,falseif 0 or negative
-
compareTo
- Specified by:
compareToin interfaceComparable<BigFloat>
-
isEqual
-
isLessThan
-
isGreaterThan
Returns whetherthisvalue is mathematically greater than to theothervalue.- Parameters:
other- the otherBigFloatto compare with- Returns:
truethisvalue is mathematically greater than to theothervalue (equivalent tothis.compareTo(other) > 0- See Also:
-
isLessThanOrEqual
Returns whetherthisvalue is mathematically less than or equal to theothervalue.- Parameters:
other- the otherBigFloatto compare with- Returns:
truethisvalue is mathematically less than or equal to theothervalue (equivalent tothis.compareTo(other) <= 0- See Also:
-
isGreaterThanOrEqual
Returns whetherthisvalue is mathematically greater than or equal to theothervalue.- Parameters:
other- the otherBigFloatto compare with- Returns:
truethisvalue is mathematically greater than or equal to theothervalue (equivalent tothis.compareTo(other) >= 0- See Also:
-
isIntValue
public boolean isIntValue()Returns whetherthisvalue can be represented asint.- Returns:
trueif the value can be represented asintvalue- See Also:
-
isDoubleValue
public boolean isDoubleValue()- Returns:
trueif the value can be represented asdoublevalue- See Also:
-
getMantissa
Returns the mantissa ofthisvalue written as mantissa * 10exponent.The mantissa is defined as having exactly 1 digit before the decimal point.
- Returns:
- the mantissa
- See Also:
-
getExponent
Returns the exponent ofthisvalue written as mantissa * 10exponent.The mantissa is defined as having exactly 1 digit before the decimal point.
- Returns:
- the exponent
- See Also:
-
getIntegralPart
Returns the integral part ofthisvalue (left of the decimal point).- Returns:
- the integral part
- See Also:
-
getFractionalPart
Returns the fractional part ofthisvalue (right of the decimal point).- Returns:
- the fractional part
- See Also:
-
getContext
Returns theBigFloat.Contextofthisvalue.- Returns:
- the
BigFloat.Context
-
toBigDecimal
Returnsthisvalue as aBigDecimalvalue.- Returns:
- the
BigDecimalvalue
-
toDouble
public double toDouble()Returnsthisvalue as adoublevalue.- Returns:
- the
doublevalue - See Also:
-
toLong
public long toLong()Returnsthisvalue as alongvalue.- Returns:
- the
longvalue - See Also:
-
toInt
public int toInt()Returnsthisvalue as aintvalue.- Returns:
- the
intvalue - See Also:
-
toString
-
isSpecial
protected boolean isSpecial() -
type
return special type of a value- Returns:
BigFloat.SpecialBigFloat.Type
-
isNaN
public boolean isNaN() -
isInfinity
public boolean isInfinity() -
negate
-
abs
-
max
-
max
-
min
-
min
-
logSpecial
-
log
-
log2
-
log10
-
exp
-
sqrt
-
pow
Returns theBigFloatthat ispow(x, y).If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision. -
root
Returns theBigFloatthat isroot(x, y).If the two values do not have the same
BigFloat.Context, the result will contain theBigFloat.Contextwith the larger precision. -
sin
-
cos
-
tan
-
cot
-
asin
-
acos
-
isRangeAbs1
- Parameters:
x- a bigfloat- Returns:
- if abs(x) invalid input: '<'= 1
-
atan
-
acot
-
sinh
-
cosh
-
tanh
-
coth
-
asinh
-
acosh
-
atanh
-
acoth
-
isBetween
-
max
-