| 
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object
  |
  +--java.lang.Number
        |
        +--org.znerd.math.RealNumber
An immutable real number. A number of basic numeric operations are defined, like:
negate()- Computes
 -thisinvert()- Computes
 1/thisadd(RealNumber)- Computes
 this + nsubtract(RealNumber)- Computes
 this - nmultiply(RealNumber)- Computes
 this * ndivide(RealNumber)- Computes
 this / npow(RealNumber)- Computes
 thisn
Examples of real numbers are 3, -1.68, the square root of 3, 2/3, pi, 18.22.3 and e.
Real numbers can be cast to native Java numbers with these conversion methods:
longValue()- Converts this number to a
 long. UsefitsLong()to make sure the conversion will succeedintValue()- Converts this number to an
 int. UsefitsInt()to make sure the conversion will succeedshortValue()- Converts this number to a
 short. UsefitsShort()to make sure the conversion will succeedbyteValue()- Converts this number to a
 byte. UsefitsByte()to make sure the conversion will succeeddoubleValue()- Converts this number to a
 double. UsefitsDouble()to make sure the conversion will succeedfloatValue()- Converts this number to a
 float. UsefitsFloat()to make sure the conversion will succeed
Use the NumberCentral to obtain RealNumber
 instances. For instance:
RealNumber n = NumberCentral.valueOf(0.399);
 PENDING: The names of the following methods may be changed in the future to reflect the expected change in the syntax of the Java language syntax to include operator overloading based on method names (see the section Operator Overloading in The Evolution of Numerical Computing in Java by James Gosling.)
negate()             --> uminusadd(RealNumber)      --> plussubtract(RealNumber) --> minusmultiply(RealNumber) --> timesConcrete subclasses should provide implementations for the following methods:
toBigDecimal(int,int)- Converts the value of this number to a
 BigDecimalwith the specified precision, using the specified rounding mode.trunc()- Truncates the value of this number to an
 IntegerNumber.
NumberCentral, Serialized Form| Field Summary | |
static int | 
MAXIMUM_RADIX
The maximum value for a radix.  | 
| Fields inherited from interface org.znerd.math.RoundingModes | 
ROUND_CEILING,  
ROUND_DOWN,  
ROUND_FLOOR,  
ROUND_UP | 
| Constructor Summary | |
protected  | 
RealNumber(int sign,
           String asString)
Constructs a new RealNumber object. | 
| Method Summary | |
 RealNumber | 
abs()
Computes |this|.  | 
 RealNumber | 
add(RealNumber n)
Computes this + n, where n is a real number.  | 
 byte | 
byteValue()
Returns the value of this number as a byte. | 
 int | 
compareTo(Object o)
Compares this object with the specified object.  | 
 int | 
compareTo(RealNumber n)
Compares this number with the specified number.  | 
protected  int | 
compareToImpl(RealNumber n)
Compares this number with the specified number, second level.  | 
 RealNumber | 
divide(RealNumber n)
Computes this/n, where n is a real number.  | 
 double | 
doubleValue()
Returns the value of this number as a double. | 
 boolean | 
equals(Object o)
 | 
 boolean | 
fitsByte()
Determines if the truncated value of this number fits in a byte. | 
 boolean | 
fitsDouble()
Determines if the rounded value of this number fits in a double. | 
 boolean | 
fitsFloat()
Determines if the rounded value of this number fits in a float. | 
 boolean | 
fitsInt()
Determines if the truncated value of this number fits in an int. | 
 boolean | 
fitsLong()
Determines if the truncated value of this number fits in a long. | 
 boolean | 
fitsShort()
Determines if the truncated value of this number fits in a short. | 
 float | 
floatValue()
Returns the value of this number as a float. | 
 int | 
getSign()
Determines the sign of this number.  | 
 int | 
intValue()
Returns the value of this number as an int. | 
 RealNumber | 
invert()
Computes 1/this.  | 
 long | 
longValue()
Returns the value of this number as a long. | 
 RealNumber | 
multiply(RealNumber n)
Computes this * n, where n is a real number.  | 
 RealNumber | 
negate()
Computes -this.  | 
 RealNumber | 
pow(RealNumber n)
Computes thisn, where n is a real number.  | 
 short | 
shortValue()
Returns the value of this number as a short. | 
 RealNumber | 
subtract(RealNumber n)
Computes this - n, where n is a real number.  | 
 BigDecimal | 
toBigDecimal(int precision)
Converts the value of this number to a BigDecimal with the
 specified precision. | 
abstract  BigDecimal | 
toBigDecimal(int precision,
             int roundingMode)
Converts the value of this number to a BigDecimal with the
 specified precision and rounding mode. | 
 BigInteger | 
