Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Interface nl.coffee.math.RealNumber

Subinterfaces:
CompositeNumber, RationalNumber
Implementing Classes:
AbstractRealNumber

public abstract interface RealNumber
An immutable real number. This interface provides the base for all real numbers, with a number of basic numeric operations, like:

Examples of real numbers are 3, -1.68, the square root of 3, 2/3, pi, 18.2**2.3 and e.

Real numbers can be cast to native Java numbers with these conversion methods:

Use the NumberCentral to obtain RealNumber instances. For instance:

RealNumber n = NumberCentral.valueOf(0.399);

Concrete implementations of this interface should implement the boolean equals(Object) method derived from java.lang.Object in order to compare RealNumber instances for equality.

PENDING (1): The names of the following methods may be changed in the near future to reflect the expected change in the syntax of the Java language syntax to include operator overloading based on method names:

PENDING (2): Rounding modes different from trunc should be specified. Method names may include roundUp, roundDown etc. if this is in accordance with proposed changes to language syntax (See: "The Evolution of Numerical Computing in Java" by James Gosling, available from the Sun JavaSoft website.)

Since:
JUMP_1.0
Version:
1.2 199809.24
Author:
Ernst de Haan
See Also:
NumberCentral

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(RealNumber n)
          Returns -1, 0 or 1 as this number is less than, equal to, or greater than n, where n is a real number.
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 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 this**n, where n is a real number.
int retryCompare(RealNumber n)
          Returns -1, 0 or 1 as this number is less than, equal to, or greater than n, 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.
java.math.BigDecimal toBigDecimal(int precision)
          Converts the value of this number to a BigDecimal with the specified precision.
java.math.BigDecimal toBigDecimal(int precision, int roundingMode)
          Converts the value of this number to a BigDecimal with the specified precision and rounding mode.
java.math.BigInteger toBigInteger()
          Converts the value of this number to a BigInteger.
IntegerNumber trunc()
          Rounds to an integer number towards 0.
 

Method Detail

getSign

public int getSign()
Determines the sign of this number. This method returns -1, 0, or 1 as this number is negative, zero, or positive.
Returns:
the sign of this number.

compareTo

public int compareTo(RealNumber n)
             throws java.lang.IllegalArgumentException,
                    CanNotCompareException
Returns -1, 0 or 1 as this number is less than, equal to, or greater than n, where n is a real number. If this number cannot be compared with n, then n.retryCompare(this) should be called in order to try to let n compare to this. If that succeeds, the negative of the result may be returned. If fails, it will throw a CanNotCompareException.
Parameters:
n - the number to compare to.
Returns:
-1 if this < n, 0 if this == n, 1 if this > n.
Throws:
java.lang.IllegalArgumentException - if n == null.
CanNotCompareException - if this number can not compare with the n, and vice versa.

retryCompare

public int retryCompare(RealNumber n)
                throws java.lang.IllegalArgumentException,
                       CanNotCompareException
Returns -1, 0 or 1 as this number is less than, equal to, or greater than n, where n is a real number. If this number cannot be compared with n, then a CanNotCompareException is thrown.
Parameters:
n - the number to compare to.
Returns:
-1 if this < n, 0 if this == n, 1 if this > n.
Throws:
java.lang.IllegalArgumentException - if n == null.
CanNotCompareException - if this number can not compare with the n.

abs

public RealNumber abs()
Computes |this|. The returned value is allways >= 0.
Returns:
the absolute of this.

negate

public RealNumber negate()
Computes -this.
Returns:
the negative of this.

invert

public RealNumber invert()
                                throws java.lang.ArithmeticException
Computes 1/this.
Returns:
the inverse of this.
Throws:
java.lang.ArithmeticException - if the value of this is zero.

add

public RealNumber add(RealNumber n)
                             throws java.lang.IllegalArgumentException
Computes this+n, where n is a real number.
Parameters:
n - the number to add to this.
Returns:
the sum of this and n.
Throws:
java.lang.IllegalArgumentException - if n is null.

