Class Apfloat
- java.lang.Object
-
- java.lang.Number
-
- org.apfloat.Apcomplex
-
- org.apfloat.Apfloat
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<Apfloat>,java.util.Formattable
- Direct Known Subclasses:
Aprational
public class Apfloat extends Apcomplex implements java.lang.Comparable<Apfloat>
Arbitrary precision floating-point number class.Apfloat numbers are immutable.
A pitfall exists with the constructors
Apfloat(float,long)andApfloat(double,long). Sincefloats anddoubles 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.3can't be presented exactly in base 2. When you construct an apfloat likenew 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 like0.30000001192092896...If you want an exact representation of a floating-point primitive (which is a rational number), you can use
Aprational(double).- Version:
- 1.11.1
- See Also:
ApfloatMath, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private ApfloatImplimplprivate static longserialVersionUID-
Fields inherited from class org.apfloat.Apcomplex
DEFAULT, EXTRA_PRECISION, I, INFINITE, ONE, ONES, REAL_ABS_IMAG_ORDER, REAL_IMAG_ORDER, ZERO, ZEROS
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedApfloat()Default constructor.Apfloat(double value)Constructs an apfloat from the specifieddouble.Apfloat(double value, long precision)Constructs an apfloat from the specifieddoubleand precision.Apfloat(double value, long precision, int radix)Constructs an apfloat from the specifieddouble, precision and radix.Apfloat(float value)Constructs an apfloat from the specifiedfloat.Apfloat(float value, long precision)Constructs an apfloat from the specifiedfloatand precision.Apfloat(float value, long precision, int radix)Constructs an apfloat from the specifiedfloat, precision and radix.Apfloat(long value)Constructs an apfloat from the specifiedlong.Apfloat(long value, long precision)Constructs an apfloat from the specifiedlongand precision.Apfloat(long value, long precision, int radix)Constructs an apfloat from the specifiedlong, precision and radix.Apfloat(java.io.PushbackReader in)Reads an apfloat from a stream using default precision and radix.Apfloat(java.io.PushbackReader in, long precision)Reads an apfloat from a stream using the specified precision.Apfloat(java.io.PushbackReader in, long precision, int radix)Reads an apfloat from a stream using the specified precision and radix.Apfloat(java.lang.String value)Constructs an apfloat from the specified string.Apfloat(java.lang.String value, long precision)Constructs an apfloat from the specified string and precision.Apfloat(java.lang.String value, long precision, int radix)Constructs an apfloat from the specified string, precision and radix.Apfloat(java.math.BigDecimal value)Creates an apfloat from aBigDecimal.Apfloat(java.math.BigDecimal value, long precision)Creates an apfloat from aBigDecimal.Apfloat(java.math.BigInteger value)Constructs an apfloat from aBigInteger.Apfloat(java.math.BigInteger value, long precision)Constructs an apfloat from aBigIntegerwith the specified precision.Apfloat(java.math.BigInteger value, long precision, int radix)Constructs an apfloat from aBigIntegerwith the specified precision and radix.protectedApfloat(ApfloatImpl impl)Constructs an apfloat that is backed by the specifiedApfloatImplobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) Apfloatabs()Apfloatadd(Apfloat x)Adds two apfloats.private ApfloataddOrSubtract(Apfloat x, boolean subtract)bytebyteValue()Returns the value of the this number as abyte.Apintceil()Ceiling function.intcompareTo(Apfloat x)Compare this apfloat to the specified apfloat.(package private) intcompareToHalf()Apfloatdivide(Apfloat x)Divides two apfloats.doubledoubleValue()Returns the value of the this number as adouble.longequalDigits(Apfloat x)Computes number of equal digits.booleanequals(java.lang.Object obj)Compares this object to the specified object.floatfloatValue()Returns the value of the this number as afloat.Apintfloor()Floor function.voidformatTo(java.util.Formatter formatter, int flags, int width, int precision)Formats the object using the provided formatter.Apfloatfrac()Returns the fractional part.private ApfloatImplgetImpl()protected ApfloatImplgetImpl(long precision)Returns anApfloatImplrepresenting the actual instance of this apfloat up to the requested precision.inthashCode()Returns a hash code for this apfloat.Apfloatimag()Imaginary part of this apfloat.intintValue()Returns the value of the this number as anint.booleanisInteger()Returns if this number has an integer value.booleanisShort()Returns if this apfloat is "short".longlongValue()Returns the value of the this number as along.longlongValueExact()Returns the value of the this number as along, checking for lost information.Apfloatmod(Apfloat x)Calculates the remainder when divided by an apfloat.Apfloatmultiply(Apfloat x)Multiplies two apfloats.Apfloatnegate()Negative value.longprecision()Returns the precision of this apfloat.Apfloatprecision(long precision)Returns an apfloat with the same value as this apfloat accurate to the specified precision.booleanpreferCompare(Apfloat x)Tests if the comparison withequalsandcompareToshould be done in the opposite order.intradix()Radix of this apfloat.Apfloatreal()Real part of this apfloat.(package private) ApintroundAway()longscale()Returns the scale of this apfloat.(package private) Apfloatscale(long scale)shortshortValue()Returns the value of the this number as ashort.intsignum()Returns the signum function of this apfloat.longsize()Returns the size of this apfloat.Apfloatsubtract(Apfloat x)Subtracts two apfloats.booleantest(Apfloat x)Tests two apfloat numbers for equality.ApfloattoRadix(int radix)Convert this apfloat to the specified radix.java.lang.StringtoString(boolean pretty)Returns a string representation of this apfloat.Apinttruncate()Truncates fractional part.voidwriteTo(java.io.Writer out, boolean pretty)Write a string representation of this apfloat to aWriter.-
Methods inherited from class org.apfloat.Apcomplex
add, byteValueExact, conj, divide, equalDigits, intValueExact, isZero, multiply, shortValueExact, subtract, test, toString, writeTo
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
impl
private ApfloatImpl impl
-
-
Constructor Detail
-
Apfloat
protected Apfloat()
Default constructor. To be used only by subclasses that overload all needed methods.
-
Apfloat
protected Apfloat(ApfloatImpl impl)
Constructs an apfloat that is backed by the specifiedApfloatImplobject.- Parameters:
impl- TheApfloatImplobject backing this apfloat.
-
Apfloat
public Apfloat(java.lang.String value) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionConstructs an apfloat from the specified string. The default radix will be used.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.- Parameters:
value- The string representing the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.lang.String value, long precision) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specified string and precision. The default radix will be used.- Parameters:
value- The string representing the number.precision- The precision of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.lang.String value, long precision, int radix) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specified string, precision and radix.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.
- Parameters:
value- The string representing the number.precision- The precision of the number.radix- The radix of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(long value) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionConstructs an apfloat from the specifiedlong. The default radix will be used. The precision of the number will beApcomplex.INFINITE.- Parameters:
value- The value of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(long value, long precision) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specifiedlongand precision. The default radix will be used.- Parameters:
value- The value of the number.precision- The precision of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(long value, long precision, int radix) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specifiedlong, precision and radix.- Parameters:
value- The value of the number.precision- The precision of the number.radix- The radix of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(float value) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionConstructs an apfloat from the specifiedfloat. The default radix will be used. The precision of the number will be the precision of afloatin the default radix, for example in radix 10 the precision is 7 digits.- Parameters:
value- The value of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(float value, long precision) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specifiedfloatand precision. The default radix will be used.Note that the resulting apfloat won't accurately represent the given
floatvalue to more than the default precision of afloat, for example in radix 10 the result is accurate to only 7 digits. The rest of the digits are unspecified even if a greater precision is specified.- Parameters:
value- The value of the number.precision- The precision of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(float value, long precision, int radix) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specifiedfloat, precision and radix.Note that the resulting apfloat won't accurately represent the given
floatvalue to more than the default precision of afloat, for example in radix 10 the result is accurate to only 7 digits. The rest of the digits are unspecified even if a greater precision is specified.- Parameters:
value- The value of the number.precision- The precision of the number.radix- The radix of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(double value) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionConstructs an apfloat from the specifieddouble. The default radix will be used. The precision of the number will be the precision of adoublein the default radix, for example in radix 10 the precision is 16 digits.- Parameters:
value- The value of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(double value, long precision) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specifieddoubleand precision. The default radix will be used.Note that the resulting apfloat won't accurately represent the given
doublevalue to more than the default precision of adouble, for example in radix 10 the result is accurate to only 16 digits. The rest of the digits are unspecified even if a greater precision is specified.In particular, this constructor does not work the same way as the
BigDecimal(double)constructor. If you want that kind of behavior then please use theApfloat(BigDecimal, long)constructor.- Parameters:
value- The value of the number.precision- The precision of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(double value, long precision, int radix) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from the specifieddouble, precision and radix.Note that the resulting apfloat won't accurately represent the given
doublevalue to more than the default precision of adouble, for example in radix 10 the result is accurate to only 16 digits. The rest of the digits are unspecified even if a greater precision is specified.In particular, this constructor does not work the same way as the
BigDecimal(double)constructor. If you want that kind of behavior then please use theApfloat(BigDecimal, long)constructor.- Parameters:
value- The value of the number.precision- The precision of the number.radix- The radix of the number.- Throws:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.io.PushbackReader in) throws java.io.IOException, java.lang.NumberFormatException, ApfloatRuntimeExceptionReads an apfloat from a stream using default precision and radix. The stream needs to be aPushbackReader, 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.- Parameters:
in- The stream to read from- Throws:
java.io.IOException- If an I/O error occurs accessing the stream.java.lang.NumberFormatException- If the number is not valid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.io.PushbackReader in, long precision) throws java.io.IOException, java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionReads an apfloat from a stream using the specified precision. The default radix is used.- Parameters:
in- The stream to read fromprecision- The precision of the number.- Throws:
java.io.IOException- If an I/O error occurs accessing the stream.java.lang.NumberFormatException- If the number is not valid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException- See Also:
Apfloat(PushbackReader)
-
Apfloat
public Apfloat(java.io.PushbackReader in, long precision, int radix) throws java.io.IOException, java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionReads an apfloat from a stream using the specified precision and radix.- Parameters:
in- The stream to read fromprecision- The precision of the number.radix- The radix of the number.- Throws:
java.io.IOException- If an I/O error occurs accessing the stream.java.lang.NumberFormatException- If the number is not valid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException- See Also:
Apfloat(PushbackReader)
-
Apfloat
public Apfloat(java.math.BigInteger value) throws java.lang.NumberFormatException, ApfloatRuntimeExceptionConstructs an apfloat from aBigInteger. Precision will beApcomplex.INFINITEand the default radix is used.- Parameters:
value- The value of the number.- Throws:
java.lang.NumberFormatException- If the default radix is not valid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.math.BigInteger value, long precision) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from aBigIntegerwith the specified precision. The default radix is used.- Parameters:
value- The value of the number.precision- The precision of the number.- Throws:
java.lang.NumberFormatException- If the default radix is not valid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.math.BigInteger value, long precision, int radix) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs an apfloat from aBigIntegerwith the specified precision and radix.- Parameters:
value- The value of the number.precision- The precision of the number.radix- The radix of the number.- Throws:
java.lang.NumberFormatException- If the radix is not valid.java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
Apfloat
public Apfloat(java.math.BigDecimal value) throws ApfloatRuntimeExceptionCreates an apfloat from aBigDecimal. An apfloat created this way will always have radix 10 regardless of the current default radix.- Parameters:
value- The value to use.- Throws:
ApfloatRuntimeException
-
Apfloat
public Apfloat(java.math.BigDecimal value, long precision) throws java.lang.IllegalArgumentException, ApfloatRuntimeExceptionCreates an apfloat from aBigDecimal. An apfloat created this way will always have radix 10 regardless of the current default radix.- Parameters:
value- The value to use.precision- The precision to use, in decimal digits.- Throws:
java.lang.IllegalArgumentException- In case the precision is invalid.ApfloatRuntimeException
-
-
Method Detail
-
radix
public int radix()
Radix of this apfloat.
-
real
public Apfloat real()
Real part of this apfloat.
-
imag
public Apfloat imag()
Imaginary part of this apfloat.- Overrides:
imagin classApcomplex- Returns:
Apcomplex.ZERO
-
precision
public long precision() throws ApfloatRuntimeExceptionReturns the precision of this apfloat.- Overrides:
precisionin classApcomplex- Returns:
- The precision of this apfloat in number of digits of the radix in which it's presented.
- Throws:
ApfloatRuntimeException
-
precision
public Apfloat precision(long precision) throws java.lang.IllegalArgumentException, ApfloatRuntimeException
Returns an apfloat with the same value as this apfloat accurate to the specified precision.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.
- Overrides:
precisionin classApcomplex- Parameters:
precision- Precision of the new apfloat.- Returns:
- An apfloat with the specified precision and same value as this apfloat.
- Throws:
java.lang.IllegalArgumentException- Ifprecisionis <= 0.ApfloatRuntimeException
-
scale
public long scale() throws ApfloatRuntimeExceptionReturns the scale of this apfloat. The scale is defined here asapfloat = signum * mantissa * radixscalewhere 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
scaleis different than injava.math.BigDecimal.- Overrides:
scalein classApcomplex- Returns:
- The exponent of this apfloat in number of digits of the radix in which it's presented.
- Throws:
ApfloatRuntimeException- See Also:
scale()
-
size
public long size() throws ApfloatRuntimeExceptionReturns the size of this apfloat. The size is defined here asapfloat = signum * mantissa * radixscaleandmantissa = n / radixsizewhere 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.- Overrides:
sizein classApcomplex- Returns:
- The number of digits in this number, from the most significant digit to the least significant nonzero digit, in the radix in which it's presented.
- Throws:
ApfloatRuntimeException- Since:
- 1.6
- See Also:
size()
-
signum
public int signum()
Returns the signum function of this apfloat.- Returns:
- -1, 0 or 1 as the value of this apfloat is negative, zero or positive, correspondingly.
-
isInteger
public boolean isInteger() throws ApfloatRuntimeExceptionReturns if this number has an integer value. Note that this does not necessarily mean that this object is an instance ofApint. Neither does it mean that the precision is infinite.- Overrides:
isIntegerin classApcomplex- Returns:
- If this number's value is an integer.
- Throws:
ApfloatRuntimeException- Since:
- 1.9.0
-
isShort
public boolean isShort() throws ApfloatRuntimeExceptionReturns if this apfloat is "short". In practice an apfloat is "short" if its mantissa fits in one machine word. If the apfloat is "short", some algorithms can be performed faster.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.
- Returns:
trueif the apfloat is "short",falseif not.- Throws:
ApfloatRuntimeException
-
negate
public Apfloat negate() throws ApfloatRuntimeException
Negative value.- Overrides:
negatein classApcomplex- Returns:
-this.- Throws:
ApfloatRuntimeException- Since:
- 1.1
-
add
public Apfloat add(Apfloat x) throws ApfloatRuntimeException
Adds two apfloats.- Parameters:
x- The number to be added to this number.- Returns:
this + x.- Throws:
ApfloatRuntimeException
-
subtract
public Apfloat subtract(Apfloat x) throws ApfloatRuntimeException
Subtracts two apfloats.- Parameters:
x- The number to be subtracted from this number.- Returns:
this - x.- Throws:
ApfloatRuntimeException
-
addOrSubtract
private Apfloat addOrSubtract(Apfloat x, boolean subtract) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
multiply
public Apfloat multiply(Apfloat x) throws ApfloatRuntimeException
Multiplies two apfloats.- Parameters:
x- The number to be multiplied by this number.- Returns:
this * x.- Throws:
ApfloatRuntimeException
-
divide
public Apfloat divide(Apfloat x) throws java.lang.ArithmeticException, ApfloatRuntimeException
Divides two apfloats.- Parameters:
x- The number by which this number is to be divided.- Returns:
this / x.- Throws:
java.lang.ArithmeticException- In case the divisor is zero.ApfloatRuntimeException
-
mod
public Apfloat mod(Apfloat x) throws ApfloatRuntimeException
Calculates the remainder when divided by an apfloat. The result has the same sign as this number. Ifxis zero, then zero is returned.- Parameters:
x- The number that is used as the divisor in the remainder calculation.- Returns:
this % x.- Throws:
ApfloatRuntimeException- Since:
- 1.2
- See Also:
ApfloatMath.fmod(Apfloat,Apfloat)
-
floor
public Apint floor() throws ApfloatRuntimeException
Floor function. Returns the largest (closest to positive infinity) value that is not greater than this apfloat and is equal to a mathematical integer.- Returns:
- This apfloat rounded towards negative infinity.
- Throws:
ApfloatRuntimeException
-
ceil
public Apint ceil() throws ApfloatRuntimeException
Ceiling function. Returns the smallest (closest to negative infinity) value that is not less than this apfloat and is equal to a mathematical integer.- Returns:
- This apfloat rounded towards positive infinity.
- Throws:
ApfloatRuntimeException
-
truncate
public Apint truncate() throws ApfloatRuntimeException
Truncates fractional part.- Returns:
- This apfloat rounded towards zero.
- Throws:
ApfloatRuntimeException
-
frac
public Apfloat frac() throws ApfloatRuntimeException
Returns the fractional part. The fractional part is always0 <= 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()- Returns:
- The fractional part of this apfloat.
- Throws:
ApfloatRuntimeException- Since:
- 1.7.0
-
doubleValue
public double doubleValue()
Returns the value of the this number as adouble. If the number is too big to fit in adouble,Double.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYis returned.- Overrides:
doubleValuein classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
double. - See Also:
doubleValue()
-
floatValue
public float floatValue()
Returns the value of the this number as afloat. If the number is too big to fit in afloat,Float.POSITIVE_INFINITYorFloat.NEGATIVE_INFINITYis returned.- Overrides:
floatValuein classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
float. - See Also:
floatValue()
-
byteValue
public byte byteValue()
Returns the value of the this number as abyte. If the number is too big to fit in abyte,Byte.MIN_VALUEorByte.MAX_VALUEis returned.- Overrides:
byteValuein classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
byte. - See Also:
byteValue()
-
shortValue
public short shortValue()
Returns the value of the this number as ashort. If the number is too big to fit in ashort,Short.MIN_VALUEorShort.MAX_VALUEis returned.- Overrides:
shortValuein classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
short. - See Also:
shortValue()
-
intValue
public int intValue()
Returns the value of the this number as anint. If the number is too big to fit in anint,Integer.MIN_VALUEorInteger.MAX_VALUEis returned.- Overrides:
intValuein classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
int. - See Also:
intValue()
-
longValue
public long longValue()
Returns the value of the this number as along. If the number is too big to fit in along,Long.MIN_VALUEorLong.MAX_VALUEis returned.- Overrides:
longValuein classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
long. - See Also:
longValue()
-
longValueExact
public long longValueExact() throws java.lang.ArithmeticExceptionDescription copied from class:ApcomplexReturns the value of the this number as along, checking for lost information. If the value of this number is out of the range of thelongtype, then anArithmeticExceptionis thrown.- Overrides:
longValueExactin classApcomplex- Returns:
- The numeric value represented by this object after conversion to type
long. - Throws:
java.lang.ArithmeticException- If the value ofthiswill not exactly fit in alongor has a nonzero fractional part.
-
equalDigits
public long equalDigits(Apfloat x) throws ApfloatRuntimeException
Computes number of equal digits.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.- Parameters:
x- Number to compare with.- Returns:
- Number of matching digits in the radix in which the numbers are presented.
- Throws:
ApfloatRuntimeException
-
toRadix
public Apfloat toRadix(int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Convert this apfloat to the specified radix.- Overrides:
toRadixin classApcomplex- Parameters:
radix- The radix.- Returns:
- This number in the specified radix.
- Throws:
java.lang.NumberFormatException- If the radix is invalid.ApfloatRuntimeException- Since:
- 1.2
-
compareTo
public int compareTo(Apfloat x)
Compare this apfloat to the specified apfloat.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);Nowx.compareTo(y) < 0becausexis assumed to be0.12000.However,
new Apfloat("0.12", 2)andnew Apfloat("0.12", 5)would be considered equal.- Specified by:
compareToin interfacejava.lang.Comparable<Apfloat>- Parameters:
x- Apfloat to which this apfloat is to be compared.- Returns:
- -1, 0 or 1 as this apfloat is numerically less than, equal to, or greater than
x.
-
preferCompare
public boolean preferCompare(Apfloat x)
Tests if the comparison withequalsandcompareToshould be done in the opposite order.Implementations should avoid infinite recursion.
- Parameters:
x- The number to compare to.- Returns:
trueif this object should invokex.equals(this)and-x.compareTo(this)instead of comparing normally.- Since:
- 1.7.0
-
equals
public boolean equals(java.lang.Object obj)
Compares this object to the specified object.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).
-
test
public boolean test(Apfloat x) throws ApfloatRuntimeException
Tests two apfloat numbers for equality. Returnsfalseif the numbers are definitely known to be not equal. Iftrueis returned, equality is unknown and should be verified by callingequals(Object). This method is usually significantly faster than callingequals(Object).- Parameters:
x- The number to test against.- Returns:
falseif the numbers are definitely not equal,trueif unknown.- Throws:
ApfloatRuntimeException- Since:
- 1.10.0
-
hashCode
public int hashCode()
Returns a hash code for this apfloat.
-
toString
public java.lang.String toString(boolean pretty) throws ApfloatRuntimeExceptionReturns a string representation of this apfloat.- Overrides:
toStringin classApcomplex- Parameters:
pretty-trueto use a fixed-point notation,falseto use an exponential notation.- Returns:
- A string representing this object.
- Throws:
ApfloatRuntimeException
-
writeTo
public void writeTo(java.io.Writer out, boolean pretty) throws java.io.IOException, ApfloatRuntimeExceptionWrite a string representation of this apfloat to aWriter.- Overrides:
writeToin classApcomplex- Parameters:
out- The outputWriter.pretty-trueto use a fixed-point notation,falseto use an exponential notation.- Throws:
java.io.IOException- In case of I/O error writing to the stream.ApfloatRuntimeException
-
formatTo
public void formatTo(java.util.Formatter formatter, int flags, int width, int precision)Formats the object using the provided formatter.The format specifiers affect the output as follows:
- By default, the exponential notation is used.
- If the alternate format is specified (
'#'), then the fixed-point notation is used. - Width is the minimum number of characters output. Any padding is done using spaces. Padding is on the left by default.
- If the
'-'flag is specified, then the padding will be on the right. - The precision is the number of significant digts output. If the precision of the number exceeds the number of characters output, the rounding mode for output is undefined.
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.
- Specified by:
formatToin interfacejava.util.Formattable- Overrides:
formatToin classApcomplex- Parameters:
formatter- The formatter.flags- The flags to modify the output format.width- The minimum number of characters to be written to the output, or-1for no minimum.precision- The maximum number of characters to be written to the output, or-1for no maximum.- Since:
- 1.3
- See Also:
formatTo(Formatter,int,int,int)
-
getImpl
protected ApfloatImpl getImpl(long precision) throws ApfloatRuntimeException
Returns anApfloatImplrepresenting the actual instance of this apfloat up to the requested precision.For apfloats this is simply the underlying
ApfloatImpl, but e.g. theAprationalclass implements this so that it only returns an approximation of the rational number.- Parameters:
precision- Precision of theApfloatImplthat is needed.- Returns:
- An
ApfloatImplrepresenting this object to the requested precision. - Throws:
ApfloatRuntimeException
-
roundAway
Apint roundAway() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
scale
Apfloat scale(long scale)
-
abs
Apfloat abs()
-
compareToHalf
int compareToHalf()
-
getImpl
private ApfloatImpl getImpl() throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
-