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 twoBigFloat
values 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 class
Manages theMathContext
and provides factory methods forBigFloat
values.private static final class
this class handle unrepresentable value in floating-point arithmetic -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BigFloat.Context
static final BigFloat
Represents a value that is not a number.static final BigFloat
Represents the positive infinity.static final BigFloat
Represents the positive infinity.private static final long
private final BigDecimal
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BigFloat
Returns theBigFloat
that is theabs(this)
(absolute value).static BigFloat
Returns theBigFloat
that isacos(x)
.static BigFloat
Returns theBigFloat
that isacosh(x)
.static BigFloat
Returns theBigFloat
that isacot(x)
.static BigFloat
Returns theBigFloat
that isacoth(x)
.add
(double x) Returns theBigFloat
that isthis + x
.add
(int x) Returns theBigFloat
that isthis + x
.add
(long x) Returns theBigFloat
that isthis + x
.Returns theBigFloat
that isthis + x
.add
(BigDecimal x) Returns theBigFloat
that isthis + x
.static BigFloat
Returns theBigFloat
that isasin(x)
.static BigFloat
Returns theBigFloat
that isasinh(x)
.static BigFloat
Returns theBigFloat
that isatan(x)
.static BigFloat
Returns theBigFloat
that isatanh(x)
.int
static BigFloat.Context
context
(int precision) Creates aBigFloat.Context
with the specified precision andRoundingMode.HALF_UP
rounding.static BigFloat.Context
context
(MathContext mathContext) Creates aBigFloat.Context
with the specifiedMathContext
.static BigFloat
Returns theBigFloat
that iscos(x)
.static BigFloat
Returns theBigFloat
that iscosh(x)
.static BigFloat
Returns theBigFloat
that iscot(x)
.static BigFloat
Returns theBigFloat
that iscoth(x)
.divide
(double x) Returns theBigFloat
that isthis / x
.divide
(int x) Returns theBigFloat
that isthis / x
.divide
(long x) Returns theBigFloat
that isthis / x
.Returns theBigFloat
that isthis / x
.divide
(BigDecimal x) Returns theBigFloat
that isthis / x
.boolean
static BigFloat
Returns theBigFloat
that isexp(x)
.Returns theBigFloat.Context
ofthis
value.Returns the exponent ofthis
value written as mantissa * 10exponent.Returns the fractional part ofthis
value (right of the decimal point).Returns the integral part ofthis
value (left of the decimal point).Returns the mantissa ofthis
value written as mantissa * 10exponent.int
hashCode()
static boolean
boolean
boolean
Returns whetherthis
value is mathematically equal to theother
value.boolean
isGreaterThan
(BigFloat other) Returns whetherthis
value is mathematically greater than to theother
value.boolean
isGreaterThanOrEqual
(BigFloat other) Returns whetherthis
value is mathematically greater than or equal to theother
value.boolean
boolean
Returns whetherthis
value can be represented asint
.boolean
isLessThan
(BigFloat other) Returns whetherthis
value is mathematically less than to theother
value.boolean
isLessThanOrEqual
(BigFloat other) Returns whetherthis
value is mathematically less than or equal to theother
value.boolean
isNaN()
boolean
Returns whether thisBigFloat
is negative.boolean
Returns whether thisBigFloat
is positive.private static boolean
protected boolean
boolean
isZero()
Returns whether thisBigFloat
is 0.static BigFloat
Returns theBigFloat
that islog(x)
.static BigFloat
Returns theBigFloat
that islog10(x)
.static BigFloat
Returns theBigFloat
that islog2(x)
.private static BigFloat
logSpecial
(BigFloat val) private static BigFloat.Context
max
(BigFloat.Context left, BigFloat.Context right) static BigFloat
Returns the the maximum of twoBigFloat
values.static BigFloat
Returns the the maximum of nBigFloat
values.static BigFloat
Returns the the minimum of twoBigFloat
values.static BigFloat
Returns the the minimum of nBigFloat
values.multiply
(double x) Returns theBigFloat
that isthis * x
.multiply
(int x) Returns theBigFloat
that isthis * x
.multiply
(long x) Returns theBigFloat
that isthis * x
.Returns theBigFloat
that isthis * x
.Returns theBigFloat
that isthis * x
.static BigFloat
Returns theBigFloat
that is- this
.pow
(double y) pow
(int y) pow
(long y) static BigFloat
Returns theBigFloat
that 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 BigFloat
Returns theBigFloat
that isroot(x, y)
.root
(BigDecimal y) int
signum()
Returns the signum function of thisBigFloat
.static BigFloat
Returns theBigFloat
that issin(x)
.static BigFloat
Returns theBigFloat
that issinh(x)
.static BigFloat
Returns theBigFloat
that issqrt(x)
.subtract
(double x) Returns theBigFloat
that isthis - x
.subtract
(int x) Returns theBigFloat
that isthis - x
.subtract
(long x) Returns theBigFloat
that isthis - x
.Returns theBigFloat
that isthis - x
.Returns theBigFloat
that isthis - x
.static BigFloat
Returns theBigFloat
that istan(x)
.static BigFloat
Returns theBigFloat
that istanh(x)
.Returnsthis
value as aBigDecimal
value.double
toDouble()
Returnsthis
value as adouble
value.int
toInt()
Returnsthis
value as aint
value.long
toLong()
Returnsthis
value as along
value.toString()
protected BigFloat.SpecialBigFloat.Type
type()
return special type of a value
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
NaN
Represents a value that is not a number.- See Also:
-
POSITIVE_INFINITY
Represents the positive infinity.- See Also:
-
NEGATIVE_INFINITY
Represents the positive infinity.- See Also:
-
value
-
context
-
-
Constructor Details
-
BigFloat
-
-
Method Details
-
context
Creates aBigFloat.Context
with the specified precision andRoundingMode.HALF_UP
rounding.- Parameters:
precision
- the precision- Returns:
- the
BigFloat.Context
-
context
Creates aBigFloat.Context
with the specifiedMathContext
.- Parameters:
mathContext
- theMathContext
- Returns:
- the
BigFloat.Context
-
add
Returns theBigFloat
that isthis + x
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with the larger precision.- Parameters:
x
- the value to add- Returns:
- the resulting
BigFloat
- See Also:
-
add
Returns theBigFloat
that isthis + x
.- Parameters:
x
- the value to add- Returns:
- the resulting
BigFloat
- See Also:
-
add
Returns theBigFloat
that isthis + x
.- Parameters:
x
- the value to add- Returns:
- the resulting
BigFloat
- See Also:
-
add
Returns theBigFloat
that isthis + x
.- Parameters:
x
- the value to add- Returns:
- the resulting
BigFloat
- See Also:
-
add
Returns theBigFloat
that isthis + x
.- Parameters:
x
- the value to add- Returns:
- the resulting
BigFloat
- See Also:
-
subtract
Returns theBigFloat
that isthis - x
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with the larger precision.- Parameters:
x
- the value to subtract- Returns:
- the resulting
BigFloat
- See Also:
-
subtract
Returns theBigFloat
that isthis - x
.- Parameters:
x
- the value to subtract- Returns:
- the resulting
BigFloat
- See Also:
-
subtract
Returns theBigFloat
that isthis - x
.- Parameters:
x
- the value to subtract- Returns:
- the resulting
BigFloat
- See Also:
-
subtract
Returns theBigFloat
that isthis - x
.- Parameters:
x
- the value to subtract- Returns:
- the resulting
BigFloat
- See Also:
-
subtract
Returns theBigFloat
that isthis - x
.- Parameters:
x
- the value to subtract- Returns:
- the resulting
BigFloat
- See Also:
-
multiply
Returns theBigFloat
that isthis * x
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with the larger precision.- Parameters:
x
- the value to multiply- Returns:
- the resulting
BigFloat
- See Also:
-
multiply
Returns theBigFloat
that isthis * x
.- Parameters:
x
- the value to multiply- Returns:
- the resulting
BigFloat
- See Also:
-
multiply
Returns theBigFloat
that isthis * x
.- Parameters:
x
- the value to multiply- Returns:
- the resulting
BigFloat
- See Also:
-
multiply
Returns theBigFloat
that isthis * x
.- Parameters:
x
- the value to multiply- Returns:
- the resulting
BigFloat
- See Also:
-
multiply
Returns theBigFloat
that isthis * x
.- Parameters:
x
- the value to multiply- Returns:
- the resulting
BigFloat
- See Also:
-
divide
Returns theBigFloat
that isthis / x
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with the larger precision.- Parameters:
x
- the value to divide with- Returns:
- the resulting
BigFloat
- See Also:
-
divide
Returns theBigFloat
that isthis / x
.- Parameters:
x
- the value to divide with- Returns:
- the resulting
BigFloat
- See Also:
-
divide
Returns theBigFloat
that isthis / x
.- Parameters:
x
- the value to divide with- Returns:
- the resulting
BigFloat
- See Also:
-
divide
Returns theBigFloat
that isthis / x
.- Parameters:
x
- the value to divide with- Returns:
- the resulting
BigFloat
- See Also:
-
divide
Returns theBigFloat
that isthis / x
.- Parameters:
x
- the value to divide with- Returns:
- the resulting
BigFloat
- See Also:
-
remainder
Returns theBigFloat
that is the remainder when dividingthis
byx
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with 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
- 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
- Parameters:
x
- the value to divide with- Returns:
- the resulting
BigFloat
- See Also:
-
pow
Returns theBigFloat
that isthis
to the power ofy
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with 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
- 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
- Parameters:
y
- the value of the power- Returns:
- the resulting
BigFloat
- See Also:
-
root
Returns theBigFloat
that is they
th root ofthis
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with 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
- 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
- Parameters:
y
- the value of the root- Returns:
- the resulting
BigFloat
- See Also:
-
hashCode
public int hashCode() -
equals
-
signum
public int signum()Returns the signum function of thisBigFloat
.- Returns:
- -1, 0, or 1 as the value of this
BigDecimal
is negative, zero, or positive.
-
isNegative
public boolean isNegative()Returns whether thisBigFloat
is negative.- Returns:
true
if negative,false
if 0 or positive
-
isZero
public boolean isZero()Returns whether thisBigFloat
is 0.- Returns:
true
if 0,false
if negative or positive
-
isPositive
public boolean isPositive()Returns whether thisBigFloat
is positive.- Returns:
true
if positive,false
if 0 or negative
-
compareTo
- Specified by:
compareTo
in interfaceComparable<BigFloat>
-
isEqual
Returns whetherthis
value is mathematically equal to theother
value.- Parameters:
other
- the otherBigFloat
to compare with- Returns:
true
if both values are mathematically equal (equivalent tothis.compareTo(other) == 0
- See Also:
-
isLessThan
Returns whetherthis
value is mathematically less than to theother
value.- Parameters:
other
- the otherBigFloat
to compare with- Returns:
true
this
value is mathematically less than to theother
value (equivalent tothis.compareTo(other) < 0
- See Also:
-
isGreaterThan
Returns whetherthis
value is mathematically greater than to theother
value.- Parameters:
other
- the otherBigFloat
to compare with- Returns:
true
this
value is mathematically greater than to theother
value (equivalent tothis.compareTo(other) > 0
- See Also:
-
isLessThanOrEqual
Returns whetherthis
value is mathematically less than or equal to theother
value.- Parameters:
other
- the otherBigFloat
to compare with- Returns:
true
this
value is mathematically less than or equal to theother
value (equivalent tothis.compareTo(other) <= 0
- See Also:
-
isGreaterThanOrEqual
Returns whetherthis
value is mathematically greater than or equal to theother
value.- Parameters:
other
- the otherBigFloat
to compare with- Returns:
true
this
value is mathematically greater than or equal to theother
value (equivalent tothis.compareTo(other) >= 0
- See Also:
-
isIntValue
public boolean isIntValue()Returns whetherthis
value can be represented asint
.- Returns:
true
if the value can be represented asint
value- See Also:
-
isDoubleValue
public boolean isDoubleValue()- Returns:
true
if the value can be represented asdouble
value- See Also:
-
getMantissa
Returns the mantissa ofthis
value 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 ofthis
value 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 ofthis
value (left of the decimal point).- Returns:
- the integral part
- See Also:
-
getFractionalPart
Returns the fractional part ofthis
value (right of the decimal point).- Returns:
- the fractional part
- See Also:
-
getContext
Returns theBigFloat.Context
ofthis
value.- Returns:
- the
BigFloat.Context
-
toBigDecimal
Returnsthis
value as aBigDecimal
value.- Returns:
- the
BigDecimal
value
-
toDouble
public double toDouble()Returnsthis
value as adouble
value.- Returns:
- the
double
value - See Also:
-
toLong
public long toLong()Returnsthis
value as along
value.- Returns:
- the
long
value - See Also:
-
toInt
public int toInt()Returnsthis
value as aint
value.- Returns:
- the
int
value - 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
Returns theBigFloat
that is- this
.- Parameters:
x
- the value to negate- Returns:
- the resulting
BigFloat
- See Also:
-
abs
Returns theBigFloat
that is theabs(this)
(absolute value).- Parameters:
x
- the value to make absolute- Returns:
- the resulting
BigFloat
- See Also:
-
max
Returns the the maximum of twoBigFloat
values. -
max
Returns the the maximum of nBigFloat
values. -
min
Returns the the minimum of twoBigFloat
values. -
min
Returns the the minimum of nBigFloat
values. -
logSpecial
-
log
Returns theBigFloat
that islog(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
log2
Returns theBigFloat
that islog2(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
log10
Returns theBigFloat
that islog10(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
exp
Returns theBigFloat
that isexp(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
sqrt
Returns theBigFloat
that issqrt(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
pow
Returns theBigFloat
that ispow(x, y)
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with the larger precision. -
root
Returns theBigFloat
that isroot(x, y)
.If the two values do not have the same
BigFloat.Context
, the result will contain theBigFloat.Context
with the larger precision. -
sin
Returns theBigFloat
that issin(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
cos
Returns theBigFloat
that iscos(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
tan
Returns theBigFloat
that istan(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
cot
Returns theBigFloat
that iscot(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
asin
Returns theBigFloat
that isasin(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
acos
Returns theBigFloat
that isacos(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
isRangeAbs1
- Parameters:
x
- a bigfloat- Returns:
- if abs(x) invalid input: '<'= 1
-
atan
Returns theBigFloat
that isatan(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
acot
Returns theBigFloat
that isacot(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
sinh
Returns theBigFloat
that issinh(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
cosh
Returns theBigFloat
that iscosh(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
tanh
Returns theBigFloat
that istanh(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
coth
Returns theBigFloat
that iscoth(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
asinh
Returns theBigFloat
that isasinh(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
acosh
Returns theBigFloat
that isacosh(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
atanh
Returns theBigFloat
that isatanh(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
acoth
Returns theBigFloat
that isacoth(x)
.- Parameters:
x
- the value- Returns:
- the resulting
BigFloat
- See Also:
-
isBetween
-
max
-