public final class Real extends Number<Real> implements Field<Real>
This class represents a real number of arbitrary precision with
known/guaranteed uncertainty. A real number consists of a
significand
, a maximum error
(on the significand value) and a decimal exponent
:
((significand ± error) · 10exponent
).
Reals number can be exact
(e.g. integer values
scaled by a power of ten). Exactness is maintained for
Ring
operations
(e.g. addition, multiplication), but typically lost when a
multiplicative inverse
is calculated. The minimum
precision used for exact numbers is set by
setExactPrecision(int)
(context local
setting, default
19
digits).
The actual precision
and accuracy
of any real number is available and guaranteed
(the true/exact value is always within the precision/accuracy range).
Operations on instances of this class are quite fast as information substantially below the precision level (aka noise) is not processed/stored. There is no limit on a real precision but precision degenerates (due to numeric errors) and calculations accelerate as more and more operations are performed.
Instances of this class can be utilized to find approximate
solutions to linear equations using the
Matrix
class for which
high-precision reals is often required, the primitive type
double
being not accurate enough to resolve equations
when the matrix's size exceeds 100x100. Furthermore, even for small
matrices the "qualified" result is indicative of possible system
singularities.
Modifier and Type | Field and Description |
---|---|
static Real |
NaN
Holds a Not-a-Number instance (infinite error).
|
static Real |
ONE
Holds the exact ONE instance.
|
static Real |
ZERO
Holds the exact ZERO instance.
|
Modifier and Type | Method and Description |
---|---|
Real |
abs()
Returns the absolute value of this real number.
|
boolean |
approximates(Real that)
Indicates if this real approximates the one specified.
|
int |
compareTo(Real that)
Compares two real numbers numerically.
|
Real |
copy()
Returns a copy of this number
allocated
by the calling thread (possibly on the stack). |
Real |
divide(long divisor)
Returns this real number divided by the specified
int
divisor. |
Real |
divide(Real that)
Returns this real number divided by the one specified.
|
double |
doubleValue()
Returns the value of this real number as a
double . |
boolean |
equals(java.lang.Object that)
Compares this real number against the specified object.
|
int |
getAccuracy()
Returns the number of decimal digits guaranteed exact which appear to
the right of the decimal point (absolute error).
|
int |
getError()
Returns the maximum error (positive) on this real significand.
|
static int |
getExactPrecision()
Returns the
local minimum
precision (number of exact digits) when exact numbers have to be
approximated. |
int |
getExponent()
Returns the exponent of the power of 10 multiplier.
|
int |
getPrecision()
Returns the total number of decimal digits guaranteed exact
(relative error).
|
LargeInteger |
getSignificand()
Returns this real
significand value.
|
int |
hashCode()
Returns the hash code for this real number.
|
Real |
inverse()
Returns the reciprocal (or inverse) of this real number.
|
boolean |
isExact()
Indicates if this real number is exact (
). |
boolean |
isLargerThan(Real that)
Compares the absolute value of two real numbers.
|
boolean |
isNaN()
Indicates if this real is Not-a-Number (unbounded value interval).
|
boolean |
isNegative()
Indicates if this real is less than zero.
|
boolean |
isPositive()
Indicates if this real is greater than zero.
|
long |
longValue()
Returns the value of this real number as a
long . |
Real |
minus(Real that)
Returns the difference between this real number and the one
specified.
|
Real |
opposite()
Returns the negation of this real number.
|
Real |
plus(Real that)
Returns the sum of this real number with the one specified.
|
LargeInteger |
round()
Returns the closest integer value to this rational number.
|
static void |
setExactPrecision(int precision)
Sets the
local minimum precision
(number of exact digits) when exact numbers have to be approximated. |
Real |
sqrt()
Returns the square root of this real number, the more accurate is this
real number, the more accurate the square root.
|
Real |
times(long multiplier)
Returns the product of this real number with the specified
long multiplier. |
Real |
times(Real that)
Returns the product of this real number with the one specified.
|
javolution.text.Text |
toText()
Returns the decimal text representation of this number.
|
static Real |
valueOf(java.lang.CharSequence chars)
Returns the real for the specified character sequence.
|
static Real |
valueOf(double doubleValue)
Returns the real number (inexact except for
0.0 )
corresponding to the specified double value. |
static Real |
valueOf(LargeInteger significand,
int error,
int exponent)
Returns a real having the specified significand, error and exponent values.
|
static Real |
valueOf(long longValue)
Returns the exact real number corresponding to the specified
long value (convenience method). |
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
public static int getExactPrecision()
local
minimum
precision (number of exact digits) when exact numbers have to be
approximated.exact
real numbers.public static void setExactPrecision(int precision)
local
minimum precision
(number of exact digits) when exact numbers have to be approximated.precision
- the minimum number of digits assumed exact for
exact
numbers.public static Real valueOf(LargeInteger significand, int error, int exponent)
0
, the real is assumed exact.
For example:[code]
// x = 0.0 ± 0.01
Real x = Real.valueOf(LargeInteger.ZERO, 1, -2);
// y = -12.3 exact
Real y = Real.valueOf(LargeInteger.valueOf(-123), 0, -1);
[/code]significand
- this real significand.error
- the maximum error on the significand.exponent
- the decimal exponent.(significand ± error)·10exponent)
java.lang.IllegalArgumentException
- if error < 0
public static Real valueOf(double doubleValue)
0.0
)
corresponding to the specified double
value.
The error is derived from the inexact representation of
double
values intrinsic to the 64 bits IEEE 754 format.doubleValue
- the double
value to convert.public static Real valueOf(long longValue)
long
value (convenience method).longValue
- the exact long value.Real.valueOf(LargeInteger.valueOf(longValue), 0, 0)
public static Real valueOf(java.lang.CharSequence chars) throws java.lang.NumberFormatException
±
symbol),
the real is supposed exact. Example of valid character sequences:
chars
- the character sequence.java.lang.NumberFormatException
- if the character sequence does not contain
a parsable real.public LargeInteger getSignificand()
public int getError()
public int getExponent()
public boolean isExact()
error
== 0
).getError() == 0
public int getAccuracy()
public final int getPrecision()
public boolean isPositive()
this > 0
public boolean isNegative()
this < 0
public boolean isNaN()
true
if this number has unbounded value interval;
false
otherwise.public boolean approximates(Real that)
Note: This method returns true
if this
or
that
isNaN()
(basically Not-A-Number
approximates anything).
that
- the real to compare with.this ≈ that
public LargeInteger round()
java.lang.ArithmeticException
- if this.isNaN()
public Real opposite()
opposite
in interface GroupAdditive<Real>
-this
.public Real plus(Real that)
plus
in interface GroupAdditive<Real>
that
- the real to be added.this + that
.public Real minus(Real that)
public Real times(long multiplier)
long
multiplier.multiplier
- the long
multiplier.this · multiplier
.public Real divide(long divisor)
int
divisor.divisor
- the int
divisor.this / divisor
public Real divide(Real that)
that
- the real divisor.this / that
.java.lang.ArithmeticException
- if that.equals(ZERO)
public Real inverse()
inverse
in interface GroupMultiplicative<Real>
1 / this
.public boolean isLargerThan(Real that)
isLargerThan
in class Number<Real>
that
- the real number to be compared with.|this| > |that|
public Real sqrt()
public javolution.text.Text toText()
public boolean equals(java.lang.Object that)
Note: This method returns true
if this
or
that
is Not-A-Number
, even though
Double.NaN == Double.NaN
has the value
false
.
public int hashCode()
public long longValue()
long
.public double doubleValue()
double
.doubleValue
in class Number<Real>
double
.public int compareTo(Real that)
compareTo
in interface java.lang.Comparable<Real>
compareTo
in class Number<Real>
that
- the real to compare with.that
.java.lang.ClassCastException
- that
is not a Real
.