SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.number
Class Real

java.lang.Object
  extended by java.lang.Number
      extended by com.numericalmethod.suanshu.number.Real
All Implemented Interfaces:
AbelianGroup<Real>, Field<Real>, Monoid<Real>, Ring<Real>, java.io.Serializable, java.lang.Comparable<Real>

public class Real
extends java.lang.Number
implements Field<Real>, java.lang.Comparable<Real>

This class represents an arbitrary precision real number.

This is simply a wrapper around BigDecimal and implements the Field interface.

This class is immutable.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.mathstructure.Field
Field.InverseNonExistent
 
Field Summary
static Real ONE
          a number representing 1
static Real ZERO
          a number representing 0
 
Constructor Summary
Real(java.math.BigDecimal value)
          Construct a Real from a BigDecimal.
Real(java.math.BigInteger value)
          Construct a Real from a BigInteger.
Real(double value)
          Construct a Real from a double.
Real(long value)
          Construct a Real from an integer.
Real(java.lang.String value)
          Construct a Real from a String representation of a number.
 
Method Summary
 Real add(Real that)
          + : G × G → G
 java.math.BigDecimal bigDecimal()
          Construct a BigDecimal from this Real number.
 int compareTo(Real that)
           
 Real divide(Real that)
          / : F × F → F That is the same as this.multiply(that.inverse())
 Real divide(Real that, int scale)
          / : R × R → R Divide a Real number by another one.
 double doubleValue()
           
 boolean equals(java.lang.Object obj)
           
 float floatValue()
           
 int hashCode()
           
 int intValue()
           
 Real inverse()
          For each a in F, there exists an element b in F such that a × b = b × a = 1 That is, it is the object such as this.multiply(this.inverse()) == this.ONE
 long longValue()
           
 Real minus(Real that)
          - : G × G → G - is not in the definition of of an additive group but can be deduced.
 Real multiply(Real that)
          · : G × G → G
 Real ONE()
          The multiplicative element 1 in the group such that for any elements a in the group, the equation 1 × a = a × 1 = a holds.
 Real opposite()
          For each a in G, there exists an element b in G such that a + b = b + a = 0 That is, it is the object such as this.add(this.opposite()) == this.ZERO
 java.lang.String toString()
           
 Real ZERO()
          The additive element 0 in the group, such that for all elements a in the group, the equation 0 + a = a + 0 = a holds.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Real ZERO
a number representing 0


ONE

public static final Real ONE
a number representing 1

Constructor Detail

Real

public Real(double value)
Construct a Real from a double. To ensure accuracy, value is first converted to String and then to BigDecimal.

Parameters:
value - a double value

Real

public Real(long value)
Construct a Real from an integer.

Parameters:
value - an integer

Real

public Real(java.math.BigDecimal value)
Construct a Real from a BigDecimal.

Parameters:
value - a BigDecimal

Real

public Real(java.math.BigInteger value)
Construct a Real from a BigInteger.

Parameters:
value - a BigInteger

Real

public Real(java.lang.String value)
Construct a Real from a String representation of a number.

Parameters:
value - a String representation of a number
Method Detail

intValue

public int intValue()
Specified by:
intValue in class java.lang.Number

longValue

public long longValue()
Specified by:
longValue in class java.lang.Number

floatValue

public float floatValue()
Specified by:
floatValue in class java.lang.Number

doubleValue

public double doubleValue()
Specified by:
doubleValue in class java.lang.Number

bigDecimal

public java.math.BigDecimal bigDecimal()
Construct a BigDecimal from this Real number.

Returns:
the same value in BigDecimal

add

public Real add(Real that)
Description copied from interface: AbelianGroup
+ : G × G → G

Specified by:
add in interface AbelianGroup<Real>
Parameters:
that - the object to be added
Returns:
this + that

minus

public Real minus(Real that)
Description copied from interface: AbelianGroup
- : G × G → G

- is not in the definition of of an additive group but can be deduced. This function is provided for convenience purpose. It is equivalent to

this.add(that.opposite())

Specified by:
minus in interface AbelianGroup<Real>
Parameters:
that - the object to be subtracted (subtrahend)
Returns:
this - that

opposite

public Real opposite()
Description copied from interface: AbelianGroup
For each a in G, there exists an element b in G such that
a + b = b + a = 0

That is, it is the object such as

this.add(this.opposite()) == this.ZERO

Specified by:
opposite in interface AbelianGroup<Real>
Returns:
-this
See Also:
Wikipedia: Additive inverse

multiply

public Real multiply(Real that)
Description copied from interface: Monoid
· : G × G → G

Specified by:
multiply in interface Monoid<Real>
Parameters:
that - the multiplicand
Returns:
this × that

divide

public Real divide(Real that)
Description copied from interface: Field
/ : F × F → F

That is the same as

this.multiply(that.inverse())

Specified by:
divide in interface Field<Real>
Parameters:
that - the denominator
Returns:
this / that

divide

public Real divide(Real that,
                   int scale)
/ : R × R → R

Divide a Real number by another one. Rounding is performed with the specific scale.

Parameters:
that - another non-zero Real number
scale - scale as in BigDecimal
Returns:
this/that

inverse

public Real inverse()
             throws Field.InverseNonExistent
Description copied from interface: Field
For each a in F, there exists an element b in F such that
a × b = b × a = 1

That is, it is the object such as

this.multiply(this.inverse()) == this.ONE

Specified by:
inverse in interface Field<Real>
Returns:
1 / this if it exists
Throws:
Field.InverseNonExistent - if the inverse does not exist
See Also:
Wikipedia: Multiplicative inverse

ZERO

public Real ZERO()
Description copied from interface: AbelianGroup
The additive element 0 in the group, such that for all elements a in the group, the equation
0 + a = a + 0 = a
holds.

Specified by:
ZERO in interface AbelianGroup<Real>
Returns:
0

ONE

public Real ONE()
Description copied from interface: Monoid
The multiplicative element 1 in the group such that for any elements a in the group, the equation
1 × a = a × 1 = a
holds.

Specified by:
ONE in interface Monoid<Real>
Returns:
1

compareTo

public int compareTo(Real that)
Specified by:
compareTo in interface java.lang.Comparable<Real>

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.