subtract

public RealNumber subtract(RealNumber n)
                                  throws java.lang.IllegalArgumentException
Computes this-n, where n is a real number.
Parameters:
n - the number to subtract from this.
Returns:
this minus n.
Throws:
java.lang.IllegalArgumentException - if n is null.

multiply

public RealNumber multiply(RealNumber n)
                                  throws java.lang.IllegalArgumentException
Computes this*n, where n is a real number.
Parameters:
n - the factor, the number to multiply this by.
Returns:
the product of this and n.
Throws:
java.lang.IllegalArgumentException - if n is null.

divide

public RealNumber divide(RealNumber n)
                                throws java.lang.IllegalArgumentException,
                                       java.lang.ArithmeticException
Computes this/n, where n is a real number.
Parameters:
n - the number to divide this by.
Returns:
this divided by n.
Throws:
java.lang.IllegalArgumentException - if n is null.
java.lang.ArithmeticException - if the value of n is zero.

pow

public RealNumber pow(RealNumber n)
                             throws java.lang.IllegalArgumentException
Computes this**n, where n is a real number.
Parameters:
n - the exponent.
Returns:
this raised to the power of n.
Throws:
java.lang.IllegalArgumentException - if n is null.

fitsLong

public boolean fitsLong()
Determines if the truncated value of this number fits in a long.
Returns:
true iff this value fits in a long.

fitsInt

public boolean fitsInt()
Determines if the truncated value of this number fits in an int.
Returns:
true iff this value fits in an int.

fitsShort

public boolean fitsShort()
Determines if the truncated value of this number fits in a short.
Returns:
true iff this value fits in a short.

fitsByte

public boolean fitsByte()
Determines if the truncated value of this number fits in a byte.
Returns:
true iff this value fits in a byte.

fitsDouble

public boolean fitsDouble()
Determines if the rounded value of this number fits in a double.
Returns:
true iff this value fits in a double.

fitsFloat

public boolean fitsFloat()
Determines if the rounded value of this number fits in a float.
Returns:
true iff this value fits in a float.

longValue

public long longValue()
Returns the value of this number as a long. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type long.

intValue

public int intValue()
Returns the value of this number as an int. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type int.

shortValue

public short shortValue()
Returns the value of this number as a short. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type short.

byteValue

public byte byteValue()
Returns the value of this number as a byte. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type byte.

doubleValue

public double doubleValue()
Returns the value of this number as a double. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type double.

floatValue

public float floatValue()
Returns the value of this number as a float. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type float.

toBigDecimal

public java.math.BigDecimal toBigDecimal(int precision)
                                 throws java.lang.IllegalArgumentException
Converts the value of this number to a BigDecimal with the specified precision. This method uses the ROUND_HALF_UP rounding mode as defined in BigDecimal.
Parameters:
precision - the number of digits behind the decimal point.
Returns:
a BigDecimal with the rounded value of this.
Throws:
java.lang.IllegalArgumentException - if precision < 0.

toBigDecimal

public java.math.BigDecimal toBigDecimal(int precision,
                               int roundingMode)
                                 throws java.lang.IllegalArgumentException
Converts the value of this number to a BigDecimal with the specified precision and rounding mode.
Parameters:
precision - the number of digits behind the decimal point.
roundingMode - the rounding mode to use, one of the modes defined in class BigDecimal.
Returns:
a BigDecimal with the rounded value of this.
Throws:
java.lang.IllegalArgumentException - if one of the following applies:
  1. precision < 0
  2. roundingMode is not one of the valid rounding modes defined in class BigDecimal

toBigInteger

public java.math.BigInteger toBigInteger()
Converts the value of this number to a BigInteger. This may involve rounding.
Returns:
the numeric value represented by this object after conversion to type BigInteger.

trunc

public IntegerNumber trunc()
Rounds to an integer number towards 0.
Returns:
this real number truncated to an integer.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD