public class Apfloat extends Apcomplex implements Comparable<Apfloat>
Apfloat numbers are immutable.
A pitfall exists with the constructors Apfloat(float,long)
and Apfloat(double,long)
. Since float
s and
double
s are always represented internally in radix 2, the
conversion to any other radix usually causes round-off errors, and the
resulting apfloat won't be accurate to the desired number of digits.
For example, 0.3
can't be presented exactly in base 2. When
you construct an apfloat like new Apfloat(0.3f, 1000)
, the
resulting number won't be accurate to 1000 digits, but only to roughly 7
digits (in radix 10). In fact, the resulting number will be something like
0.30000001192092896
...
ApfloatMath
,
Serialized FormModifier | Constructor and Description |
---|---|
protected |
Apfloat()
Default constructor.
|
protected |
Apfloat(ApfloatImpl impl)
Constructs an apfloat that is backed by the specified
ApfloatImpl object. |
|
Apfloat(BigDecimal value)
Creates an apfloat from a
BigDecimal . |
|
Apfloat(BigDecimal value,
long precision)
Creates an apfloat from a
BigDecimal . |
|
Apfloat(BigInteger value)
Constructs an apfloat from a
BigInteger . |
|
Apfloat(BigInteger value,
long precision)
Constructs an apfloat from a
BigInteger with
the specified precision. |
|
Apfloat(BigInteger value,
long precision,
int radix)
Constructs an apfloat from a
BigInteger with
the specified precision and radix. |
|
Apfloat(double value)
Constructs an apfloat from the specified
double . |
|
Apfloat(double value,
long precision)
Constructs an apfloat from the specified
double
and precision. |
|
Apfloat(double value,
long precision,
int radix)
Constructs an apfloat from the specified
double ,
precision and radix. |
|
Apfloat(float value)
Constructs an apfloat from the specified
float . |
|
Apfloat(float value,
long precision)
Constructs an apfloat from the specified
float
and precision. |
|
Apfloat(float value,
long precision,
int radix)
Constructs an apfloat from the specified
float ,
precision and radix. |
|
Apfloat(long value)
Constructs an apfloat from the specified
long . |
|
Apfloat(long value,
long precision)
Constructs an apfloat from the specified
long
and precision. |
|
Apfloat(long value,
long precision,
int radix)
Constructs an apfloat from the specified
long ,
precision and radix. |
|
Apfloat(PushbackReader in)
Reads an apfloat from a stream using default precision and radix.
|
|
Apfloat(PushbackReader in,
long precision)
Reads an apfloat from a stream using the specified precision.
|
|
Apfloat(PushbackReader in,
long precision,
int radix)
Reads an apfloat from a stream using the specified precision
and radix.
|
|
Apfloat(String value)
Constructs an apfloat from the specified string.
|
|
Apfloat(String value,
long precision)
Constructs an apfloat from the specified string and precision.
|
|
Apfloat(String value,
long precision,
int radix)
Constructs an apfloat from the specified string, precision and radix.
|
Modifier and Type | Method and Description |
---|---|
Apfloat |
add(Apfloat x)
Adds two apfloats.
|
byte |
byteValue()
Returns the value of the this number as a
byte . |
Apint |
ceil()
Ceiling function.
|
int |
compareTo(Apfloat x)
Compare this apfloat to the specified apfloat.
|
Apfloat |
divide(Apfloat x)
Divides two apfloats.
|
double |
doubleValue()
Returns the value of the this number as a
double . |
long |
equalDigits(Apfloat x)
Computes number of equal digits.
|
boolean |
equals(Object obj)
Compares this object to the specified object.
|
float |
floatValue()
Returns the value of the this number as a
float . |
Apint |
floor()
Floor function.
|
void |
formatTo(Formatter formatter,
int flags,
int width,
int precision)
Formats the object using the provided formatter.
|
Apfloat |
frac()
Returns the fractional part.
|
protected ApfloatImpl |
getImpl(long precision)
Returns an
ApfloatImpl representing the actual instance
of this apfloat up to the requested precision. |
int |
hashCode()
Returns a hash code for this apfloat.
|
Apfloat |
imag()
Imaginary part of this apfloat.
|
int |
intValue()
Returns the value of the this number as an
int . |
boolean |
isShort()
Returns if this apfloat is "short".
|
long |
longValue()
Returns the value of the this number as a
long . |
Apfloat |
mod(Apfloat x)
Calculates the remainder when divided by an apfloat.
|
Apfloat |
multiply(Apfloat x)
Multiplies two apfloats.
|
Apfloat |
negate()
Negative value.
|
long |
precision()
Returns the precision of this apfloat.
|
Apfloat |
precision(long precision)
Returns an apfloat with the same value as this apfloat accurate to the
specified precision.
|
boolean |
preferCompare(Apfloat x)
Tests if the comparison with
equals and compareTo should be done in the opposite order. |
int |
radix()
Radix of this apfloat.
|
Apfloat |
real()
Real part of this apfloat.
|
long |
scale()
Returns the scale of this apfloat.
|
short |
shortValue()
Returns the value of the this number as a
short . |
int |
signum()
Returns the signum function of this apfloat.
|
long |
size()
Returns the size of this apfloat.
|
Apfloat |
subtract(Apfloat x)
Subtracts two apfloats.
|
Apfloat |
toRadix(int radix)
Convert this apfloat to the specified radix.
|
String |
toString(boolean pretty)
Returns a string representation of this apfloat.
|
Apint |
truncate()
Truncates fractional part.
|
void |
writeTo(Writer out,
boolean pretty)
Write a string representation of this apfloat to a
Writer . |
protected Apfloat()
protected Apfloat(ApfloatImpl impl)
ApfloatImpl
object.impl
- The ApfloatImpl
object backing this apfloat.public Apfloat(String value) throws NumberFormatException, ApfloatRuntimeException
The precision will be calculated from the number of digits specified in the string. For example:
"0.1"
will have a precision of 1 digit.
"1.0"
will have a precision of 2 digits.
"100"
will have a precision of 3 digits.
value
- The string representing the number.NumberFormatException
- In case the number is invalid.ApfloatRuntimeException
public Apfloat(String value, long precision) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
value
- The string representing the number.precision
- The precision of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(String value, long precision, int radix) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
Note that it's impossible to construct apfloats with a specified exponent and with radix >= 14, since the characters 'e' and 'E' will be treated as digits of the mantissa.
For example, in radix 10, "1e5" means the decimal number 100000. But in radix 16, "1e5" means the decimal number 485.
value
- The string representing the number.precision
- The precision of the number.radix
- The radix of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(long value) throws NumberFormatException, ApfloatRuntimeException
long
.
The default radix will be used. The precision of the number
will be Apcomplex.INFINITE
.value
- The value of the number.NumberFormatException
- In case the number is invalid.ApfloatRuntimeException
public Apfloat(long value, long precision) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
long
and precision. The default radix will be used.value
- The value of the number.precision
- The precision of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(long value, long precision, int radix) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
long
,
precision and radix.value
- The value of the number.precision
- The precision of the number.radix
- The radix of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(float value) throws NumberFormatException, ApfloatRuntimeException
float
.
The default radix will be used. The precision of the number
will be the precision of a float
in the default
radix, for example in radix 10 the precision is 7 digits.value
- The value of the number.NumberFormatException
- In case the number is invalid.ApfloatRuntimeException
public Apfloat(float value, long precision) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
float
and precision. The default radix will be used.value
- The value of the number.precision
- The precision of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(float value, long precision, int radix) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
float
,
precision and radix.value
- The value of the number.precision
- The precision of the number.radix
- The radix of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(double value) throws NumberFormatException, ApfloatRuntimeException
double
.
The default radix will be used. The precision of the number
will be the precision of a double
in the default
radix, for example in radix 10 the precision is 16 digits.value
- The value of the number.NumberFormatException
- In case the number is invalid.ApfloatRuntimeException
public Apfloat(double value, long precision) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
double
and precision. The default radix will be used.value
- The value of the number.precision
- The precision of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(double value, long precision, int radix) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
double
,
precision and radix.value
- The value of the number.precision
- The precision of the number.radix
- The radix of the number.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(PushbackReader in) throws IOException, NumberFormatException, ApfloatRuntimeException
PushbackReader
,
as the first invalid character is pushed back to the stream.
Note that since only a pushback buffer of one character is used,
the number read may still not be valid. For example, if the stream
contains "-#"
or "1.5e#"
(here '#'
is the first invalid character), the number is actually not valid, and
only the character '#'
would be put back to the stream.
The precision is determined similarly as in the Apfloat(String)
constructor that is as the number of digits read from the stream.
in
- The stream to read fromIOException
- If an I/O error occurs accessing the stream.NumberFormatException
- If the number is not valid.ApfloatRuntimeException
public Apfloat(PushbackReader in, long precision) throws IOException, NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
in
- The stream to read fromprecision
- The precision of the number.IOException
- If an I/O error occurs accessing the stream.NumberFormatException
- If the number is not valid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
Apfloat(PushbackReader)
public Apfloat(PushbackReader in, long precision, int radix) throws IOException, NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
in
- The stream to read fromprecision
- The precision of the number.radix
- The radix of the number.IOException
- If an I/O error occurs accessing the stream.NumberFormatException
- If the number is not valid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
Apfloat(PushbackReader)
public Apfloat(BigInteger value) throws NumberFormatException, ApfloatRuntimeException
BigInteger
.
Precision will be Apcomplex.INFINITE
and the default radix
is used.value
- The value of the number.NumberFormatException
- If the default radix is not valid.ApfloatRuntimeException
public Apfloat(BigInteger value, long precision) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
BigInteger
with
the specified precision. The default radix is used.value
- The value of the number.precision
- The precision of the number.NumberFormatException
- If the default radix is not valid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(BigInteger value, long precision, int radix) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException
BigInteger
with
the specified precision and radix.value
- The value of the number.precision
- The precision of the number.radix
- The radix of the number.NumberFormatException
- If the radix is not valid.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public Apfloat(BigDecimal value) throws ApfloatRuntimeException
BigDecimal
. An apfloat created this
way will always have radix 10 regardless of the current default radix.value
- The value to use.ApfloatRuntimeException
public Apfloat(BigDecimal value, long precision) throws IllegalArgumentException, ApfloatRuntimeException
BigDecimal
. An apfloat created this
way will always have radix 10 regardless of the current default radix.value
- The value to use.precision
- The precision to use, in decimal digits.IllegalArgumentException
- In case the precision is invalid.ApfloatRuntimeException
public int radix()
public Apfloat real()
public Apfloat imag()
imag
in class Apcomplex
Apcomplex.ZERO
public long precision() throws ApfloatRuntimeException
precision
in class Apcomplex
ApfloatRuntimeException
public Apfloat precision(long precision) throws IllegalArgumentException, ApfloatRuntimeException
If the requested precision less than this number's current precision, the functionality is quite obvious: the precision is simply truncated, and e.g. comparison and equality checking will work as expected. Some rounding errors in e.g. addition and subtraction may still occur, as "invisible" trailing digits can remain in the number.
If the requested precision more than this number's current precision, the functionality is quite undefined: the digits up to this number's current precision are guaranteed to be the same, but the "new" digits are undefined: they may be zero, or they may be digits that have been previously discarded with a call to precision() with a smaller number of digits, or they may be something else, or any combination of these.
These limitations allow various performance optimizations to be made.
precision
in class Apcomplex
precision
- Precision of the new apfloat.IllegalArgumentException
- If precision
is <= 0.ApfloatRuntimeException
public long scale() throws ApfloatRuntimeException
apfloat = signum * mantissa * radixscale
where 1/radix <= mantissa < 1. In other words,
scale = floor(logradix(apfloat)) + 1
.
For example, 1 has a scale of 1, and 100 has a scale of 3 (in radix 10). For integers, scale is equal to the number of digits in the apfloat.
Zero has a scale of -INFINITE
.
Note that this definition of scale
is different than in java.math.BigDecimal
.
scale
in class Apcomplex
ApfloatRuntimeException
scale()
public long size() throws ApfloatRuntimeException
apfloat = signum * mantissa * radixscale
and
mantissa = n / radixsize
where 1/radix <= mantissa < 1 and n is the smallest possible integer. In other words, the size is the number of significant digits in the mantissa (excluding leading and trailing zeros but including all zeros between the first and last nonzero digit). For example, 1 has a size of 1, and 100 has also a size of 1 (in radix 10). 11 has a size of 2, and 10001000 has a size of 5.
Zero has a size of 0
.
size
in class Apcomplex
ApfloatRuntimeException
size()
public int signum()
public boolean isShort() throws ApfloatRuntimeException
For example, division by a "short" apfloat requires only a single pass through the data, but that algorithm can't be used for divisors that aren't "short", where calculating an inverse root is required instead.
The return value of this method is implementation dependent.
true
if the apfloat is "short", false
if not.ApfloatRuntimeException
public Apfloat negate() throws ApfloatRuntimeException
negate
in class Apcomplex
-this
.ApfloatRuntimeException
public Apfloat add(Apfloat x) throws ApfloatRuntimeException
x
- The number to be added to this number.this + x
.ApfloatRuntimeException
public Apfloat subtract(Apfloat x) throws ApfloatRuntimeException
x
- The number to be subtracted from this number.this - x
.ApfloatRuntimeException
public Apfloat multiply(Apfloat x) throws ApfloatRuntimeException
x
- The number to be multiplied by this number.this * x
.ApfloatRuntimeException
public Apfloat divide(Apfloat x) throws ArithmeticException, ApfloatRuntimeException
x
- The number by which this number is to be divided.this / x
.ArithmeticException
- In case the divisor is zero.ApfloatRuntimeException
public Apfloat mod(Apfloat x) throws ApfloatRuntimeException
x
is zero, then zero is returned.x
- The number that is used as the divisor in the remainder calculation.this % x
.ApfloatRuntimeException
ApfloatMath.fmod(Apfloat,Apfloat)
public Apint floor() throws ApfloatRuntimeException
ApfloatRuntimeException
public Apint ceil() throws ApfloatRuntimeException
ApfloatRuntimeException
public Apint truncate() throws ApfloatRuntimeException
ApfloatRuntimeException
public Apfloat frac() throws ApfloatRuntimeException
0 <= abs(frac()) < 1
.
The fractional part has the same sign as the number. For the fractional and integer parts, this always holds:
x = x.truncate() + x.frac()
ApfloatRuntimeException
public double doubleValue()
double
.
If the number is too big to fit in a double
,
Double.POSITIVE_INFINITY
or
Double.NEGATIVE_INFINITY
is returned.doubleValue
in class Apcomplex
double
.doubleValue()
public float floatValue()
float
.
If the number is too big to fit in a float
,
Float.POSITIVE_INFINITY
or
Float.NEGATIVE_INFINITY
is returned.floatValue
in class Apcomplex
float
.floatValue()
public byte byteValue()
byte
.
If the number is too big to fit in a byte
,
Byte.MIN_VALUE
or
Byte.MAX_VALUE
is returned.byteValue
in class Apcomplex
byte
.byteValue()
public short shortValue()
short
.
If the number is too big to fit in a short
,
Short.MIN_VALUE
or
Short.MAX_VALUE
is returned.shortValue
in class Apcomplex
short
.shortValue()
public int intValue()
int
.
If the number is too big to fit in an int
,
Integer.MIN_VALUE
or
Integer.MAX_VALUE
is returned.intValue
in class Apcomplex
int
.intValue()
public long longValue()
long
.
If the number is too big to fit in a long
,
Long.MIN_VALUE
or
Long.MAX_VALUE
is returned.longValue
in class Apcomplex
long
.longValue()
public long equalDigits(Apfloat x) throws ApfloatRuntimeException
Compares the digits of the numbers starting from the most significant digits. The exponent and sign are taken into consideration, so if either one doesn't match, the numbers are considered to have zero equal digits.
For example, the numbers 12345 and 123456 have zero matching digits, and the numbers 12345 and 12355 have three matching digits.
The result of this method is roughly equal to
Math.min(scale(), x.scale()) - subtract(x).scale()
but it typically is a lot more efficient to execute.
x
- Number to compare with.ApfloatRuntimeException
public Apfloat toRadix(int radix) throws NumberFormatException, ApfloatRuntimeException
toRadix
in class Apcomplex
radix
- The radix.NumberFormatException
- If the radix is invalid.ApfloatRuntimeException
public int compareTo(Apfloat x)
Note: if two apfloats are compared where one number doesn't have enough precise digits, the mantissa is assumed to contain zeros. For example:
Apfloat x = new Apfloat("0.12", 2); Apfloat y = new Apfloat("0.12345", 5);Now
x.compareTo(y) < 0
because x
is assumed to
be 0.12000
.
However, new Apfloat("0.12", 2)
and new Apfloat("0.12", 5)
would be considered equal.
compareTo
in interface Comparable<Apfloat>
x
- Apfloat to which this apfloat is to be compared.x
.public boolean preferCompare(Apfloat x)
equals
and compareTo
should be done in the opposite order.Implementations should avoid infinite recursion.
x
- The number to compare to.true
if this object should invoke x.equals(this)
and -x.compareTo(this)
instead of comparing normally.public boolean equals(Object obj)
Note: if two apfloats are compared where one number doesn't have enough
precise digits, the mantissa is assumed to contain zeros.
See compareTo(Apfloat)
.
public int hashCode()
public String toString(boolean pretty) throws ApfloatRuntimeException
toString
in class Apcomplex
pretty
- true
to use a fixed-point notation, false
to use an exponential notation.ApfloatRuntimeException
public void writeTo(Writer out, boolean pretty) throws IOException, ApfloatRuntimeException
Writer
.writeTo
in class Apcomplex
out
- The output Writer
.pretty
- true
to use a fixed-point notation, false
to use an exponential notation.IOException
- In case of I/O error writing to the stream.ApfloatRuntimeException
public void formatTo(Formatter formatter, int flags, int width, int precision)
The format specifiers affect the output as follows:
'#'
), then the fixed-point notation is used.'-'
flag is specified, then the padding will be on the right.The decimal separator will be localized if the formatter specifies a locale. The digits will be localized also, but only if the radix is less than or equal to 10.
formatTo
in interface Formattable
formatTo
in class Apcomplex
formatter
- The formatter.flags
- The flags to modify the output format.width
- The minimum number of characters to be written to the output, or -1
for no minimum.precision
- The maximum number of characters to be written to the output, or -1
for no maximum.formatTo(Formatter,int,int,int)
protected ApfloatImpl getImpl(long precision) throws ApfloatRuntimeException
ApfloatImpl
representing the actual instance
of this apfloat up to the requested precision.
For apfloats this is simply the underlying ApfloatImpl
,
but e.g. the Aprational
class implements this so that
it only returns an approximation of the rational number.
precision
- Precision of the ApfloatImpl
that is needed.ApfloatImpl
representing this object to the requested precision.ApfloatRuntimeException
Copyright © 2018. All rights reserved.