ral
Class Real
java.lang.Object
ral.Real
public final class Real
- extends Object
Java integer implementation of 63-bit precision floating point.
Version 1.13
Copyright 2003-2009 Roar Lauritzsen
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
The following link provides a copy of the GNU General Public License:
http://www.gnu.org/licenses/gpl.txt
If you are unable to obtain the copy from this address, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
General notes
Real
objects are not immutable, like Java
Double
or BigDecimal
. This means that you
should not think of a Real
object as a "number", but rather
as a "register holding a number". This design choice is done to encourage
object reuse and limit garbage production for more efficient execution on
e.g. a limited MIDP device. The design choice is reflected in the API,
where an operation like add
does not return a new
object containing the result (as with BigDecimal
), but rather
adds the argument to the object itself, and returns nothing.
- This library implements infinities and NaN (Not-a-Number) following
the IEEE 754 logic. If an operation produces a result larger (in
magnitude) than the largest representable number, a value representing
positive or negative infinity is generated. If an operation produces a
result smaller than the smallest representable number, a positive or
negative zero is generated. If an operation is undefined, a NaN value is
produced. Abnormal numbers are often fine to use in further
calculations. In most cases where the final result would be meaningful,
abnormal numbers accomplish this, e.g. atan(1/0)=π/2. In most cases
where the final result is not meaningful, a NaN will be produced.
No exception is ever (deliberately) thrown.
- Error bounds listed under Method Detail
are calculated using William Rossi's rossi.dfp.dfp at 40 decimal digits
accuracy. Error bounds are for "typical arguments" and may increase when
results approach zero or
infinity. The abbreviation
ULP
means Unit in the
Last Place. An error bound of ˝ ULP means that the result is correctly
rounded. The relative execution time listed under each method is the
average from running on SonyEricsson T610 (R3C), K700i, and Nokia 6230i.
- The library is not thread-safe. Static
Real
objects are
used extensively as temporary values to avoid garbage production and the
overhead of new
. To make the library thread-safe, references
to all these static objects must be replaced with code that instead
allocates new Real
objects in their place.
- There is one bug that occurs again and again and is really difficult
to debug. Although the pre-calculated constants are declared
static
final
, Java cannot really protect the contents of the objects in
the same way as const
s are protected in C/C++. Consequently,
you can accidentally change these values if you send them into a function
that modifies its arguments. If you were to modify Real.ONE
for instance, many of the succeeding calculations would be wrong because
the same variable is used extensively in the internal calculations of
Real.java.
Field Summary |
static Real |
E
A Real constant that is closer than any other to e,
the base of the natural logarithms. |
int |
exponent
The exponent of a Real . |
static Real |
FIVE
A Real constant holding the exact value of 5. |
static Real |
HALF
A Real constant holding the exact value of 1/2. |
static String |
hexChar
This string holds the only valid characters to use in hexadecimal
numbers. |
static Real |
HUNDRED
A Real constant holding the exact value of 100. |
static Real |
INF
A Real constant holding the value of positive infinity. |
static Real |
INF_N
A Real constant holding the value of negative infinity. |
static Real |
LN10
A Real constant that is closer than any other to the
natural logarithm of 10. |
static Real |
LN2
A Real constant that is closer than any other to the
natural logarithm of 2. |
static Real |
LOG10E
A Real constant that is closer than any other to the
base-10 logarithm of e. |
static Real |
LOG2E
A Real constant that is closer than any other to the
base-2 logarithm of e. |
static boolean |
magicRounding
Set to false during numerical algorithms to favor accuracy
over prettyness. |
long |
mantissa
The mantissa of a Real . |
static Real |
MAX
A Real constant holding the maximum non-infinite positive
number = 4.197e323228496. |
static Real |
MIN
A Real constant holding the minimum non-zero positive
number = 2.383e-323228497. |
static Real |
NAN
A Real constant holding the value of NaN (not-a-number). |
static Real |
ONE
A Real constant holding the exact value of 1. |
static Real |
ONE_N
A Real constant holding the exact value of -1. |
static Real |
PERCENT
A Real constant that is closer than any other to 1/100. |
static Real |
PI
A Real constant that is closer than any other to π, the
ratio of the circumference of a circle to its diameter. |
static Real |
PI_2
A Real constant that is closer than any other to π/2. |
static Real |
PI_4
A Real constant that is closer than any other to π/4. |
static Real |
PI_8
A Real constant that is closer than any other to π/8. |
static Real |
PI2
A Real constant that is closer than any other to 2π. |
static long |
randSeedA
The seed of the first 64-bit CRC generator of the random
routine. |
static long |
randSeedB
The seed of the second 64-bit CRC generator of the random
routine. |
byte |
sign
The sign of a Real . |
static Real |
SQRT1_2
A Real constant that is closer than any other to the
square root of 1/2. |
static Real |
SQRT2
A Real constant that is closer than any other to the
square root of 2. |
static Real |
TEN
A Real constant holding the exact value of 10. |
static Real |
TENTH
A Real constant that is closer than any other to 1/10. |
static Real |
THIRD
A Real constant that is closer than any other to 1/3. |
static Real |
THREE
A Real constant holding the exact value of 3. |
static Real |
TWO
A Real constant holding the exact value of 2. |
static Real |
ZERO
A Real constant holding the exact value of 0. |
static Real |
ZERO_N
A Real constant holding the value of negative zero. |
Constructor Summary |
Real()
Creates a new Real with a value of zero. |
Real(byte[] data,
int offset)
Creates a new Real , assigning the value previously encoded
into twelve consecutive bytes in a byte array using toBytes . |
Real(int a)
Creates a new Real , assigning the value of an integer. |
Real(int s,
int e,
long m)
Creates a new Real , assigning a value by directly setting
the fields of the internal representation. |
Real(long a)
Creates a new Real , assigning the value of a long
integer. |
Real(Real a)
Creates a new Real , assigning the value of another
Real . |
Real(String a)
Creates a new Real , assigning the value encoded in a
String using base-10. |
Real(String a,
int base)
Creates a new Real , assigning the value encoded in a
String using the specified number base. |
Method Summary |
void |
abs()
Calculates the absolute value. |
boolean |
absLessThan(Real a)
Returns true if the absolute value of this
Real is less than the absolute value of
a . |
static void |
accumulateRandomness(long seed)
Accumulate more randomness into the random number generator, to
decrease the predictability of the output from random() . |
void |
acos()
Calculates the trigonometric arc cosine of this Real ,
in the range 0.0 to π. |
void |
acosh()
Calculates the hyperbolic arc cosine of this Real . |
void |
add(int a)
Calculates the sum of this Real and the integer
a . |
void |
add(Real a)
Calculates the sum of this Real and a . |
long |
add128(long extra,
Real a,
long aExtra)
Calculates the sum of this Real and a with
extended precision. |
void |
and(Real a)
Calculates the logical AND of this Real and
a . |
void |
asin()
Calculates the trigonometric arc sine of this Real ,
in the range -π/2 to π/2. |
void |
asinh()
Calculates the hyperbolic arc sine of this Real . |
void |
assign(byte[] data,
int offset)
Assigns this Real a value previously encoded into into
twelve consecutive bytes in a byte array using toBytes . |
void |
assign(int a)
Assigns this Real the value of an integer. |
void |
assign(int s,
int e,
long m)
Assigns this Real a value by directly setting the fields
of the internal representation. |
void |
assign(long a)
Assigns this Real the value of a signed long integer. |
void |
assign(Real a)
Assigns this Real the value of another Real . |
void |
assign(String a)
Assigns this Real a value encoded in a String
using base-10, as specified in assign(String,int) . |
void |
assign(String a,
int base)
Assigns this Real a value encoded in a String
using the specified number base. |
void |
assignDoubleBits(long bits)
Assigns this Real the value corresponding to a given bit
representation. |
void |
assignFloatBits(int bits)
Assigns this Real the value corresponding to a given bit
representation. |
void |
atan()
Calculates the trigonometric arc tangent of this Real ,
in the range -π/2 to π/2. |
void |
atan2(Real x)
Calculates the trigonometric arc tangent of this
Real divided by x , in the range -π
to π. |
void |
atanh()
Calculates the hyperbolic arc tangent of this Real . |
void |
bic(Real a)
Calculates the value of this Real AND NOT
a . |
void |
cbrt()
Calculates the cube root of this Real . |
void |
ceil()
Calculates the smallest (closest to negative infinity)
Real value that is greater than or equal to this
Real and is equal to a mathematical integer. |
void |
copysign(Real a)
Copies the sign from a . |
void |
cos()
Calculates the trigonometric cosine of this Real . |
void |
cosh()
Calculates the hyperbolic cosine of this Real . |
void |
date()
Assigns this Real the current date. |
void |
div(int a)
Calculates the quotient of this Real and the integer
a . |
void |
div(Real a)
Calculates the quotient of this Real and a . |
void |
divf(Real a)
Calculates the mathematical integer that is less than or equal to
this Real divided by a . |
boolean |
equals(Object a)
Returns true if this Java object is the same
object as a . |
boolean |
equalTo(int a)
Returns true if this Real is equal to
the integer a . |
boolean |
equalTo(Real a)
Returns true if this Real is equal to
a . |
void |
erfc()
Calculates the complementary error function for this Real . |
void |
exp()
Calculates e raised to the power of this Real . |
void |
exp10()
Calculates 10 raised to the power of this Real . |
void |
exp2()
Calculates 2 raised to the power of this Real . |
void |
fact()
Calculates the factorial of this Real . |
void |
floor()
Calculates the largest (closest to positive infinity)
Real value that is less than or equal to this
Real and is equal to a mathematical integer. |
void |
frac()
Calculates the fractional part of this Real by subtracting
the closest value towards zero that is equal to a mathematical integer. |
void |
fromDHMS()
Converts this Real from "days, hours, minutes and
seconds" to "hours". |
void |
gamma()
Calculates the gamma function for this Real . |
boolean |
greaterEqual(int a)
Returns true if this Real is greater than
or equal to the integer a . |
boolean |
greaterEqual(Real a)
Returns true if this Real is greater than
or equal to a . |
boolean |
greaterThan(int a)
Returns true if this Real is greater than
the integer a . |
boolean |
greaterThan(Real a)
Returns true if this Real is greater than
a . |
void |
hypot(Real a)
Calculates sqrt(this*this+a*a) . |
void |
inverfc()
Calculates the inverse complementary error function for this
Real . |
boolean |
isFinite()
Returns true if the value of this Real is
finite, false otherwise. |
boolean |
isFiniteNonZero()
Returns true if the value of this Real is
finite and nonzero, false otherwise. |
boolean |
isInfinity()
Returns true if the value of this Real is
infinite, false otherwise. |
boolean |
isIntegral()
Returns true if the value of this Real
represents a mathematical integer. |
boolean |
isNan()
Returns true if the value of this Real is
Not-a-Number (NaN), false otherwise. |
boolean |
isNegative()
Returns true if the value of this Real is
negative, false otherwise. |
boolean |
isOdd()
Returns true if the mathematical integer represented
by this Real is odd. |
boolean |
isZero()
Returns true if the value of this Real is
zero, false otherwise. |
boolean |
lessEqual(int a)
Returns true if this Real is less than or
equal to the integer a . |
boolean |
lessEqual(Real a)
Returns true if this Real is less than or
equal to a . |
boolean |
lessThan(int a)
Returns true if this Real is less than
the integer a . |
boolean |
lessThan(Real a)
Returns true if this Real is less than
a . |
void |
ln()
Calculates the natural logarithm (base-e) of this
Real . |
void |
log10()
Calculates the base-10 logarithm of this Real . |
void |
log2()
Calculates the base-2 logarithm of this Real . |
int |
lowPow10()
Calculates the closest power of 10 that is less than or equal to this
Real . |
void |
makeInfinity(int s)
Makes this Real the value of infinity with the specified
sign. |
void |
makeNan()
Makes this Real the value of Not-a-Number (NaN). |
void |
makeZero()
Makes this Real the value of positive zero. |
void |
makeZero(int s)
Makes this Real the value of zero with the specified sign. |
void |
mod(Real a)
Calculates the value of this Real modulo a . |
void |
mul(int a)
Calculates the product of this Real and the integer
a . |
void |
mul(Real a)
Calculates the product of this Real and a . |
long |
mul128(long extra,
Real a,
long aExtra)
Calculates the product of this Real and a with
extended precision. |
void |
neg()
Negates this Real . |
void |
nextafter(Real a)
Calculates the next representable neighbour of this Real
in the direction towards a . |
void |
normalize()
Readjusts the mantissa of this Real . |
long |
normalize128(long extra)
Readjusts the mantissa of a Real with extended
precision. |
boolean |
notEqualTo(int a)
Returns true if this Real is not equal to
the integer a . |
boolean |
notEqualTo(Real a)
Returns true if this Real is not equal to
a . |
void |
nroot(Real n)
Calculates the n'th root of this Real . |
void |
or(Real a)
Calculates the logical OR of this Real and
a . |
void |
pow(int a)
Calculates the value of this Real raised to the power of
the integer a . |
void |
pow(Real a)
Calculates the value of this Real raised to the power of
a . |
void |
random()
Calculates a pseudorandom number in the range [0, 1). |
void |
rdiv(int a)
Calculates the quotient of the integer a and this
Real . |
void |
rdiv(Real a)
Calculates the quotient of a and this Real . |
void |
recip()
Calculates the reciprocal of this Real . |
long |
recip128(long extra)
Calculates the reciprocal of this Real with
extended precision. |
void |
round()
Rounds this Real value to the closest value that is equal
to a mathematical integer. |
void |
roundFrom128(long extra)
Rounds an extended precision Real to the nearest
Real of normal precision. |
void |
rsqrt()
Calculates the reciprocal square root of this Real . |
void |
scalbn(int n)
Multiplies this Real by 2 to the power of n . |
void |
sin()
Calculates the trigonometric sine of this Real . |
void |
sinh()
Calculates the hyperbolic sine of this Real . |
void |
sqr()
Calculates the square of this Real . |
void |
sqrt()
Calculates the square root of this Real . |
void |
sub(int a)
Calculates the difference between this Real and the
integer a . |
void |
sub(Real a)
Calculates the difference between this Real and
a . |
void |
swap(Real a)
Exchanges the contents of this Real and a . |
void |
tan()
Calculates the trigonometric tangent of this Real . |
void |
tanh()
Calculates the hyperbolic tangent of this Real . |
void |
time()
Assigns this Real the current time. |
void |
toBytes(byte[] data,
int offset)
Encodes an accurate representation of this Real value into
twelve consecutive bytes in a byte array. |
void |
toDHMS()
Converts this Real from "hours" to "days, hours,
minutes and seconds". |
long |
toDoubleBits()
Returns a representation of this Real according to the
IEEE 754 floating-point "double format" bit layout. |
int |
toFloatBits()
Returns a representation of this Real according to the
IEEE 754 floating-point "single format" bit layout. |
int |
toInteger()
Converts this Real value to the closest int
value towards zero. |
long |
toLong()
Converts this Real value to the closest long
value towards zero. |
String |
toString()
Converts this Real to a String using
the default NumberFormat . |
String |
toString(int base)
Converts this Real to a String using
the default NumberFormat with base set
according to the argument. |
String |
toString(Real.NumberFormat format)
Converts this Real to a String using
the given NumberFormat . |
void |
trunc()
Truncates this Real value to the closest value towards
zero that is equal to a mathematical integer. |
void |
xor(Real a)
Calculates the logical XOR of this Real and
a . |
mantissa
public long mantissa
- The mantissa of a
Real
. To maintain numbers in a
normalized state and to preserve the integrity of abnormal numbers, it
is discouraged to modify the inner representation of a
Real
directly.
The number represented by a Real
equals:
-1sign · mantissa · 2-62 · 2exponent-0x40000000
The normalized mantissa of a finite Real
must be
between 0x4000000000000000L
and
0x7fffffffffffffffL
. Using a denormalized
Real
in any operation other than normalize()
may produce undefined results. The mantissa of zero and
of an infinite value is 0x0000000000000000L
.
The mantissa of a NaN is any nonzero value. However, it is
recommended to use the value 0x4000000000000000L
. Any
other values are reserved for future extensions.
exponent
public int exponent
- The exponent of a
Real
. To maintain numbers in a
normalized state and to preserve the integrity of abnormal numbers, it
is discouraged to modify the inner representation of a
Real
directly.
The exponent of a finite Real
must be between
0x00000000
and 0x7fffffff
. The exponent of
zero 0x00000000
.
The exponent of an infinite value and of a NaN is any negative
value. However, it is recommended to use the value
0x80000000
. Any other values are reserved for future
extensions.
sign
public byte sign
- The sign of a
Real
. To maintain numbers in a normalized
state and to preserve the integrity of abnormal numbers, it is
discouraged to modify the inner representation of a Real
directly.
The sign of a finite, zero or infinite Real
is 0 for
positive values and 1 for negative values. Any other values may produce
undefined results.
The sign of a NaN is ignored. However, it is recommended to use the
value 0
. Any other values are reserved for future
extensions.
magicRounding
public static boolean magicRounding
- Set to
false
during numerical algorithms to favor accuracy
over prettyness. This flag is initially set to true
.
The flag controls the operation of a subtraction of two
almost-identical numbers that differ only in the last three bits of the
mantissa. With this flag enabled, the result of such a subtraction is
rounded down to zero. Probabilistically, this is the correct course of
action in an overwhelmingly large percentage of calculations.
However, certain numerical algorithms such as differentiation depend
on keeping maximum accuracy during subtraction.
Note, that because of magicRounding
,
a.sub(b)
may produce zero even though
a.equalTo(b)
returns false
. This must be
considered e.g. when trying to avoid division by zero.
ZERO
public static final Real ZERO
- A
Real
constant holding the exact value of 0. Among other
uses, this value is used as a result when a positive underflow occurs.
ONE
public static final Real ONE
- A
Real
constant holding the exact value of 1.
TWO
public static final Real TWO
- A
Real
constant holding the exact value of 2.
THREE
public static final Real THREE
- A
Real
constant holding the exact value of 3.
FIVE
public static final Real FIVE
- A
Real
constant holding the exact value of 5.
TEN
public static final Real TEN
- A
Real
constant holding the exact value of 10.
HUNDRED
public static final Real HUNDRED
- A
Real
constant holding the exact value of 100.
HALF
public static final Real HALF
- A
Real
constant holding the exact value of 1/2.
THIRD
public static final Real THIRD
- A
Real
constant that is closer than any other to 1/3.
TENTH
public static final Real TENTH
- A
Real
constant that is closer than any other to 1/10.
PERCENT
public static final Real PERCENT
- A
Real
constant that is closer than any other to 1/100.
SQRT2
public static final Real SQRT2
- A
Real
constant that is closer than any other to the
square root of 2.
SQRT1_2
public static final Real SQRT1_2
- A
Real
constant that is closer than any other to the
square root of 1/2.
PI2
public static final Real PI2
- A
Real
constant that is closer than any other to 2π.
PI
public static final Real PI
- A
Real
constant that is closer than any other to π, the
ratio of the circumference of a circle to its diameter.
PI_2
public static final Real PI_2
- A
Real
constant that is closer than any other to π/2.
PI_4
public static final Real PI_4
- A
Real
constant that is closer than any other to π/4.
PI_8
public static final Real PI_8
- A
Real
constant that is closer than any other to π/8.
E
public static final Real E
- A
Real
constant that is closer than any other to e,
the base of the natural logarithms.
LN2
public static final Real LN2
- A
Real
constant that is closer than any other to the
natural logarithm of 2.
LN10
public static final Real LN10
- A
Real
constant that is closer than any other to the
natural logarithm of 10.
LOG2E
public static final Real LOG2E
- A
Real
constant that is closer than any other to the
base-2 logarithm of e.
LOG10E
public static final Real LOG10E
- A
Real
constant that is closer than any other to the
base-10 logarithm of e.
MAX
public static final Real MAX
- A
Real
constant holding the maximum non-infinite positive
number = 4.197e323228496.
MIN
public static final Real MIN
- A
Real
constant holding the minimum non-zero positive
number = 2.383e-323228497.
NAN
public static final Real NAN
- A
Real
constant holding the value of NaN (not-a-number).
This value is always used as a result to signal an invalid operation.
INF
public static final Real INF
- A
Real
constant holding the value of positive infinity.
This value is always used as a result to signal a positive overflow.
INF_N
public static final Real INF_N
- A
Real
constant holding the value of negative infinity.
This value is always used as a result to signal a negative overflow.
ZERO_N
public static final Real ZERO_N
- A
Real
constant holding the value of negative zero. This
value is used as a result e.g. when a negative underflow occurs.
ONE_N
public static final Real ONE_N
- A
Real
constant holding the exact value of -1.
randSeedA
public static long randSeedA
- The seed of the first 64-bit CRC generator of the random
routine. Set this value to control the generated sequence of random
numbers. Should never be set to 0. See
random()
.
Initialized to mantissa of pi.
randSeedB
public static long randSeedB
- The seed of the second 64-bit CRC generator of the random
routine. Set this value to control the generated sequence of random
numbers. Should never be set to 0. See
random()
.
Initialized to mantissa of e.
hexChar
public static final String hexChar
- This string holds the only valid characters to use in hexadecimal
numbers. Equals
"0123456789ABCDEF"
.
See assign(String,int)
.
- See Also:
- Constant Field Values
Real
public Real()
- Creates a new
Real
with a value of zero.
Real
public Real(Real a)
- Creates a new
Real
, assigning the value of another
Real
. See assign(Real)
.
- Parameters:
a
- the Real
to assign.
Real
public Real(int a)
- Creates a new
Real
, assigning the value of an integer. See
assign(int)
.
- Parameters:
a
- the int
to assign.
Real
public Real(long a)
- Creates a new
Real
, assigning the value of a long
integer. See assign(long)
.
- Parameters:
a
- the long
to assign.
Real
public Real(String a)
- Creates a new
Real
, assigning the value encoded in a
String
using base-10. See assign(String)
.
- Parameters:
a
- the String
to assign.
Real
public Real(String a,
int base)
- Creates a new
Real
, assigning the value encoded in a
String
using the specified number base. See assign(String,int)
.
- Parameters:
a
- the String
to assign.base
- the number base of a
. Valid base values are 2,
8, 10 and 16.
Real
public Real(int s,
int e,
long m)
- Creates a new
Real
, assigning a value by directly setting
the fields of the internal representation. The arguments must represent
a valid, normalized Real
. This is the fastest way of
creating a constant value. See assign(int,int,long)
.
- Parameters:
s
- sign
bit, 0 for positive sign, 1 for negative signe
- exponent
m
- mantissa
Real
public Real(byte[] data,
int offset)
- Creates a new
Real
, assigning the value previously encoded
into twelve consecutive bytes in a byte array using toBytes
. See assign(byte[],int)
.
- Parameters:
data
- byte array to decode into this Real
.offset
- offset to start encoding from. The bytes
data[offset]...data[offset+11]
will be
read.
assign
public void assign(Real a)
- Assigns this
Real
the value of another Real
.
Equivalent double code: |
this = a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.3
|
- Parameters:
a
- the Real
to assign.
assign
public void assign(int a)
- Assigns this
Real
the value of an integer.
All integer values can be represented without loss of accuracy.
Equivalent double code: |
this = (double)a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.6
|
- Parameters:
a
- the int
to assign.
assign
public void assign(long a)
- Assigns this
Real
the value of a signed long integer.
All long values can be represented without loss of accuracy.
Equivalent double code: |
this = (double)a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the long
to assign.
assign
public void assign(String a)
- Assigns this
Real
a value encoded in a String
using base-10, as specified in assign(String,int)
.
Equivalent double code: |
this = Double.valueOf (a);
|
Approximate error bound: |
˝-1 ULPs
|
Execution time relative to add:
|
80
|
- Parameters:
a
- the String
to assign.
assign
public void assign(String a,
int base)
- Assigns this
Real
a value encoded in a String
using the specified number base. The string is parsed as follows:
- If the string is
null
or an empty string, zero is
assigned.
- Leading spaces are ignored.
- An optional sign, '+', '-' or '/', where '/' precedes a negative
two's-complement number, reading: "an infinite number of 1-bits
preceding the number".
- Optional digits preceding the radix, in the specified base.
- In base-2, allowed digits are '01'.
- In base-8, allowed digits are '01234567'.
- In base-10, allowed digits are '0123456789'.
- In base-16, allowed digits are '0123456789ABCDEF'.
- An optional radix character, '.' or ','.
- Optional digits following the radix.
- The following spaces are ignored.
- An optional exponent indicator, 'e'. If not base-16, or after a
space, 'E' is also accepted.
- An optional sign, '+' or '-'.
- Optional exponent digits in base-10.
Valid examples:
base-2: "-.110010101e+5"
base-8: "+5462E-99"
base-10: " 3,1415927"
base-16: "/FFF800C.CCCE e64"
The number is parsed until the end of the string or an unknown
character is encountered, then silently returns even if the whole
string has not been parsed. Please note that specifying an
excessive number of digits in base-10 may in fact decrease the
accuracy of the result because of the extra multiplications performed.
Equivalent double code: |
this = Double.valueOf (a);
// Works only for base-10
|
Approximate error bound:
| base-10 |
˝-1 ULPs
|
2/8/16 |
˝ ULPs
|
Execution time relative to add:
| base-2 |
54
|
base-8 |
60
|
base-10 |
80
|
base-16 |
60
|
- Parameters:
a
- the String
to assign.base
- the number base of a
. Valid base values are
2, 8, 10 and 16.
assign
public void assign(int s,
int e,
long m)
- Assigns this
Real
a value by directly setting the fields
of the internal representation. The arguments must represent a valid,
normalized Real
. This is the fastest way of assigning a
constant value.
Equivalent double code: |
this = (1-2*s) * m *
Math.pow (2.0,e-0x400000e3);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.3
|
- Parameters:
s
- sign
bit, 0 for positive sign, 1 for negative signe
- exponent
m
- mantissa
assign
public void assign(byte[] data,
int offset)
- Assigns this
Real
a value previously encoded into into
twelve consecutive bytes in a byte array using toBytes
.
Error bound: |
0 ULPs
|
Execution time relative to add:
|
1.2
|
- Parameters:
data
- byte array to decode into this Real
.offset
- offset to start encoding from. The bytes
data[offset]...data[offset+11]
will be
read.
toBytes
public void toBytes(byte[] data,
int offset)
- Encodes an accurate representation of this
Real
value into
twelve consecutive bytes in a byte array. Can be decoded using assign(byte[],int)
.
Execution time relative to add:
|
1.2
|
- Parameters:
data
- byte array to save this Real
in.offset
- offset to start encoding to. The bytes
data[offset]...data[offset+11]
will be
written.
assignFloatBits
public void assignFloatBits(int bits)
- Assigns this
Real
the value corresponding to a given bit
representation. The argument is considered to be a representation of a
floating-point value according to the IEEE 754 floating-point "single
format" bit layout.
Equivalent float code: |
this = Float.intBitsToFloat (bits);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.6
|
- Parameters:
bits
- a float
value encoded in an int
.
assignDoubleBits
public void assignDoubleBits(long bits)
- Assigns this
Real
the value corresponding to a given bit
representation. The argument is considered to be a representation of a
floating-point value according to the IEEE 754 floating-point "double
format" bit layout.
Equivalent double code: |
this = Double.longBitsToDouble (bits);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.6
|
- Parameters:
bits
- a double
value encoded in a long
.
toFloatBits
public int toFloatBits()
- Returns a representation of this
Real
according to the
IEEE 754 floating-point "single format" bit layout.
Equivalent float code: |
Float.floatToIntBits (this)
|
Execution time relative to add:
|
0.7
|
- Returns:
- the bits that represent the floating-point number.
toDoubleBits
public long toDoubleBits()
- Returns a representation of this
Real
according to the
IEEE 754 floating-point "double format" bit layout.
Equivalent double code: |
Double.doubleToLongBits (this)
|
Execution time relative to add:
|
0.7
|
- Returns:
- the bits that represent the floating-point number.
makeZero
public void makeZero()
- Makes this
Real
the value of positive zero.
Equivalent double code: |
this = 0;
|
Execution time relative to add:
|
0.2
|
makeZero
public void makeZero(int s)
- Makes this
Real
the value of zero with the specified sign.
Equivalent double code: |
this = 0.0 * (1-2*s);
|
Execution time relative to add:
|
0.2
|
- Parameters:
s
- sign bit, 0 to make a positive zero, 1 to make a negative zero
makeInfinity
public void makeInfinity(int s)
- Makes this
Real
the value of infinity with the specified
sign.
Equivalent double code: |
this = Double.POSITIVE_INFINITY
* (1-2*s);
|
Execution time relative to add:
|
0.3
|
- Parameters:
s
- sign bit, 0 to make positive infinity, 1 to make negative
infinity
makeNan
public void makeNan()
- Makes this
Real
the value of Not-a-Number (NaN).
Equivalent double code: |
this = Double.NaN ;
|
Execution time relative to add:
|
0.3
|
isZero
public boolean isZero()
- Returns
true
if the value of this Real
is
zero, false
otherwise.
Equivalent double code: |
(this == 0)
|
Execution time relative to add:
|
0.3
|
- Returns:
true
if the value represented by this object is
zero, false
otherwise.
isInfinity
public boolean isInfinity()
- Returns
true
if the value of this Real
is
infinite, false
otherwise.
Equivalent double code: |
Double.isInfinite (this)
|
Execution time relative to add:
|
0.3
|
- Returns:
true
if the value represented by this object is
infinite, false
if it is finite or NaN.
isNan
public boolean isNan()
- Returns
true
if the value of this Real
is
Not-a-Number (NaN), false
otherwise.
Equivalent double code: |
Double.isNaN (this)
|
Execution time relative to add:
|
0.3
|
- Returns:
true
if the value represented by this object is
NaN, false
otherwise.
isFinite
public boolean isFinite()
- Returns
true
if the value of this Real
is
finite, false
otherwise.
Equivalent double code: |
(!Double.isNaN (this) &&
!Double.isInfinite (this))
|
Execution time relative to add:
|
0.3
|
- Returns:
true
if the value represented by this object is
finite, false
if it is infinite or NaN.
isFiniteNonZero
public boolean isFiniteNonZero()
- Returns
true
if the value of this Real
is
finite and nonzero, false
otherwise.
Equivalent double code: |
(!Double.isNaN (this) &&
!Double.isInfinite (this) &&
(this!=0))
|
Execution time relative to add:
|
0.3
|
- Returns:
true
if the value represented by this object is
finite and nonzero, false
if it is infinite, NaN or
zero.
isNegative
public boolean isNegative()
- Returns
true
if the value of this Real
is
negative, false
otherwise.
Equivalent double code: |
(this < 0)
|
Execution time relative to add:
|
0.3
|
- Returns:
true
if the value represented by this object
is negative, false
if it is positive or NaN.
abs
public void abs()
- Calculates the absolute value.
Replaces the contents of this
Real
with the result.
Equivalent double code: |
this = Math.abs (this);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.2
|
neg
public void neg()
- Negates this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = -this;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.2
|
copysign
public void copysign(Real a)
- Copies the sign from
a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.abs (this)*Math.signum (a);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.2
|
- Parameters:
a
- the Real
to copy the sign from.
normalize
public void normalize()
- Readjusts the mantissa of this
Real
. The exponent is
adjusted accordingly. This is necessary when the mantissa has been
modified directly
for some purpose and may be
denormalized. The normalized mantissa of a finite Real
must have bit 63 cleared and bit 62 set. Using a denormalized
Real
in any other operation may produce undefined
results.
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
0.7
|
normalize128
public long normalize128(long extra)
- Readjusts the mantissa of a
Real
with extended
precision. The exponent is adjusted accordingly. This is necessary when
the mantissa has been modified directly
for some
purpose and may be denormalized. The normalized mantissa of a finite
Real
must have bit 63 cleared and bit 62 set. Using a
denormalized Real
in any other operation may
produce undefined results.
Approximate error bound: |
2-64 ULPs (i.e. of a normal precision Real )
|
Execution time relative to add:
|
0.7
|
- Parameters:
extra
- the extra 64 bits of mantissa of this extended precision
Real
.
- Returns:
- the extra 64 bits of mantissa of the resulting extended
precision
Real
.
roundFrom128
public void roundFrom128(long extra)
- Rounds an extended precision
Real
to the nearest
Real
of normal precision. Replaces the contents of this
Real
with the result.
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
1.0
|
- Parameters:
extra
- the extra 64 bits of mantissa of this extended precision
Real
.
equals
public boolean equals(Object a)
- Returns
true
if this Java object is the same
object as a
. Since a Real
should be
thought of as a "register holding a number", this method compares the
object references, not the contents of the two objects.
This is very different from equalTo(Real)
.
- Overrides:
equals
in class Object
- Parameters:
a
- the object to compare to this.
- Returns:
true
if this object is the same as a
.
equalTo
public boolean equalTo(Real a)
- Returns
true
if this Real
is equal to
a
.
If the numbers are incomparable, i.e. the values are infinities of
the same sign or any of them is NaN, false
is always
returned. This method must not be confused with equals(Object)
.
Equivalent double code: |
(this == a)
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the value represented by this object is
equal to the value represented by a
. false
otherwise, or if the numbers are incomparable.
equalTo
public boolean equalTo(int a)
- Returns
true
if this Real
is equal to
the integer a
.
Equivalent double code: |
(this == a)
|
Execution time relative to add:
|
1.7
|
- Parameters:
a
- the int
to compare to this.
- Returns:
true
if the value represented by this object is
equal to the integer a
. false
otherwise.
notEqualTo
public boolean notEqualTo(Real a)
- Returns
true
if this Real
is not equal to
a
.
If the numbers are incomparable, i.e. the values are infinities of
the same sign or any of them is NaN, false
is always
returned.
This distinguishes notEqualTo(a)
from the expression
!equalTo(a)
.
Equivalent double code: |
(this != a)
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the value represented by this object is not
equal to the value represented by a
. false
otherwise, or if the numbers are incomparable.
notEqualTo
public boolean notEqualTo(int a)
- Returns
true
if this Real
is not equal to
the integer a
.
If this Real
is NaN, false
is always
returned.
This distinguishes notEqualTo(a)
from the expression
!equalTo(a)
.
Equivalent double code: |
(this != a)
|
Execution time relative to add:
|
1.7
|
- Parameters:
a
- the int
to compare to this.
- Returns:
true
if the value represented by this object is not
equal to the integer a
. false
otherwise, or if this Real
is NaN.
lessThan
public boolean lessThan(Real a)
- Returns
true
if this Real
is less than
a
.
If the numbers are incomparable, i.e. the values are infinities of
the same sign or any of them is NaN, false
is always
returned.
Equivalent double code: |
(this < a)
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the value represented by this object is
less than the value represented by a
.
false
otherwise, or if the numbers are incomparable.
lessThan
public boolean lessThan(int a)
- Returns
true
if this Real
is less than
the integer a
.
If this Real
is NaN, false
is always
returned.
Equivalent double code: |
(this < a)
|
Execution time relative to add:
|
1.7
|
- Parameters:
a
- the int
to compare to this.
- Returns:
true
if the value represented by this object is
less than the integer a
. false
otherwise,
or if this Real
is NaN.
lessEqual
public boolean lessEqual(Real a)
- Returns
true
if this Real
is less than or
equal to a
.
If the numbers are incomparable, i.e. the values are infinities of
the same sign or any of them is NaN, false
is always
returned.
Equivalent double code: |
(this <= a)
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the value represented by this object is
less than or equal to the value represented by a
.
false
otherwise, or if the numbers are incomparable.
lessEqual
public boolean lessEqual(int a)
- Returns
true
if this Real
is less than or
equal to the integer a
.
If this Real
is NaN, false
is always
returned.
Equivalent double code: |
(this <= a)
|
Execution time relative to add:
|
1.7
|
- Parameters:
a
- the int
to compare to this.
- Returns:
true
if the value represented by this object is
less than or equal to the integer a
. false
otherwise, or if this Real
is NaN.
greaterThan
public boolean greaterThan(Real a)
- Returns
true
if this Real
is greater than
a
.
If the numbers are incomparable, i.e. the values are infinities of
the same sign or any of them is NaN, false
is always
returned.
Equivalent double code: |
(this > a)
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the value represented by this object is
greater than the value represented by a
.
false
otherwise, or if the numbers are incomparable.
greaterThan
public boolean greaterThan(int a)
- Returns
true
if this Real
is greater than
the integer a
.
If this Real
is NaN, false
is always
returned.
Equivalent double code: |
(this > a)
|
Execution time relative to add:
|
1.7
|
- Parameters:
a
- the int
to compare to this.
- Returns:
true
if the value represented by this object is
greater than the integer a
.
false
otherwise, or if this Real
is NaN.
greaterEqual
public boolean greaterEqual(Real a)
- Returns
true
if this Real
is greater than
or equal to a
.
If the numbers are incomparable, i.e. the values are infinities of
the same sign or any of them is NaN, false
is always
returned.
Equivalent double code: |
(this >= a)
|
Execution time relative to add:
|
1.0
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the value represented by this object is
greater than or equal to the value represented by a
.
false
otherwise, or if the numbers are incomparable.
greaterEqual
public boolean greaterEqual(int a)
- Returns
true
if this Real
is greater than
or equal to the integer a
.
If this Real
is NaN, false
is always
returned.
Equivalent double code: |
(this >= a)
|
Execution time relative to add:
|
1.7
|
- Parameters:
a
- the int
to compare to this.
- Returns:
true
if the value represented by this object is
greater than or equal to the integer a
.
false
otherwise, or if this Real
is NaN.
absLessThan
public boolean absLessThan(Real a)
- Returns
true
if the absolute value of this
Real
is less than the absolute value of
a
.
If the numbers are incomparable, i.e. the values are both infinite
or any of them is NaN, false
is always returned.
Equivalent double code: |
(Math.abs (this) <
Math.abs (a))
|
Execution time relative to add:
|
0.5
|
- Parameters:
a
- the Real
to compare to this.
- Returns:
true
if the absolute of the value represented by
this object is less than the absolute of the value represented by
a
.
false
otherwise, or if the numbers are incomparable.
scalbn
public void scalbn(int n)
- Multiplies this
Real
by 2 to the power of n
.
Replaces the contents of this Real
with the result.
This operation is faster than normal multiplication since it only
involves adding to the exponent.
Equivalent double code: |
this *= Math.pow (2.0,n);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.3
|
- Parameters:
n
- the integer argument.
nextafter
public void nextafter(Real a)
- Calculates the next representable neighbour of this
Real
in the direction towards a
.
Replaces the contents of this Real
with the result.
If the two values are equal, nothing happens.
Equivalent double code: |
this += Math.ulp (this)*Math.signum (a-this);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.8
|
- Parameters:
a
- the Real
argument.
floor
public void floor()
- Calculates the largest (closest to positive infinity)
Real
value that is less than or equal to this
Real
and is equal to a mathematical integer.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.floor (this);
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
0.5
|
ceil
public void ceil()
- Calculates the smallest (closest to negative infinity)
Real
value that is greater than or equal to this
Real
and is equal to a mathematical integer.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.ceil (this);
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
1.8
|
round
public void round()
- Rounds this
Real
value to the closest value that is equal
to a mathematical integer. If two Real
values that are
mathematical integers are equally close, the result is the integer
value with the largest magnitude (positive or negative). Replaces the
contents of this Real
with the result.
Equivalent double code: |
this = Math.rint (this);
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
1.3
|
trunc
public void trunc()
- Truncates this
Real
value to the closest value towards
zero that is equal to a mathematical integer.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = (double)((long)this);
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
1.2
|
frac
public void frac()
- Calculates the fractional part of this
Real
by subtracting
the closest value towards zero that is equal to a mathematical integer.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this -= (double)((long)this);
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
1.2
|
toInteger
public int toInteger()
- Converts this
Real
value to the closest int
value towards zero.
If the value of this Real
is too large, Integer.MAX_VALUE
is returned. However, if the value of this
Real
is too small, -Integer.MAX_VALUE
is
returned, not Integer.MIN_VALUE
. This is done to ensure that
the sign will be correct if you calculate
-this.toInteger()
. A NaN is converted to 0.
Equivalent double code: |
(int)this
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
0.6
|
- Returns:
- an
int
representation of this Real
.
toLong
public long toLong()
- Converts this
Real
value to the closest long
value towards zero.
If the value of this Real
is too large, Long.MAX_VALUE
is returned. However, if the value of this
Real
is too small, -Long.MAX_VALUE
is
returned, not Long.MIN_VALUE
. This is done to ensure that the
sign will be correct if you calculate -this.toLong()
.
A NaN is converted to 0.
Equivalent double code: |
(long)this
|
Approximate error bound: |
0 ULPs
|
Execution time relative to add:
|
0.5
|
- Returns:
- a
long
representation of this Real
.
isIntegral
public boolean isIntegral()
- Returns
true
if the value of this Real
represents a mathematical integer. If the value is too large to
determine if it is an integer, true
is returned.
Equivalent double code: |
(this == (long)this)
|
Execution time relative to add:
|
0.6
|
- Returns:
true
if the value represented by this object
represents a mathematical integer, false
otherwise.
isOdd
public boolean isOdd()
- Returns
true
if the mathematical integer represented
by this Real
is odd. You must first determine
that the value is actually an integer using isIntegral()
. If the value is too large to determine if the
integer is odd, false
is returned.
Equivalent double code: |
((((long)this)&1) == 1)
|
Execution time relative to add:
|
0.6
|
- Returns:
true
if the mathematical integer represented by
this Real
is odd, false
otherwise.
swap
public void swap(Real a)
- Exchanges the contents of this
Real
and a
.
Equivalent double code: |
tmp=this; this=a; a=tmp;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
0.5
|
- Parameters:
a
- the Real
to exchange with this.
add
public void add(Real a)
- Calculates the sum of this
Real
and a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this += a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
«« 1.0 »»
|
- Parameters:
a
- the Real
to add to this.
add
public void add(int a)
- Calculates the sum of this
Real
and the integer
a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this += a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
1.8
|
- Parameters:
a
- the int
to add to this.
add128
public long add128(long extra,
Real a,
long aExtra)
- Calculates the sum of this
Real
and a
with
extended precision. Replaces the contents of this Real
with the result. Returns the extra mantissa of the extended precision
result.
An extra 64 bits of mantissa is added to both arguments for extended
precision. If any of the arguments are not of extended precision, use
0
for the extra mantissa.
Extended prevision can be useful in many situations. For instance,
when accumulating a lot of very small values it is advantageous for the
accumulator to have extended precision. To convert the extended
precision value back to a normal Real
for further
processing, use roundFrom128(long)
.
Equivalent double code: |
this += a;
|
Approximate error bound: |
2-62 ULPs (i.e. of a normal precision Real )
|
Execution time relative to add:
|
2.0
|
- Parameters:
extra
- the extra 64 bits of mantissa of this extended precision
Real
.a
- the Real
to add to this.aExtra
- the extra 64 bits of mantissa of the extended precision
value a
.
- Returns:
- the extra 64 bits of mantissa of the resulting extended
precision
Real
.
sub
public void sub(Real a)
- Calculates the difference between this
Real
and
a
.
Replaces the contents of this Real
with the result.
(To achieve extended precision subtraction, it is enough to call
a.neg
()
before calling add128
(extra,a,aExtra)
, since only
the sign bit of a
need to be changed.)
Equivalent double code: |
this -= a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
2.0
|
- Parameters:
a
- the Real
to subtract from this.
sub
public void sub(int a)
- Calculates the difference between this
Real
and the
integer a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this -= a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
2.4
|
- Parameters:
a
- the int
to subtract from this.
mul
public void mul(Real a)
- Calculates the product of this
Real
and a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this *= a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
1.3
|
- Parameters:
a
- the Real
to multiply to this.
mul
public void mul(int a)
- Calculates the product of this
Real
and the integer
a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this *= a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
1.3
|
- Parameters:
a
- the int
to multiply to this.
mul128
public long mul128(long extra,
Real a,
long aExtra)
- Calculates the product of this
Real
and a
with
extended precision.
Replaces the contents of this Real
with the result.
Returns the extra mantissa of the extended precision result.
An extra 64 bits of mantissa is added to both arguments for
extended precision. If any of the arguments are not of extended
precision, use 0
for the extra mantissa. See also add128(long,Real,long)
.
Equivalent double code: |
this *= a;
|
Approximate error bound: |
2-60 ULPs
|
Execution time relative to add:
|
3.1
|
- Parameters:
extra
- the extra 64 bits of mantissa of this extended precision
Real
.a
- the Real
to multiply to this.aExtra
- the extra 64 bits of mantissa of the extended precision
value a
.
- Returns:
- the extra 64 bits of mantissa of the resulting extended
precision
Real
.
sqr
public void sqr()
- Calculates the square of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = this*this;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
1.1
|
div
public void div(Real a)
- Calculates the quotient of this
Real
and a
.
Replaces the contents of this Real
with the result.
(To achieve extended precision division, call
aExtra=a.recip128
(aExtra)
before
calling mul128
(extra,a,aExtra)
.)
Equivalent double code: |
this /= a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
2.6
|
- Parameters:
a
- the Real
to divide this with.
div
public void div(int a)
- Calculates the quotient of this
Real
and the integer
a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this /= a;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
2.6
|
- Parameters:
a
- the int
to divide this with.
rdiv
public void rdiv(Real a)
- Calculates the quotient of
a
and this Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = a/this;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
3.1
|
- Parameters:
a
- the Real
to be divided by this.
rdiv
public void rdiv(int a)
- Calculates the quotient of the integer
a
and this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = a/this;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
3.9
|
- Parameters:
a
- the int
to be divided by this.
recip
public void recip()
- Calculates the reciprocal of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = 1/this;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
2.3
|
recip128
public long recip128(long extra)
- Calculates the reciprocal of this
Real
with
extended precision.
Replaces the contents of this Real
with the result.
Returns the extra mantissa of the extended precision result.
An extra 64 bits of mantissa is added for extended precision.
If the argument is not of extended precision, use 0
for the extra mantissa. See also add128(long,Real,long)
.
Equivalent double code: |
this = 1/this;
|
Approximate error bound: |
2-60 ULPs
|
Execution time relative to add:
|
17
|
- Parameters:
extra
- the extra 64 bits of mantissa of this extended precision
Real
.
- Returns:
- the extra 64 bits of mantissa of the resulting extended
precision
Real
.
divf
public void divf(Real a)
- Calculates the mathematical integer that is less than or equal to
this
Real
divided by a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.floor (this/a);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
22
|
- Parameters:
a
- the Real
argument.
mod
public void mod(Real a)
- Calculates the value of this
Real
modulo a
.
Replaces the contents of this Real
with the result.
The modulo in this case is defined as the remainder after subtracting
a
multiplied by the mathematical integer that is less than
or equal to this Real
divided by a
.
Equivalent double code: |
this = this -
a*Math.floor (this/a);
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
27
|
- Parameters:
a
- the Real
argument.
and
public void and(Real a)
- Calculates the logical AND of this
Real
and
a
.
Replaces the contents of this Real
with the result.
Semantics of bitwise logical operations exactly mimic those of
Java's bitwise integer operators. In these operations, the
internal binary representation of the numbers are used. If the
values represented by the operands are not mathematical
integers, the fractional bits are also included in the operation.
Negative numbers are interpreted as two's-complement,
generalized to real numbers: Negating the number inverts all
bits, including an infinite number of 1-bits before the radix
point and an infinite number of 1-bits after the radix point. The
infinite number of 1-bits after the radix is rounded upwards
producing an infinite number of 0-bits, until the first 0-bit is
encountered which will be switched to a 1 (rounded or not, these
two forms are mathematically equivalent). For example, the number
"1" negated, becomes (in binary form)
...1111110.111111....
Rounding of the infinite
number of 1's after the radix gives the number
...1111111.000000...
, which is exactly the way we
usually see "-1" as two's-complement.
This method calculates a negative value if and only
if this and a
are both negative.
Equivalent int code: |
this &= a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
1.5
|
- Parameters:
a
- the Real
argument
or
public void or(Real a)
- Calculates the logical OR of this
Real
and
a
.
Replaces the contents of this Real
with the result.
See and(Real)
for an explanation of the
interpretation of a Real
in bitwise operations.
This method calculates a negative value if and only
if either this or a
is negative.
Equivalent int code: |
this |= a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
1.6
|
- Parameters:
a
- the Real
argument
xor
public void xor(Real a)
- Calculates the logical XOR of this
Real
and
a
.
Replaces the contents of this Real
with the result.
See and(Real)
for an explanation of the
interpretation of a Real
in bitwise operations.
This method calculates a negative value if and only
if exactly one of this and a
is negative.
The operation NOT has been omitted in this library
because it cannot be generalized to fractional numbers. If this
Real
represents a mathematical integer, the
operation NOT can be calculated as "this XOR -1",
which is equivalent to "this XOR
/FFFFFFFF.0000
".
Equivalent int code: |
this ^= a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
1.5
|
- Parameters:
a
- the Real
argument
bic
public void bic(Real a)
- Calculates the value of this
Real
AND NOT
a
. The opeation is read as "bit clear".
Replaces the contents of this Real
with the result.
See and(Real)
for an explanation of the
interpretation of a Real
in bitwise operations.
This method calculates a negative value if and only
if this is negative and not a
is negative.
Equivalent int code: |
this &= ~a;
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
1.5
|
- Parameters:
a
- the Real
argument
sqrt
public void sqrt()
- Calculates the square root of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.sqrt (this);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
19
|
rsqrt
public void rsqrt()
- Calculates the reciprocal square root of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = 1/Math.sqrt (this);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
21
|
cbrt
public void cbrt()
- Calculates the cube root of this
Real
.
Replaces the contents of this Real
with the result.
The cube root of a negative value is the negative of the cube
root of that value's magnitude.
Equivalent double code: |
this = Math.cbrt (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
32
|
nroot
public void nroot(Real n)
- Calculates the n'th root of this
Real
.
Replaces the contents of this Real
with the result.
For odd integer n, the n'th root of a negative value is the
negative of the n'th root of that value's magnitude.
Equivalent double code: |
this = Math.pow (this,1/a);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
110
|
- Parameters:
n
- the Real
argument.
hypot
public void hypot(Real a)
- Calculates
sqrt(this*this+a*a)
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.hypot (this,a);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
24
|
- Parameters:
a
- the Real
argument.
exp
public void exp()
- Calculates e raised to the power of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.exp (this);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
31
|
exp2
public void exp2()
- Calculates 2 raised to the power of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.exp (this *
Math.log (2));
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
27
|
exp10
public void exp10()
- Calculates 10 raised to the power of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.exp (this *
Math.log (10));
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
31
|
ln
public void ln()
- Calculates the natural logarithm (base-e) of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.log (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
51
|
log2
public void log2()
- Calculates the base-2 logarithm of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.log (this)/Math.log (2);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
51
|
log10
public void log10()
- Calculates the base-10 logarithm of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.log10 (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
53
|
lowPow10
public int lowPow10()
- Calculates the closest power of 10 that is less than or equal to this
Real
.
Replaces the contents of this Real
with the result.
The base-10 exponent of the result is returned.
Equivalent double code: |
int exp = (int)(Math.floor (Math.log10 (this)));
this = Math.pow (10, exp);
return exp;
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
3.6
|
- Returns:
- the base-10 exponent
pow
public void pow(Real a)
- Calculates the value of this
Real
raised to the power of
a
.
Replaces the contents of this Real
with the result.
Special cases:
- if a is 0.0 or -0.0 then result is 1.0
- if a is NaN then result is NaN
- if this is NaN and a is not zero then result is NaN
- if a is 1.0 then result is this
- if |this| > 1.0 and a is +Infinity then result is +Infinity
- if |this| < 1.0 and a is -Infinity then result is +Infinity
- if |this| > 1.0 and a is -Infinity then result is +0
- if |this| < 1.0 and a is +Infinity then result is +0
- if |this| = 1.0 and a is ±Infinity then result is NaN
- if this = +0 and a > 0 then result is +0
- if this = +0 and a < 0 then result is +Inf
- if this = -0 and a > 0, and odd integer then result is -0
- if this = -0 and a < 0, and odd integer then result is -Inf
- if this = -0 and a > 0, not odd integer then result is +0
- if this = -0 and a < 0, not odd integer then result is +Inf
- if this = +Inf and a > 0 then result is +Inf
- if this = +Inf and a < 0 then result is +0
- if this = -Inf and a not integer then result is NaN
- if this = -Inf and a > 0, and odd integer then result is -Inf
- if this = -Inf and a > 0, not odd integer then result is +Inf
- if this = -Inf and a < 0, and odd integer then result is -0
- if this = -Inf and a < 0, not odd integer then result is +0
- if this < 0 and a not integer then result is NaN
- if this < 0 and a odd integer then result is -(|this|a)
- if this < 0 and a not odd integer then result is |this|a
- else result is exp(ln(this)*a)
Equivalent double code: |
this = Math.pow (this, a);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
110
|
- Parameters:
a
- the Real
argument.
pow
public void pow(int a)
- Calculates the value of this
Real
raised to the power of
the integer a
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.pow (this, a);
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
84
|
- Parameters:
a
- the integer argument.
sin
public void sin()
- Calculates the trigonometric sine of this
Real
.
Replaces the contents of this Real
with the result.
The input value is treated as an angle measured in radians.
Equivalent double code: |
this = Math.sin (this);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
28
|
cos
public void cos()
- Calculates the trigonometric cosine of this
Real
.
Replaces the contents of this Real
with the result.
The input value is treated as an angle measured in radians.
Equivalent double code: |
this = Math.cos (this);
|
Approximate error bound: |
1 ULPs
|
Execution time relative to add:
|
37
|
tan
public void tan()
- Calculates the trigonometric tangent of this
Real
.
Replaces the contents of this Real
with the result.
The input value is treated as an angle measured in radians.
Equivalent double code: |
this = Math.tan (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
70
|
asin
public void asin()
- Calculates the trigonometric arc sine of this
Real
,
in the range -π/2 to π/2.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.asin (this);
|
Approximate error bound: |
3 ULPs
|
Execution time relative to add:
|
68
|
acos
public void acos()
- Calculates the trigonometric arc cosine of this
Real
,
in the range 0.0 to π.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.acos (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
67
|
atan
public void atan()
- Calculates the trigonometric arc tangent of this
Real
,
in the range -π/2 to π/2.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.atan (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
37
|
atan2
public void atan2(Real x)
- Calculates the trigonometric arc tangent of this
Real
divided by x
, in the range -π
to π. The signs of both arguments are used to determine the
quadrant of the result. Replaces the contents of this
Real
with the result.
Equivalent double code: |
this = Math.atan2 (this,x);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
48
|
- Parameters:
x
- the Real
argument.
sinh
public void sinh()
- Calculates the hyperbolic sine of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.sinh (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
67
|
cosh
public void cosh()
- Calculates the hyperbolic cosine of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.cosh (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
66
|
tanh
public void tanh()
- Calculates the hyperbolic tangent of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
this = Math.tanh (this);
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
70
|
asinh
public void asinh()
- Calculates the hyperbolic arc sine of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
none
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
77
|
acosh
public void acosh()
- Calculates the hyperbolic arc cosine of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
none
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
75
|
atanh
public void atanh()
- Calculates the hyperbolic arc tangent of this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
none
|
Approximate error bound: |
2 ULPs
|
Execution time relative to add:
|
57
|
fact
public void fact()
- Calculates the factorial of this
Real
.
Replaces the contents of this Real
with the result.
The definition is generalized to all real numbers (not only integers),
by using the fact that (n!)=gamma
(n+1)
.
Equivalent double code: |
none
|
Approximate error bound: |
15 ULPs
|
Execution time relative to add:
|
8-190
|
gamma
public void gamma()
- Calculates the gamma function for this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
none
|
Approximate error bound: |
100+ ULPs
|
Execution time relative to add:
|
190
|
erfc
public void erfc()
- Calculates the complementary error function for this
Real
.
Replaces the contents of this Real
with the result.
The complementary error function is defined as the integral from
x to infinity of 2/√π ·e-t˛ dt. It is
related to the error function, erf, by the formula
erfc(x)=1-erf(x).
Equivalent double code: |
none
|
Approximate error bound: |
219 ULPs
|
Execution time relative to add:
|
80-4900
|
inverfc
public void inverfc()
- Calculates the inverse complementary error function for this
Real
.
Replaces the contents of this Real
with the result.
Equivalent double code: |
none
|
Approximate error bound: |
219 ULPs
|
Execution time relative to add:
|
240-5100
|
toDHMS
public void toDHMS()
- Converts this
Real
from "hours" to "days, hours,
minutes and seconds".
Replaces the contents of this Real
with the result.
The format converted to is encoded into the digits of the
number (in decimal form):
"DDDDhh.mmss
". Here "DDDD
," is number
of days, "hh
" is hours (0-23), "mm
" is
minutes (0-59) and "ss
" is seconds
(0-59). Additional digits represent fractions of a second.
If the number of hours of the input is greater or equal to
8784 (number of hours in year 0
), the format
converted to is instead "YYYYMMDDhh.mmss
". Here
"YYYY
" is the number of years since the imaginary
year 0
in the Gregorian calendar, extrapolated back
from year 1582. "MM
" is the month (1-12) and
"DD
" is the day of the month (1-31). See a thorough
discussion of date calculations here.
Equivalent double code: |
none
|
Approximate error bound: |
?
|
Execution time relative to add:
|
19
|
fromDHMS
public void fromDHMS()
- Converts this
Real
from "days, hours, minutes and
seconds" to "hours".
Replaces the contents of this Real
with the result.
The format converted from is encoded into the digits of the
number (in decimal form):
"DDDDhh.mmss
". Here "DDDD
" is number of
days, "hh
" is hours (0-23), "mm
" is
minutes (0-59) and "ss
" is seconds
(0-59). Additional digits represent fractions of a second.
If the number of days in the input is greater than or equal to
10000, the format converted from is instead
"YYYYMMDDhh.mmss
". Here "YYYY
" is the
number of years since the imaginary year 0
in the
Gregorian calendar, extrapolated back from year
1582. "MM
" is the month (1-12) and
"DD
" is the day of the month (1-31). If month or day
is 0 it is treated as 1. See a thorough discussion of date
calculations here.
Equivalent double code: |
none
|
Approximate error bound: |
?
|
Execution time relative to add:
|
19
|
time
public void time()
- Assigns this
Real
the current time. The time is
encoded into the digits of the number (in decimal form), using the
format "hh.mmss
", where "hh
" is hours,
"mm
" is minutes and "code>ss" is seconds.
Equivalent double code: |
none
|
Error bound: |
˝ ULPs
|
Execution time relative to add:
|
8.9
|
date
public void date()
- Assigns this
Real
the current date. The date is
encoded into the digits of the number (in decimal form), using
the format "YYYYMMDD00
", where "YYYY
"
is the year, "MM
" is the month (1-12) and
"DD
" is the day of the month (1-31). The
"00
" in this format is a sort of padding to make it
compatible with the format used by toDHMS()
and fromDHMS()
.
Equivalent double code: |
none
|
Error bound: |
0 ULPs
|
Execution time relative to add:
|
30
|
accumulateRandomness
public static void accumulateRandomness(long seed)
- Accumulate more randomness into the random number generator, to
decrease the predictability of the output from
random()
. The input should contain data with some form of
inherent randomness e.g. System.currentTimeMillis().
- Parameters:
seed
- some extra randomness for the random number generator.
random
public void random()
- Calculates a pseudorandom number in the range [0, 1).
Replaces the contents of this
Real
with the result.
The algorithm used is believed to be cryptographically secure,
combining two relatively weak 64-bit CRC generators into a strong
generator by skipping bits from one generator whenever the other
generator produces a 0-bit. The algorithm passes the ent test.
Equivalent double code: |
this = Math.random ();
|
Approximate error bound: |
-
|
Execution time relative to add:
|
81
|
toString
public String toString()
- Converts this
Real
to a String
using
the default NumberFormat
.
See NumberFormat
for a description
of the default way that numbers are formatted.
Equivalent double code: |
this.toString()
|
Execution time relative to add:
|
130
|
- Overrides:
toString
in class Object
- Returns:
- a
String
representation of this Real
.
toString
public String toString(int base)
- Converts this
Real
to a String
using
the default NumberFormat
with base
set
according to the argument.
See NumberFormat
for a description
of the default way that numbers are formatted.
Equivalent double code: |
this.toString() // Works only for base-10
|
Execution time relative to add:
| base-2 |
120
|
base-8 |
110
|
base-10 |
130
|
base-16 |
120
|
- Parameters:
base
- the base for the conversion. Valid base values are
2, 8, 10 and 16.
- Returns:
- a
String
representation of this Real
.
toString
public String toString(Real.NumberFormat format)
- Converts this
Real
to a String
using
the given NumberFormat
.
See NumberFormat
for a description of the
various ways that numbers may be formatted.
Equivalent double code: |
String.format("%...g",this); // Works only for base-10
|
Execution time relative to add:
| base-2 |
120
|
base-8 |
110
|
base-10 |
130
|
base-16 |
120
|
- Parameters:
format
- the number format to use in the conversion.
- Returns:
- a
String
representation of this Real
.
Copyright 2003-2009 Roar Lauritzsen