toBigInteger()
Converts the value of this number to a BigInteger. | 
 String | 
toString()
 | 
 IntegerNumber | 
trunc()
Rounds to an integer number towards 0.  | 
| Methods inherited from class java.lang.Object | 
clone, 
finalize, 
getClass, 
hashCode, 
notify, 
notifyAll, 
wait, 
wait, 
wait | 
| Field Detail | 
public static final int MAXIMUM_RADIX
| Constructor Detail | 
protected RealNumber(int sign,
                     String asString)
RealNumber object.
 The sign of the value needs to be specified. Any negative value is interpreted as meaning that the value of this number is negative. Any positive value is interpreted as meaning that the value of this number is positive.
sign - the sign of this number.asString - textual presentation of this number, not null.asString == null.| Method Detail | 
public final String toString()
public final boolean equals(Object o)
public final int getSign()
public final int compareTo(Object o)
                    throws NullPointerException,
                           ClassCastException
This method calls compareTo(RealNumber).
o - the object to compare to, not null.n == null.(n instanceof RealNumber) == false.
public final int compareTo(RealNumber n)
                    throws NullPointerException
This method attempts to compare this number with the specified number
 based on the sign. If the signs are equal,
 compareToImpl(RealNumber) is called. If that method throws a
 CanNotCompareException, then it is attempted to return the
 negated result of n.compareToImpl(RealNumber). If
 that method throws an exception as well, then it is thrown up to the
 caller of this method.
n - the number to compare to, not null.n == null.
protected int compareToImpl(RealNumber n)
                     throws CanNotCompareException
The implementation of this method in class RealNumber throws
 a CanNotCompareException. Concrete subclasses are encouraged to
 improve this behaviour by overriding this method.
Note that this method does not check if n == null. This
 is already done in compareTo(RealNumber).
n - the number to compare to, guaranteed to be not null.public RealNumber abs()
null and always with a value
    >= 0.public RealNumber negate()
null.
public RealNumber invert()
                  throws ArithmeticException
null.
public RealNumber add(RealNumber n)
               throws IllegalArgumentException
The implementation of this method in class RealNumber calls
 NumberCentral.add(RealNumber,RealNumber).
n - the number to add to this, not null.null.n == null.
public RealNumber subtract(RealNumber n)
                    throws IllegalArgumentException
The implementation of this method in class RealNumber calls
 add(RealNumber) with n.negate() as the
 argument.
n - the number to subtract from this, not null.null.n == null.
public RealNumber multiply(RealNumber n)
                    throws IllegalArgumentException
The implementation of this method in class RealNumber calls
 NumberCentral.multiply(RealNumber,RealNumber).
n - the factor, the number to multiply this by, not null.n == null.
public RealNumber divide(RealNumber n)
                  throws IllegalArgumentException,
                         ArithmeticException
The implementation of this method in class RealNumber calls
 multiply(RealNumber) with n.invert() as the
 argument.
n - the number to divide this by, not null.null.n == null.
public RealNumber pow(RealNumber n)
               throws IllegalArgumentException
The implementation of this method in class RealNumber calls
 NumberCentral.pow(RealNumber,RealNumber).
n - the exponent, not null.n == null.public boolean fitsLong()
long.true iff this value fits in a long.public boolean fitsInt()
int.true iff this value fits in an int.public boolean fitsShort()
short.true iff this value fits in a short.public boolean fitsByte()
byte.true iff this value fits in a byte.public boolean fitsDouble()
double.true iff this value fits in a double.public boolean fitsFloat()
float.true iff this value fits in a float.public long longValue()
long. This may
 involve rounding.long.public int intValue()
int. This may
 involve rounding.int.public short shortValue()
short. This may
 involve rounding.short.public byte byteValue()
byte. This may
 involve rounding.byte.public double doubleValue()
double. This may
 involve rounding.double.public float floatValue()
float. This may
 involve rounding.float.
public BigDecimal toBigDecimal(int precision)
                        throws IllegalArgumentException
BigDecimal with the
 specified precision. This method uses the ROUND_HALF_UP
 rounding mode as defined in BigDecimal.precision - the number of digits behind the decimal point.BigDecimal with the rounded value of this.precision < 0.
public abstract BigDecimal toBigDecimal(int precision,
                                        int roundingMode)
                                 throws IllegalArgumentException
BigDecimal with the
 specified precision and rounding mode.precision - the number of digits behind the decimal point, >= 0.roundingMode - the rounding mode to use, one of the modes defined in class
    BigDecimal.BigDecimal with the rounded value of this, never
    null.BigDecimalpublic BigInteger toBigInteger()
BigInteger. This
 may involve rounding.BigInteger.public IntegerNumber trunc()
null.
  | 
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||