Class Aprational
- All Implemented Interfaces:
Serializable
,Comparable<Apfloat>
,Formattable
- Direct Known Subclasses:
Apint
Apint
.- Version:
- 1.10.0
- Author:
- Mikko Tommila
- See Also:
-
Field Summary
Fields inherited from class org.apfloat.Apcomplex
DEFAULT, I, INFINITE, ONE, REAL_ABS_IMAG_ORDER, REAL_IMAG_ORDER, ZERO
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Default constructor.Aprational
(double value) Constructs an aprational from adouble
.Aprational
(double value, int radix) Constructs an aprational from adouble
using the specified radix.Reads an aprational from a reader.Aprational
(PushbackReader in, int radix) Reads an aprational from a reader.Aprational
(String value) Constructs an aprational from a string.Aprational
(String value, int radix) Constructs an aprational from a string with the specified radix.Aprational
(BigInteger value) Constructs an aprational from aBigInteger
.Aprational
(BigInteger value, int radix) Constructs an aprational from aBigInteger
using the specified radix.Aprational
(Apint value) Construct an integer aprational whose denominator is one.Aprational
(Apint numerator, Apint denominator) Construct an aprational with the specified numerator and denominator. -
Method Summary
Modifier and TypeMethodDescriptionadd
(Aprational x) Adds two aprational numbers.ceil()
Ceiling function.int
Compare this aprational to the specified apfloat.int
Compare this aprational to the specified aprational.Denominator of this aprational.divide
(Aprational x) Divides two aprational numbers.boolean
Compares this object to the specified object.floor()
Floor function.void
Formats the object using the provided formatter.frac()
Returns the fractional part.protected ApfloatImpl
getImpl
(long precision) Returns anApfloatImpl
representing the approximation of this aprational up to the requested precision.int
hashCode()
Returns a hash code for this aprational.boolean
Returns if this number has an integer value.boolean
isShort()
Returns if this aprational is "short".mod
(Aprational x) Calculates the remainder when divided by an aprational.Multiplies two aprational numbers.negate()
Negative value.Numerator of this aprational.long
Returns the precision of this aprational.boolean
Tests if the comparison withequals
andcompareTo
should be done in the opposite order.int
radix()
Radix of this aprational.long
scale()
Returns the scale of this aprational.int
signum()
Returns the signum function of this aprational.long
size()
Returns the size of this aprational.Subtracts two aprational numbers.boolean
Tests two apfloat numbers for equality.boolean
test
(Aprational x) Tests two aprational numbers for equality.toRadix
(int radix) Convert this aprational to the specified radix.toString()
Returns a string representation of this aprational.toString
(boolean pretty) Returns a string representation of this aprational.truncate()
Truncates fractional part.void
Write a string representation of this aprational to aWriter
.void
Write a string representation of this aprational to aWriter
.Methods inherited from class org.apfloat.Apfloat
add, byteValue, divide, doubleValue, equalDigits, floatValue, imag, intValue, longValue, longValueExact, mod, multiply, precision, real, shortValue, subtract
Methods inherited from class org.apfloat.Apcomplex
add, byteValueExact, conj, divide, equalDigits, intValueExact, isZero, multiply, shortValueExact, subtract, test
-
Constructor Details
-
Aprational
protected Aprational()Default constructor. To be used only by subclasses that overload all needed methods. -
Aprational
Construct an integer aprational whose denominator is one.- Parameters:
value
- The numerator of the number.- Throws:
ApfloatRuntimeException
-
Aprational
public Aprational(Apint numerator, Apint denominator) throws IllegalArgumentException, ApfloatRuntimeException Construct an aprational with the specified numerator and denominator.- Parameters:
numerator
- The numerator.denominator
- The denominator.- Throws:
IllegalArgumentException
- In case the denominator is zero, or if the denominator is not one or the numerator is not zero, and the radix of the numerator and denominator are different.ApfloatRuntimeException
-
Aprational
public Aprational(String value) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException Constructs an aprational from a string. The default radix is used.The input must be of one of the formats
integer
numerator [whitespace] "/" [whitespace] denominator
- Parameters:
value
- The input string.- Throws:
NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the denominator is zero.ApfloatRuntimeException
-
Aprational
public Aprational(String value, int radix) throws NumberFormatException, IllegalArgumentException, ApfloatRuntimeException Constructs an aprational from a string with the specified radix.The input must be of one of the formats
integer
numerator [whitespace] "/" [whitespace] denominator
- Parameters:
value
- The input string.radix
- The radix to be used.- Throws:
NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the denominator is zero.ApfloatRuntimeException
-
Aprational
public Aprational(PushbackReader in) throws IOException, NumberFormatException, IllegalArgumentException, ApfloatRuntimeException Reads an aprational from a reader. The default radix is used. The constructor stops reading at the first character it doesn't understand. The reader must thus be aPushbackReader
so that the invalid character can be returned back to the stream.The input must be of one of the formats
integer [whitespace]
numerator [whitespace] "/" [whitespace] denominator
- Parameters:
in
- The input stream.- Throws:
IOException
- In case of I/O error reading the stream.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the denominator is zero.ApfloatRuntimeException
-
Aprational
public Aprational(PushbackReader in, int radix) throws IOException, NumberFormatException, IllegalArgumentException, ApfloatRuntimeException Reads an aprational from a reader. The specified radix is used.- Parameters:
in
- The input stream.radix
- The radix to be used.- Throws:
IOException
- In case of I/O error reading the stream.NumberFormatException
- In case the number is invalid.IllegalArgumentException
- In case the denominator is zero.ApfloatRuntimeException
- See Also:
-
Aprational
Constructs an aprational from aBigInteger
. The default radix is used.- Parameters:
value
- The numerator of the number.- Throws:
ApfloatRuntimeException
-
Aprational
Constructs an aprational from aBigInteger
using the specified radix.- Parameters:
value
- The numerator of the number.radix
- The radix of the number.- Throws:
ApfloatRuntimeException
-
Aprational
Constructs an aprational from adouble
. The exact value represented by thedouble
is used. The default radix is used.Note that
double
s are presented as an integer multiplied by a power of two (positive or negative). Many numbers can't be represented exactly this way, e.g.new Aprational(0.1)
won't result in1/10
but in3602879701896397/36028797018963968
.- Parameters:
value
- The numerator of the number.- Throws:
ApfloatRuntimeException
-
Aprational
Constructs an aprational from adouble
using the specified radix. The exact value represented by thedouble
is used.Note that
double
s are presented as an integer multiplied by a power of two (positive or negative). Many numbers can't be represented exactly this way, e.g.new Aprational(0.1)
won't result in1/10
but in3602879701896397/36028797018963968
.- Parameters:
value
- The numerator of the number.radix
- The radix of the number.- Throws:
ApfloatRuntimeException
-
-
Method Details
-
numerator
-
denominator
-
radix
-
precision
Returns the precision of this aprational.- Overrides:
precision
in classApfloat
- Returns:
INFINITE
- Throws:
ApfloatRuntimeException
-
scale
Returns the scale of this aprational. Scale is equal to the number of digits in the aprational's truncated value.Zero has a scale of
-INFINITE
.- Overrides:
scale
in classApfloat
- Returns:
- Number of digits in the truncated value of this aprational in the radix in which it's presented.
- Throws:
ApfloatRuntimeException
- See Also:
-
size
Returns the size of this aprational. Size is equal to the number of significant digits in the aprational's floating-point expansion. If the expansion is infinite then this method returnsINFINITE
.Zero has a size of
0
.- Overrides:
size
in classApfloat
- Returns:
- Number of significant digits in the floating-point expansion of this aprational in the radix in which it's presented.
- Throws:
ApfloatRuntimeException
- Since:
- 1.6
- See Also:
-
signum
-
isShort
Returns if this aprational is "short".- Overrides:
isShort
in classApfloat
- Returns:
true
if the aprational is "short",false
if not.- Throws:
ApfloatRuntimeException
- See Also:
-
isInteger
Returns if this number has an integer value. Note that this does not necessarily mean that this object is an instance ofApint
.A rational number is an integer if the denominator is one.
- Overrides:
isInteger
in classApfloat
- Returns:
- If this number's value is an integer.
- Throws:
ApfloatRuntimeException
- Since:
- 1.9.0
-
negate
Negative value.- Overrides:
negate
in classApfloat
- Returns:
-this
.- Throws:
ApfloatRuntimeException
- Since:
- 1.1
-
add
Adds two aprational numbers.- Parameters:
x
- The number to be added to this number.- Returns:
this + x
.- Throws:
ApfloatRuntimeException
-
subtract
Subtracts two aprational numbers.- Parameters:
x
- The number to be subtracted from this number.- Returns:
this - x
.- Throws:
ApfloatRuntimeException
-
multiply
Multiplies two aprational numbers.- Parameters:
x
- The number to be multiplied by this number.- Returns:
this * x
.- Throws:
ApfloatRuntimeException
-
divide
Divides two aprational numbers.- Parameters:
x
- The number by which this number is to be divided.- Returns:
this / x
.- Throws:
ArithmeticException
- In case the divisor is zero.ApfloatRuntimeException
-
mod
Calculates the remainder when divided by an aprational. The result has the same sign as this number. Ifx
is 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
-
floor
Floor function. Returns the largest (closest to positive infinity) value that is not greater than this aprational and is equal to a mathematical integer.- Overrides:
floor
in classApfloat
- Returns:
- This aprational rounded towards negative infinity.
- Throws:
ApfloatRuntimeException
-
ceil
Ceiling function. Returns the smallest (closest to negative infinity) value that is not less than this aprational and is equal to a mathematical integer.- Overrides:
ceil
in classApfloat
- Returns:
- This aprational rounded towards positive infinity.
- Throws:
ApfloatRuntimeException
-
truncate
Truncates fractional part.- Overrides:
truncate
in classApfloat
- Returns:
- This aprational rounded towards zero.
- Throws:
ApfloatRuntimeException
-
frac
Returns the fractional part. The fractional part is always0 <= abs(x.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()
- Overrides:
frac
in classApfloat
- Returns:
- The fractional part of this aprational.
- Throws:
ApfloatRuntimeException
- Since:
- 1.7.0
-
toRadix
Convert this aprational to the specified radix.- Overrides:
toRadix
in classApfloat
- Parameters:
radix
- The radix.- Returns:
- This number in the specified radix.
- Throws:
NumberFormatException
- If the radix is invalid.ApfloatRuntimeException
- Since:
- 1.2
-
compareTo
Compare this aprational to the specified aprational.- Parameters:
x
- Aprational to which this aprational is to be compared.- Returns:
- -1, 0 or 1 as this aprational is numerically less than, equal to, or greater than
x
.
-
compareTo
Compare this aprational to the specified apfloat.- Specified by:
compareTo
in interfaceComparable<Apfloat>
- Overrides:
compareTo
in classApfloat
- Parameters:
x
- Apfloat to which this aprational is to be compared.- Returns:
- -1, 0 or 1 as this aprational is numerically less than, equal to, or greater than
x
.
-
preferCompare
Description copied from class:Apfloat
Tests if the comparison withequals
andcompareTo
should be done in the opposite order.Implementations should avoid infinite recursion.
- Overrides:
preferCompare
in classApfloat
- Parameters:
x
- The number to compare to.- Returns:
true
if this object should invokex.equals(this)
and-x.compareTo(this)
instead of comparing normally.
-
equals
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
Apfloat.compareTo(Apfloat)
. -
test
Tests two aprational numbers for equality. Returnsfalse
if the numbers are definitely known to be not equal. Iftrue
is 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:
false
if the numbers are definitely not equal,true
if unknown.- Since:
- 1.10.0
-
test
Description copied from class:Apfloat
Tests two apfloat numbers for equality. Returnsfalse
if the numbers are definitely known to be not equal. Iftrue
is returned, equality is unknown and should be verified by callingApfloat.equals(Object)
. This method is usually significantly faster than callingequals(Object)
.- Overrides:
test
in classApfloat
- Parameters:
x
- The number to test against.- Returns:
false
if the numbers are definitely not equal,true
if unknown.- Throws:
ApfloatRuntimeException
-
hashCode
-
toString
-
toString
Returns a string representation of this aprational.- Overrides:
toString
in classApfloat
- Parameters:
pretty
-true
to use a fixed-point notation,false
to use an exponential notation.- Returns:
- A string representing this object.
- Throws:
ApfloatRuntimeException
-
writeTo
Write a string representation of this aprational to aWriter
.- Overrides:
writeTo
in classApcomplex
- Parameters:
out
- The outputWriter
.- Throws:
IOException
- In case of I/O error writing to the stream.ApfloatRuntimeException
-
writeTo
Write a string representation of this aprational to aWriter
.- Overrides:
writeTo
in classApfloat
- Parameters:
out
- The outputWriter
.pretty
-true
to use a fixed-point notation,false
to use an exponential notation.- Throws:
IOException
- In case of I/O error writing to the stream.ApfloatRuntimeException
-
formatTo
Formats the object using the provided formatter.- Specified by:
formatTo
in interfaceFormattable
- Overrides:
formatTo
in classApfloat
- 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-1
for no minimum.precision
- The maximum number of characters to be written to the output, or-1
for no maximum.- Since:
- 1.3
- See Also:
-
getImpl
Returns anApfloatImpl
representing the approximation of this aprational up to the requested precision.- Overrides:
getImpl
in classApfloat
- Parameters:
precision
- Precision of theApfloatImpl
that is needed.- Returns:
- An
ApfloatImpl
representing this object to the requested precision. - Throws:
ApfloatRuntimeException
-