Class RealNumber
java.lang.Object
java.lang.Number
org.znerd.math.RealNumber
- All Implemented Interfaces:
Serializable, Comparable, RoundingModes
- Direct Known Subclasses:
CompositeNumber, RationalNumber
An immutable real number. A number of basic numeric operations are defined,
like:
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:
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
Use the
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
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)--> times
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.
- Version:
- $Revision: 1.17 $ $Date: 2002/10/01 18:53:42 $
- Author:
- Ernst de Haan (znerd@FreeBSD.org)
- See Also:
-
Field Summary
FieldsFields inherited from interface RoundingModes
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRealNumber(int sign, String asString) Constructs a newRealNumberobject. -
Method Summary
Modifier and TypeMethodDescriptionabs()Computes |this|.add(RealNumber n) Computes this + n, where n is a real number.byteReturns the value of this number as abyte.final intCompares this object with the specified object.final intCompares this number with the specified number.protected intCompares this number with the specified number, second level.divide(RealNumber n) Computes this/n, where n is a real number.doubleReturns the value of this number as adouble.final booleanbooleanfitsByte()Determines if the truncated value of this number fits in abyte.booleanDetermines if the rounded value of this number fits in adouble.booleanDetermines if the rounded value of this number fits in afloat.booleanfitsInt()Determines if the truncated value of this number fits in anint.booleanfitsLong()Determines if the truncated value of this number fits in along.booleanDetermines if the truncated value of this number fits in ashort.floatReturns the value of this number as afloat.final intgetSign()Determines the sign of this number.intintValue()Returns the value of this number as anint.invert()Computes 1/this.longReturns the value of this number as along.Computes this * n, where n is a real number.negate()Computes -this.pow(RealNumber n) Computes thisn, where n is a real number.protected RealNumberComputes thisn, where n is a real number, second level.round(int radix, int precision, RoundingMode roundingMode) Rounds to the specified radix, using the specified precision and rounding mode.shortReturns the value of this number as ashort.Computes this - n, where n is a real number.toBigDecimal(int precision) Converts the value of this number to aBigDecimalwith the specified precision.abstract BigDecimaltoBigDecimal(int precision, int roundingMode) Converts the value of this number to aBigDecimalwith the specified precision and rounding mode.Converts the value of this number to aBigInteger.final StringtoString()trunc()Rounds to an integer number towards 0.
-
Field Details
-
MAXIMUM_RADIX
public static final int MAXIMUM_RADIXThe maximum value for a radix. The value of this field is 255.- See Also:
-
-
Constructor Details
-
RealNumber
Constructs a newRealNumberobject. 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.- Parameters:
sign- the sign of this number; -1 if this number is smaller than zero, 0 is this number is 0 or 1 if this number is greater than zero.asString- textual presentation of this number, notnull.- Throws:
IllegalArgumentException- ifasString == null.
-
-
Method Details
-
toString
-
equals
-
getSign
public final 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, either -1, 0 or 1.
-
compareTo
Compares this object with the specified object. This method callscompareTo(RealNumber).- Specified by:
compareToin interfaceComparable- Parameters:
o- the object to compare to, notnull.- Returns:
- -1 if this object is considered less than o, 0 if this object is considered equal n, 1 if this object is greater than n.
- Throws:
NullPointerException- ifn == null.ClassCastException- if(n instanceof RealNumber) == false.
-
compareTo
Compares this number with the specified number. 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 aCanNotCompareException, then it is attempted to return the negated result ofn.compareToImpl(RealNumber). If that method throws an exception as well, then it is thrown up to the caller of this method.- Parameters:
n- the number to compare to, notnull.- Returns:
- -1 if this < n, 0 if this == n, 1 if this > n.
- Throws:
NullPointerException- ifn == null.
-
compareToImpl
Compares this number with the specified number, second level. The implementation of this method in classRealNumberthrows aCanNotCompareException. Concrete subclasses are encouraged to improve this behaviour by overriding this method. Note that this method does not check ifn == null. This is already done incompareTo(RealNumber).- Parameters:
n- the number to compare to, guaranteed to be notnull.- Returns:
- -1 if this < n, 0 if this == n, 1 if this > n.
- Throws:
CanNotCompareException- if the comparison failed.
-
round
public DigitSet round(int radix, int precision, RoundingMode roundingMode) throws IllegalArgumentException, UnsupportedOperationException Rounds to the specified radix, using the specified precision and rounding mode. If rounding is not supported by this class, then anUnsupportedOperationExceptionis thrown. The implementation of this method in classRealNumberthrows such an exception.- Parameters:
radix- the radix, always >= 2 and <=MAXIMUM_RADIX.precision- the precision, always >= 1.roundingMode- the rounding mode, nevernull.- Returns:
- the rounded number, never
null. - Throws:
IllegalArgumentException- ifradix < 2 || radix >.MAXIMUM_RADIX|| precision < 1 || roundingMode == nullUnsupportedOperationException- if rounding is not supported by this class.
-
abs
Computes |this|. The returned value is always >= 0.- Returns:
- the absolute of this, not
nulland always with a value >= 0.
-
negate
-
invert
Computes 1/this.- Returns:
- the inverse of this, not
null. - Throws:
ArithmeticException- if the value of this is zero.
-
add
Computes this + n, where n is a real number.- Parameters:
n- the number to add to this, notnull.- Returns:
- the sum of this and n, not
null. - Throws:
IllegalArgumentException- ifn == null.
-
subtract
Computes this - n, where n is a real number. The implementation of this method in classRealNumbercallsadd(RealNumber)withn.negate()as the argument.- Parameters:
n- the number to subtract from this, notnull.- Returns:
- this minus n, not
null. - Throws:
IllegalArgumentException- ifn == null.
-
multiply
Computes this * n, where n is a real number.- Parameters:
n- the factor, the number to multiply this by, notnull.- Returns:
- the product of this and n.
- Throws:
IllegalArgumentException- ifn == null.
-
divide
Computes this/n, where n is a real number. The implementation of this method in classRealNumbercallsmultiply(RealNumber)withn.invert()as the argument.- Parameters:
n- the number to divide this by, notnull.- Returns:
- this divided by n, not
null. - Throws:
IllegalArgumentException- ifn == null.ArithmeticException- if the value of n is zero.
-
pow
Computes thisn, where n is a real number. This method first checks the precondition and then callspowImpl(RealNumber). If that method throws anUnsupportedOperationExceptionthen aPowerinstance is returned.- Parameters:
n- the exponent, notnull.- Returns:
- this raised to the power of n, never
null. - Throws:
IllegalArgumentException- ifn == null.
-
powImpl
Computes thisn, where n is a real number, second level. This method may be implemented by subclasses. The implementation of this method in classRealNumberthrows anUnsupportedOperationException.- Parameters:
n- the exponent, guaranteed not to benullif called bypow(RealNumber).- Returns:
- this raised to the power of n, never
null. - Throws:
UnsupportedOperationException- if this operation is not supported by this class.
-
fitsLong
public boolean fitsLong()Determines if the truncated value of this number fits in along.- Returns:
trueiff this value fits in along.
-
fitsInt
public boolean fitsInt()Determines if the truncated value of this number fits in anint.- Returns:
trueiff this value fits in anint.
-
fitsShort
public boolean fitsShort()Determines if the truncated value of this number fits in ashort.- Returns:
trueiff this value fits in ashort.
-
fitsByte
public boolean fitsByte()Determines if the truncated value of this number fits in abyte.- Returns:
trueiff this value fits in abyte.
-
fitsDouble
public boolean fitsDouble()Determines if the rounded value of this number fits in adouble.- Returns:
trueiff this value fits in adouble.
-
fitsFloat
public boolean fitsFloat()Determines if the rounded value of this number fits in afloat.- Returns:
trueiff this value fits in afloat.
-
longValue
-
intValue
-
shortValue
public short shortValue()Returns the value of this number as ashort. This may involve rounding.- Overrides:
shortValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
short.
-
byteValue
-
doubleValue
public double doubleValue()Returns the value of this number as adouble. This may involve rounding.- Specified by:
doubleValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
double.
-
floatValue
public float floatValue()Returns the value of this number as afloat. This may involve rounding.- Specified by:
floatValuein classNumber- Returns:
- the numeric value represented by this object after conversion
to type
float.
-
toBigDecimal
Converts the value of this number to aBigDecimalwith the specified precision. This method uses theROUND_HALF_UProunding mode as defined inBigDecimal.- Parameters:
precision- the number of digits behind the decimal point.- Returns:
- a
BigDecimalwith the rounded value of this. - Throws:
IllegalArgumentException- ifprecision < 0.
-
toBigDecimal
public abstract BigDecimal toBigDecimal(int precision, int roundingMode) throws IllegalArgumentException Converts the value of this number to aBigDecimalwith the specified precision and rounding mode.- Parameters:
precision- the number of digits behind the decimal point, >= 0.roundingMode- the rounding mode to use, one of the modes defined in classBigDecimal.- Returns:
- a
BigDecimalwith the rounded value of this, nevernull. - Throws:
IllegalArgumentException- if one of the following applies:- precision < 0
- roundingMode is not one of the valid rounding modes
defined in class
BigDecimal
-
toBigInteger
Converts the value of this number to aBigInteger. This may involve rounding.- Returns:
- the numeric value represented by this object after conversion
to type
BigInteger.
-
trunc
Rounds to an integer number towards 0.- Returns:
- this real number truncated to an integer, never
null.
-