Class FloatingPoint
- All Implemented Interfaces:
Serializable
,Comparable<FloatingPoint>
,javolution.lang.Immutable
,javolution.lang.Realtime
,javolution.lang.ValueType
,javolution.xml.XMLSerializable
,Field<FloatingPoint>
,GroupAdditive<FloatingPoint>
,GroupMultiplicative<FloatingPoint>
,Ring<FloatingPoint>
,Structure<FloatingPoint>
This class represents a floating point number of arbitrary precision.
A floating point number consists of a significand
and a decimal exponent
:
(significand · 10exponent
).
Unlike Real
numbers, no calculation error is performed on
floating point instances but the number of digits used during
calculations can be specified (see setDigits(int)
). The
largest the number of digits, the smallest the numeric error.
For example:[code]
FloatingPoint two = FloatingPoint.valueOf(2);
FloatingPoint.setDigits(30); // 30 digits calculations.
System.out.println(two.sqrt());
> 0.141421356237309504880168872420E1
[/code]
Instances of this class are immutable and can be used to find
accurate solutions to linear equations with the Matrix
class.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final FloatingPoint
The Not-a-Number instance (unique).static final FloatingPoint
The floating point instance representing the multiplicative identity.static final FloatingPoint
The floating point instance representing the additive identity. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Returns the absolute value of this floating point number.int
compareTo
(FloatingPoint that) Compares two floating point number numerically.copy()
Returns a copy of this numberallocated
by the calling thread (possibly on the stack).divide
(FloatingPoint that) Returns this floating point number divided by the one specified.double
Returns the value of this floating point number as adouble
.boolean
Compares this floating point number against the specified object.static int
Returns thelocal
number of digits used during calculations (default 20 digits).int
Returns the decimal exponent.Returns the significand value.int
hashCode()
Returns the hash code for this floating point number.inverse()
Returns the inverse of this floating point number.boolean
isLargerThan
(FloatingPoint that) Compares the absolute value of two FloatingPoint numbers.boolean
isNaN()
Indicates if this floating point is Not-a-Number.boolean
Indicates if this rational number is less than zero.boolean
Indicates if this floating point number is greater than zero.boolean
isZero()
Indicates if this floating point number is equal to zero.long
Returns the value of this floating point number as along
.minus
(FloatingPoint that) Returns the difference between this FloatingPoint number and the one specified.opposite()
Returns the opposite of this floating point number.plus
(FloatingPoint that) Returns the sum of this floating point number with the one specified.round()
Returns the closest integer to this floating point number.static void
setDigits
(int digits) Sets thelocal
number of digits to be used during calculations.sqrt()
Returns the square root of this floating point number.times
(long multiplier) Returns the product of this floating point number with the specifiedlong
multiplier.times
(FloatingPoint that) Returns the product of this floating point number with the one specified.javolution.text.Text
toText()
Returns the decimal text representation of this number.static FloatingPoint
valueOf
(double doubleValue) Returns the floating point number for the specifieddouble
value (convenience method).static FloatingPoint
valueOf
(long longValue) Returns the floating point number for the specifiedlong
value (convenience method).static FloatingPoint
valueOf
(long significand, int exponent) Returns the floating point number for the specifiedlong
significand and power of two exponent (convenience method).static FloatingPoint
valueOf
(CharSequence chars) Returns the floating point number for the specified character sequence.static FloatingPoint
valueOf
(LargeInteger significand, int exponent) Returns the floating point number for the specifiedLargeInteger
significand and power of two exponent.Methods inherited from class org.jscience.mathematics.number.Number
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
-
Field Details
-
ZERO
The floating point instance representing the additive identity. -
ONE
The floating point instance representing the multiplicative identity. -
NaN
The Not-a-Number instance (unique).
-
-
Method Details
-
valueOf
Returns the floating point number for the specifiedLargeInteger
significand and power of two exponent.- Parameters:
significand
- the significand value.exponent
- the power of two exponent.- Returns:
(significand · 2exponent
-
valueOf
Returns the floating point number for the specifiedlong
significand and power of two exponent (convenience method).- Parameters:
significand
- the significand value.exponent
- the power of two exponent.- Returns:
(significand · 2exponent
-
valueOf
Returns the floating point number for the specifiedlong
value (convenience method).- Parameters:
longValue
- thelong
value.- Returns:
FloatingPoint.valueOf(longValue, 0)
-
valueOf
Returns the floating point number for the specifieddouble
value (convenience method).- Parameters:
doubleValue
- thedouble
value.- Returns:
FloatingPoint.valueOf(longValue, 0)
-
valueOf
Returns the floating point number for the specified character sequence. The number of digits- Parameters:
chars
- the character sequence.- Returns:
- the corresponding FloatingPoint number.
-
getDigits
public static int getDigits()Returns thelocal
number of digits used during calculations (default 20 digits).- Returns:
- the number of digits.
-
setDigits
public static void setDigits(int digits) Sets thelocal
number of digits to be used during calculations.- Parameters:
digits
- the number of digits.- Throws:
IllegalArgumentException
- ifdigits <= 0
-
getSignificand
Returns the significand value.- Returns:
- this floating point significand.
-
getExponent
public int getExponent()Returns the decimal exponent.- Returns:
- this floating point decimal exponent.
-
round
Returns the closest integer to this floating point number.- Returns:
- this floating point rounded to the nearest integer.
-
opposite
Returns the opposite of this floating point number.- Specified by:
opposite
in interfaceGroupAdditive<FloatingPoint>
- Returns:
-this
.
-
plus
Returns the sum of this floating point number with the one specified.- Specified by:
plus
in interfaceGroupAdditive<FloatingPoint>
- Parameters:
that
- the floating point number to be added.- Returns:
this + that
.
-
minus
Returns the difference between this FloatingPoint number and the one specified.- Overrides:
minus
in classNumber<FloatingPoint>
- Parameters:
that
- the floating point number to be subtracted.- Returns:
this - that
.
-
times
Returns the product of this floating point number with the specifiedlong
multiplier.- Parameters:
multiplier
- thelong
multiplier.- Returns:
this · multiplier
.
-
times
Returns the product of this floating point number with the one specified.- Specified by:
times
in interfaceGroupMultiplicative<FloatingPoint>
- Specified by:
times
in interfaceRing<FloatingPoint>
- Parameters:
that
- the floating point number multiplier.- Returns:
this · that
.
-
inverse
Returns the inverse of this floating point number.- Specified by:
inverse
in interfaceGroupMultiplicative<FloatingPoint>
- Returns:
1 / this
.- Throws:
ArithmeticException
- ifdividend.isZero()
-
divide
Returns this floating point number divided by the one specified.- Parameters:
that
- the FloatingPoint number divisor.- Returns:
this / that
.- Throws:
ArithmeticException
- ifthat.equals(ZERO)
-
abs
Returns the absolute value of this floating point number.- Returns:
|this|
.
-
sqrt
Returns the square root of this floating point number.- Returns:
- the positive square root of this floating point number.
-
isZero
public boolean isZero()Indicates if this floating point number is equal to zero.- Returns:
this == 0
-
isPositive
public boolean isPositive()Indicates if this floating point number is greater than zero.- Returns:
this > 0
-
isNegative
public boolean isNegative()Indicates if this rational number is less than zero.- Returns:
this < 0
-
isNaN
public boolean isNaN()Indicates if this floating point is Not-a-Number.- Returns:
true
if this number has unbounded value;false
otherwise.
-
isLargerThan
Compares the absolute value of two FloatingPoint numbers.- Specified by:
isLargerThan
in classNumber<FloatingPoint>
- Parameters:
that
- the FloatingPoint number to be compared with.- Returns:
|this| > |that|
-
toText
public javolution.text.Text toText()Returns the decimal text representation of this number.- Specified by:
toText
in interfacejavolution.lang.Realtime
- Specified by:
toText
in classNumber<FloatingPoint>
- Returns:
- the text representation of this number.
-
equals
Compares this floating point number against the specified object.- Specified by:
equals
in classNumber<FloatingPoint>
- Parameters:
that
- the object to compare with.- Returns:
true
if the objects are the same;false
otherwise.
-
hashCode
public int hashCode()Returns the hash code for this floating point number.- Specified by:
hashCode
in classNumber<FloatingPoint>
- Returns:
- the hash code value.
-
longValue
public long longValue()Returns the value of this floating point number as along
.- Specified by:
longValue
in classNumber<FloatingPoint>
- Returns:
- the numeric value represented by this floating point
after conversion to type
long
.
-
doubleValue
public double doubleValue()Returns the value of this floating point number as adouble
.- Specified by:
doubleValue
in classNumber<FloatingPoint>
- Returns:
- the numeric value represented by this FloatingPoint after conversion
to type
double
.
-
compareTo
Compares two floating point number numerically.- Specified by:
compareTo
in interfaceComparable<FloatingPoint>
- Specified by:
compareTo
in classNumber<FloatingPoint>
- Parameters:
that
- the floating point number to compare with.- Returns:
- -1, 0 or 1 as this FloatingPoint number is numerically less than,
equal to, or greater than
that
.
-
copy
Description copied from class:Number
Returns a copy of this numberallocated
by the calling thread (possibly on the stack).- Specified by:
copy
in interfacejavolution.lang.ValueType
- Specified by:
copy
in classNumber<FloatingPoint>
- Returns:
- an identical and independant copy of this number.
-