Class Real
- java.lang.Object
-
- java.lang.Number
-
- org.jscience.mathematics.number.Number<Real>
-
- org.jscience.mathematics.number.Real
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<Real>,javolution.lang.Immutable,javolution.lang.Realtime,javolution.lang.ValueType,javolution.xml.XMLSerializable,Field<Real>,GroupAdditive<Real>,GroupMultiplicative<Real>,Ring<Real>,Structure<Real>
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 maximumerror(on the significand value) and a decimalexponent: ((significand ± error) · 10exponent).Reals number can be
exact(e.g. integer values scaled by a power of ten). Exactness is maintained forRingoperations (e.g. addition, multiplication), but typically lost when a multiplicativeinverseis calculated. The minimum precision used for exact numbers is set bysetExactPrecision(int)(context localsetting, default19digits).The actual
precisionandaccuracyof 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
Matrixclass for which high-precision reals is often required, the primitive typedoublebeing 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.- Version:
- 3.3, January 8, 2006
- Author:
- Jean-Marie Dautelle
- See Also:
- Wikipedia: Real number, Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Realabs()Returns the absolute value of this real number.booleanapproximates(Real that)Indicates if this real approximates the one specified.intcompareTo(Real that)Compares two real numbers numerically.Realcopy()Returns a copy of this numberallocatedby the calling thread (possibly on the stack).Realdivide(long divisor)Returns this real number divided by the specifiedintdivisor.Realdivide(Real that)Returns this real number divided by the one specified.doubledoubleValue()Returns the value of this real number as adouble.booleanequals(java.lang.Object that)Compares this real number against the specified object.intgetAccuracy()Returns the number of decimal digits guaranteed exact which appear to the right of the decimal point (absolute error).intgetError()Returns the maximum error (positive) on this real significand.static intgetExactPrecision()Returns thelocalminimum precision (number of exact digits) when exact numbers have to be approximated.intgetExponent()Returns the exponent of the power of 10 multiplier.intgetPrecision()Returns the total number of decimal digits guaranteed exact (relative error).LargeIntegergetSignificand()Returns this real significand value.inthashCode()Returns the hash code for this real number.Realinverse()Returns the reciprocal (or inverse) of this real number.booleanisExact()Indicates if this real number is exact ().error== 0booleanisLargerThan(Real that)Compares the absolute value of two real numbers.booleanisNaN()Indicates if this real is Not-a-Number (unbounded value interval).booleanisNegative()Indicates if this real is less than zero.booleanisPositive()Indicates if this real is greater than zero.longlongValue()Returns the value of this real number as along.Realminus(Real that)Returns the difference between this real number and the one specified.Realopposite()Returns the negation of this real number.Realplus(Real that)Returns the sum of this real number with the one specified.LargeIntegerround()Returns the closest integer value to this rational number.static voidsetExactPrecision(int precision)Sets thelocalminimum precision (number of exact digits) when exact numbers have to be approximated.Realsqrt()Returns the square root of this real number, the more accurate is this real number, the more accurate the square root.Realtimes(long multiplier)Returns the product of this real number with the specifiedlongmultiplier.Realtimes(Real that)Returns the product of this real number with the one specified.javolution.text.TexttoText()Returns the decimal text representation of this number.static RealvalueOf(double doubleValue)Returns the real number (inexact except for0.0) corresponding to the specifieddoublevalue.static RealvalueOf(long longValue)Returns the exact real number corresponding to the specifiedlongvalue (convenience method).static RealvalueOf(java.lang.CharSequence chars)Returns the real for the specified character sequence.static RealvalueOf(LargeInteger significand, int error, int exponent)Returns a real having the specified significand, error and exponent values.-
Methods inherited from class org.jscience.mathematics.number.Number
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
-
-
-
-
Method Detail
-
getExactPrecision
public static int getExactPrecision()
Returns thelocalminimum precision (number of exact digits) when exact numbers have to be approximated.- Returns:
- the minimum number of digits assumed exact for
exactreal numbers.
-
setExactPrecision
public static void setExactPrecision(int precision)
Sets thelocalminimum precision (number of exact digits) when exact numbers have to be approximated.- Parameters:
precision- the minimum number of digits assumed exact forexactnumbers.
-
valueOf
public static Real valueOf(LargeInteger significand, int error, int exponent)
Returns a real having the specified significand, error and exponent values. If the error is0, 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]- Parameters:
significand- this real significand.error- the maximum error on the significand.exponent- the decimal exponent.- Returns:
(significand ± error)·10exponent)- Throws:
java.lang.IllegalArgumentException- iferror < 0
-
valueOf
public static Real valueOf(double doubleValue)
Returns the real number (inexact except for0.0) corresponding to the specifieddoublevalue. The error is derived from the inexact representation ofdoublevalues intrinsic to the 64 bits IEEE 754 format.- Parameters:
doubleValue- thedoublevalue to convert.- Returns:
- the corresponding real number.
-
valueOf
public static Real valueOf(long longValue)
Returns the exact real number corresponding to the specifiedlongvalue (convenience method).- Parameters:
longValue- the exact long value.- Returns:
Real.valueOf(LargeInteger.valueOf(longValue), 0, 0)
-
valueOf
public static Real valueOf(java.lang.CharSequence chars) throws java.lang.NumberFormatException
Returns the real for the specified character sequence. If the precision is not specified (using the±symbol), the real is supposed exact. Example of valid character sequences:- "1.2E3" (1200 exact)
- "1.2E3±1E-2" (1200 ± 0.01)
- Parameters:
chars- the character sequence.- Returns:
- the corresponding real number.
- Throws:
java.lang.NumberFormatException- if the character sequence does not contain a parsable real.
-
getSignificand
public LargeInteger getSignificand()
Returns this real significand value.- Returns:
- the significand.
-
getError
public int getError()
Returns the maximum error (positive) on this real significand.- Returns:
- the maximum error on the significand.
-
getExponent
public int getExponent()
Returns the exponent of the power of 10 multiplier.- Returns:
- the decimal exponent.
-
isExact
public boolean isExact()
Indicates if this real number is exact ().error== 0- Returns:
getError() == 0
-
getAccuracy
public int getAccuracy()
Returns the number of decimal digits guaranteed exact which appear to the right of the decimal point (absolute error).- Returns:
- a measure of the absolute error of this real number.
-
getPrecision
public final int getPrecision()
Returns the total number of decimal digits guaranteed exact (relative error).- Returns:
- a measure of the relative error of this real number.
-
isPositive
public boolean isPositive()
Indicates if this real is greater than zero.- Returns:
this > 0
-
isNegative
public boolean isNegative()
Indicates if this real is less than zero.- Returns:
this < 0
-
isNaN
public boolean isNaN()
Indicates if this real is Not-a-Number (unbounded value interval).- Returns:
trueif this number has unbounded value interval;falseotherwise.
-
approximates
public boolean approximates(Real that)
Indicates if this real approximates the one specified. This method takes into account possible errors (e.g. numeric errors) to make this determination.Note: This method returns
trueifthisorthatisNaN()(basically Not-A-Number approximates anything).- Parameters:
that- the real to compare with.- Returns:
this ≈ that
-
round
public LargeInteger round()
Returns the closest integer value to this rational number.- Returns:
- this real rounded to the nearest integer.
- Throws:
java.lang.ArithmeticException- ifthis.isNaN()
-
opposite
public Real opposite()
Returns the negation of this real number.- Specified by:
oppositein interfaceGroupAdditive<Real>- Returns:
-this.
-
plus
public Real plus(Real that)
Returns the sum of this real number with the one specified.- Specified by:
plusin interfaceGroupAdditive<Real>- Parameters:
that- the real to be added.- Returns:
this + that.
-
minus
public Real minus(Real that)
Returns the difference between this real number and the one specified.
-
times
public Real times(long multiplier)
Returns the product of this real number with the specifiedlongmultiplier.- Parameters:
multiplier- thelongmultiplier.- Returns:
this · multiplier.
-
divide
public Real divide(long divisor)
Returns this real number divided by the specifiedintdivisor.- Parameters:
divisor- theintdivisor.- Returns:
this / divisor
-
divide
public Real divide(Real that)
Returns this real number divided by the one specified.- Parameters:
that- the real divisor.- Returns:
this / that.- Throws:
java.lang.ArithmeticException- ifthat.equals(ZERO)
-
inverse
public Real inverse()
Returns the reciprocal (or inverse) of this real number.- Specified by:
inversein interfaceGroupMultiplicative<Real>- Returns:
1 / this.
-
isLargerThan
public boolean isLargerThan(Real that)
Compares the absolute value of two real numbers.- Specified by:
isLargerThanin classNumber<Real>- Parameters:
that- the real number to be compared with.- Returns:
|this| > |that|
-
sqrt
public Real sqrt()
Returns the square root of this real number, the more accurate is this real number, the more accurate the square root.- Returns:
- the positive square root of this real number.
-
toText
public javolution.text.Text toText()
Returns the decimal text representation of this number.
-
equals
public boolean equals(java.lang.Object that)
Compares this real number against the specified object.Note: This method returns
trueifthisorthatis Not-A-Number, even thoughDouble.NaN == Double.NaNhas the valuefalse.
-
hashCode
public int hashCode()
Returns the hash code for this real number.
-
longValue
public long longValue()
Returns the value of this real number as along.
-
doubleValue
public double doubleValue()
Returns the value of this real number as adouble.- Specified by:
doubleValuein classNumber<Real>- Returns:
- the numeric value represented by this real after conversion
to type
double.
-
compareTo
public int compareTo(Real that)
Compares two real numbers numerically.- Specified by:
compareToin interfacejava.lang.Comparable<Real>- Specified by:
compareToin classNumber<Real>- Parameters:
that- the real to compare with.- Returns:
- -1, 0 or 1 as this real is numerically less than, equal to,
or greater than
that. - Throws:
java.lang.ClassCastException-thatis not aReal.
-
-