public abstract class RealNumber extends java.lang.Number implements java.lang.Comparable, RoundingModes
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
-this
invert()
- Computes
1/this
add(RealNumber)
- Computes
this + n
subtract(RealNumber)
- Computes
this - n
multiply(RealNumber)
- Computes
this * n
divide(RealNumber)
- Computes
this / n
pow(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
BigDecimal
with the specified precision, using the specified rounding mode.trunc()
- Truncates the value of this number to an
IntegerNumber
.
NumberCentral
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
MAXIMUM_RADIX
The maximum value for a radix.
|
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP
Modifier | Constructor and Description |
---|---|
protected |
RealNumber(int sign,
java.lang.String asString)
Constructs a new
RealNumber object. |
Modifier and Type | Method and Description |
---|---|
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(java.lang.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(java.lang.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.
|
protected RealNumber |
powImpl(RealNumber n)
Computes thisn, where n is a real number,
second level.
|
DigitSet |
round(int radix,
int precision,
RoundingMode roundingMode)
Rounds to the specified radix, using the specified precision and
rounding mode.
|
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. |
abstract 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 . |
java.lang.String |
toString() |
IntegerNumber |
trunc()
Rounds to an integer number towards 0.
|
public static final int MAXIMUM_RADIX
protected RealNumber(int sign, java.lang.String asString) throws java.lang.IllegalArgumentException
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; -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, not null
.java.lang.IllegalArgumentException
- if asString == null
.public final java.lang.String toString()
toString
in class java.lang.Object
public final boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public final int getSign()
public final int compareTo(java.lang.Object o) throws java.lang.NullPointerException, java.lang.ClassCastException
compareTo(RealNumber)
.compareTo
in interface java.lang.Comparable
o
- the object to compare to, not null
.java.lang.NullPointerException
- if n == null
.java.lang.ClassCastException
- if (n instanceof RealNumber) == false
.public final int compareTo(RealNumber n) throws java.lang.NullPointerException
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
.java.lang.NullPointerException
- if n == null
.protected int compareToImpl(RealNumber n) throws CanNotCompareException
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
.CanNotCompareException
- if the comparison failed.public DigitSet round(int radix, int precision, RoundingMode roundingMode) throws java.lang.IllegalArgumentException, java.lang.UnsupportedOperationException
UnsupportedOperationException
is thrown. The implementation of
this method in class RealNumber
throws such an exception.radix
- the radix, always >= 2 and <= MAXIMUM_RADIX
.precision
- the precision, always >= 1.roundingMode
- the rounding mode, never null
.null
.java.lang.IllegalArgumentException
- if radix < 2
|| radix > MAXIMUM_RADIX
|| precision < 1
|| roundingMode == null
.java.lang.UnsupportedOperationException
- if rounding is not supported by this class.public RealNumber abs()
null
and always with a value
>= 0.public RealNumber negate()
null
.public RealNumber invert() throws java.lang.ArithmeticException
null
.java.lang.ArithmeticException
- if the value of this is zero.public RealNumber add(RealNumber n) throws java.lang.IllegalArgumentException
n
- the number to add to this, not null
.null
.java.lang.IllegalArgumentException
- if n == null
.public RealNumber subtract(RealNumber n) throws java.lang.IllegalArgumentException
RealNumber
calls
add(RealNumber)
with n.negate()
as the
argument.n
- the number to subtract from this, not null
.null
.java.lang.IllegalArgumentException
- if n == null
.public RealNumber multiply(RealNumber n) throws java.lang.IllegalArgumentException
n
- the factor, the number to multiply this by, not null
.java.lang.IllegalArgumentException
- if n == null
.public RealNumber divide(RealNumber n) throws java.lang.IllegalArgumentException, java.lang.ArithmeticException
RealNumber
calls
multiply(RealNumber)
with n.invert()
as the
argument.n
- the number to divide this by, not null
.null
.java.lang.IllegalArgumentException
- if n == null
.java.lang.ArithmeticException
- if the value of n is zero.public RealNumber pow(RealNumber n) throws java.lang.IllegalArgumentException
powImpl(RealNumber)
. If that method throws an
UnsupportedOperationException
then a Power
instance is
returned.n
- the exponent, not null
.null
.java.lang.IllegalArgumentException
- if n == null
.protected RealNumber powImpl(RealNumber n)
RealNumber
throws an
UnsupportedOperationException
.n
- the exponent, guaranteed not to be null
if called by
pow(RealNumber)
.null
.java.lang.UnsupportedOperationException
- if this operation is not supported by this class.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.longValue
in class java.lang.Number
long
.public int intValue()
int
. This may
involve rounding.intValue
in class java.lang.Number
int
.public short shortValue()
short
. This may
involve rounding.shortValue
in class java.lang.Number
short
.public byte byteValue()
byte
. This may
involve rounding.byteValue
in class java.lang.Number
byte
.public double doubleValue()
double
. This may
involve rounding.doubleValue
in class java.lang.Number
double
.public float floatValue()
float
. This may
involve rounding.floatValue
in class java.lang.Number
float
.public java.math.BigDecimal toBigDecimal(int precision) throws java.lang.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.java.lang.IllegalArgumentException
- if precision < 0
.public abstract java.math.BigDecimal toBigDecimal(int precision, int roundingMode) throws java.lang.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
.java.lang.IllegalArgumentException
- if one of the following applies:
BigDecimal
public java.math.BigInteger toBigInteger()
BigInteger
. This
may involve rounding.BigInteger
.public IntegerNumber trunc()
null
.