|
|||||||||
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:
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 FormField 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. |
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. |
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) throws 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
.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
compareTo(RealNumber)
.o
- the object to compare to, not null
.n == null
.(n instanceof RealNumber) == false
.public final int compareTo(RealNumber n) throws 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
.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
.public DigitSet round(int radix, int precision, RoundingMode roundingMode) throws IllegalArgumentException, 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
.radix < 2
|| radix > MAXIMUM_RADIX
|| precision < 1
|| roundingMode == 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
n
- the number to add to this, not null
.null
.n == null
.public RealNumber subtract(RealNumber n) throws IllegalArgumentException
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
n
- the factor, the number to multiply this by, not null
.n == null
.public RealNumber divide(RealNumber n) throws IllegalArgumentException, ArithmeticException
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
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
.BigDecimal
public 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 |