Class Aprational
- java.lang.Object
-
- java.lang.Number
-
- org.apfloat.Apcomplex
-
- org.apfloat.Apfloat
-
- org.apfloat.Aprational
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<Apfloat>,java.util.Formattable
- Direct Known Subclasses:
Apint
public class Aprational extends Apfloat
Arbitrary precision rational number class. An aprational consists of a numerator and a denominator of typeApint.- Version:
- 1.10.0
- See Also:
Apint,AprationalMath, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ref.SoftReference<Apfloat>approxprivate Apintdenominatorprivate java.lang.ref.SoftReference<Apfloat>inverseDenprivate Apintnumeratorprivate longscaleprivate static longserialVersionUIDprivate longsizeprivate static longUNDEFINED-
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 protectedAprational()Default constructor.Aprational(double value)Constructs an aprational from adouble.Aprational(double value, int radix)Constructs an aprational from adoubleusing the specified radix.Aprational(java.io.PushbackReader in)Reads an aprational from a reader.Aprational(java.io.PushbackReader in, int radix)Reads an aprational from a reader.Aprational(java.lang.String value)Constructs an aprational from a string.Aprational(java.lang.String value, int radix)Constructs an aprational from a string with the specified radix.Aprational(java.math.BigInteger value)Constructs an aprational from aBigInteger.Aprational(java.math.BigInteger value, int radix)Constructs an aprational from aBigIntegerusing 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) Aprationalabs()Aprationaladd(Aprational x)Adds two aprational numbers.Apintceil()Ceiling function.private voidcheckDenominator()intcompareTo(Apfloat x)Compare this aprational to the specified apfloat.intcompareTo(Aprational x)Compare this aprational to the specified aprational.(package private) intcompareToHalf()Apintdenominator()Denominator of this aprational.Aprationaldivide(Aprational x)Divides two aprational numbers.private ApfloatensureApprox(long precision)booleanequals(java.lang.Object obj)Compares this object to the specified object.Apintfloor()Floor function.voidformatTo(java.util.Formatter formatter, int flags, int width, int precision)Formats the object using the provided formatter.Aprationalfrac()Returns the fractional part.private ApfloatgetApprox(long precision)protected ApfloatImplgetImpl(long precision)Returns anApfloatImplrepresenting the approximation of this aprational up to the requested precision.private ApfloatgetInverseDen()inthashCode()Returns a hash code for this aprational.booleanisInteger()Returns if this number has an integer value.booleanisShort()Returns if this aprational is "short".Aprationalmod(Aprational x)Calculates the remainder when divided by an aprational.Aprationalmultiply(Aprational x)Multiplies two aprational numbers.Aprationalnegate()Negative value.Apintnumerator()Numerator of this aprational.longprecision()Returns the precision of this aprational.booleanpreferCompare(Apfloat x)Tests if the comparison withequalsandcompareToshould be done in the opposite order.intradix()Radix of this aprational.private Aprationalreduce()(package private) ApintroundAway()longscale()Returns the scale of this aprational.(package private) Aprationalscale(long scale)private voidsetApprox(Apfloat approx)private voidsetInverseDen(Apfloat inverseDen)intsignum()Returns the signum function of this aprational.longsize()Returns the size of this aprational.Aprationalsubtract(Aprational x)Subtracts two aprational numbers.booleantest(Apfloat x)Tests two apfloat numbers for equality.booleantest(Aprational x)Tests two aprational numbers for equality.AprationaltoRadix(int radix)Convert this aprational to the specified radix.java.lang.StringtoString()Returns a string representation of this aprational.java.lang.StringtoString(boolean pretty)Returns a string representation of this aprational.Apinttruncate()Truncates fractional part.voidwriteTo(java.io.Writer out)Write a string representation of this aprational to aWriter.voidwriteTo(java.io.Writer out, boolean pretty)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
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
UNDEFINED
private static final long UNDEFINED
- See Also:
- Constant Field Values
-
numerator
private Apint numerator
-
denominator
private Apint denominator
-
scale
private volatile long scale
-
size
private volatile long size
-
inverseDen
private transient java.lang.ref.SoftReference<Apfloat> inverseDen
-
approx
private transient java.lang.ref.SoftReference<Apfloat> approx
-
-
Constructor Detail
-
Aprational
protected Aprational()
Default constructor. To be used only by subclasses that overload all needed methods.
-
Aprational
public Aprational(Apint value) throws ApfloatRuntimeException
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 java.lang.IllegalArgumentException, ApfloatRuntimeException
Construct an aprational with the specified numerator and denominator.- Parameters:
numerator- The numerator.denominator- The denominator.- Throws:
java.lang.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(java.lang.String value) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs 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:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the denominator is zero.ApfloatRuntimeException
-
Aprational
public Aprational(java.lang.String value, int radix) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionConstructs 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:
java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the denominator is zero.ApfloatRuntimeException
-
Aprational
public Aprational(java.io.PushbackReader in) throws java.io.IOException, java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionReads 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 aPushbackReaderso 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:
java.io.IOException- In case of I/O error reading the stream.java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the denominator is zero.ApfloatRuntimeException
-
Aprational
public Aprational(java.io.PushbackReader in, int radix) throws java.io.IOException, java.lang.NumberFormatException, java.lang.IllegalArgumentException, ApfloatRuntimeExceptionReads an aprational from a reader. The specified radix is used.- Parameters:
in- The input stream.radix- The radix to be used.- Throws:
java.io.IOException- In case of I/O error reading the stream.java.lang.NumberFormatException- In case the number is invalid.java.lang.IllegalArgumentException- In case the denominator is zero.ApfloatRuntimeException- See Also:
Aprational(PushbackReader)
-
Aprational
public Aprational(java.math.BigInteger value) throws ApfloatRuntimeExceptionConstructs an aprational from aBigInteger. The default radix is used.- Parameters:
value- The numerator of the number.- Throws:
ApfloatRuntimeException
-
Aprational
public Aprational(java.math.BigInteger value, int radix) throws ApfloatRuntimeExceptionConstructs an aprational from aBigIntegerusing the specified radix.- Parameters:
value- The numerator of the number.radix- The radix of the number.- Throws:
ApfloatRuntimeException
-
Aprational
public Aprational(double value) throws ApfloatRuntimeExceptionConstructs an aprational from adouble. The exact value represented by thedoubleis used. The default radix is used.Note that
doubles 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/10but in3602879701896397/36028797018963968.- Parameters:
value- The numerator of the number.- Throws:
ApfloatRuntimeException
-
Aprational
public Aprational(double value, int radix) throws ApfloatRuntimeExceptionConstructs an aprational from adoubleusing the specified radix. The exact value represented by thedoubleis used.Note that
doubles 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/10but in3602879701896397/36028797018963968.- Parameters:
value- The numerator of the number.radix- The radix of the number.- Throws:
ApfloatRuntimeException
-
-
Method Detail
-
numerator
public Apint numerator()
Numerator of this aprational.- Returns:
nwherethis = n / m.
-
denominator
public Apint denominator()
Denominator of this aprational.- Returns:
mwherethis = n / m.
-
radix
public int radix()
Radix of this aprational.
-
precision
public long precision() throws ApfloatRuntimeExceptionReturns the precision of this aprational.- Overrides:
precisionin classApfloat- Returns:
INFINITE- Throws:
ApfloatRuntimeException
-
scale
public long scale() throws ApfloatRuntimeExceptionReturns 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:
scalein classApfloat- Returns:
- Number of digits in the truncated value of this aprational in the radix in which it's presented.
- Throws:
ApfloatRuntimeException- See Also:
Apfloat.scale()
-
size
public long size() throws ApfloatRuntimeExceptionReturns 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:
sizein 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:
Apfloat.size()
-
signum
public int signum()
Returns the signum function of this aprational.
-
isShort
public boolean isShort() throws ApfloatRuntimeExceptionReturns if this aprational is "short".- Overrides:
isShortin classApfloat- Returns:
trueif the aprational is "short",falseif not.- Throws:
ApfloatRuntimeException- See Also:
Apfloat.isShort()
-
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.A rational number is an integer if the denominator is one.
- Overrides:
isIntegerin classApfloat- Returns:
- If this number's value is an integer.
- Throws:
ApfloatRuntimeException- Since:
- 1.9.0
-
negate
public Aprational negate() throws ApfloatRuntimeException
Negative value.- Overrides:
negatein classApfloat- Returns:
-this.- Throws:
ApfloatRuntimeException- Since:
- 1.1
-
add
public Aprational add(Aprational x) throws ApfloatRuntimeException
Adds two aprational numbers.- Parameters:
x- The number to be added to this number.- Returns:
this + x.- Throws:
ApfloatRuntimeException
-
subtract
public Aprational subtract(Aprational x) throws ApfloatRuntimeException
Subtracts two aprational numbers.- Parameters:
x- The number to be subtracted from this number.- Returns:
this - x.- Throws:
ApfloatRuntimeException
-
multiply
public Aprational multiply(Aprational x) throws ApfloatRuntimeException
Multiplies two aprational numbers.- Parameters:
x- The number to be multiplied by this number.- Returns:
this * x.- Throws:
ApfloatRuntimeException
-
divide
public Aprational divide(Aprational x) throws java.lang.ArithmeticException, ApfloatRuntimeException
Divides two aprational numbers.- 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 Aprational mod(Aprational x) throws ApfloatRuntimeException
Calculates the remainder when divided by an aprational. 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
-
floor
public Apint floor() throws ApfloatRuntimeException
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:
floorin classApfloat- Returns:
- This aprational 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 aprational and is equal to a mathematical integer.- Overrides:
ceilin classApfloat- Returns:
- This aprational rounded towards positive infinity.
- Throws:
ApfloatRuntimeException
-
truncate
public Apint truncate() throws ApfloatRuntimeException
Truncates fractional part.- Overrides:
truncatein classApfloat- Returns:
- This aprational rounded towards zero.
- Throws:
ApfloatRuntimeException
-
frac
public Aprational frac() throws ApfloatRuntimeException
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:
fracin classApfloat- Returns:
- The fractional part of this aprational.
- Throws:
ApfloatRuntimeException- Since:
- 1.7.0
-
toRadix
public Aprational toRadix(int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Convert this aprational to the specified radix.- Overrides:
toRadixin classApfloat- 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(Aprational x)
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
public int compareTo(Apfloat x)
Compare this aprational to the specified apfloat.
-
preferCompare
public boolean preferCompare(Apfloat x)
Description copied from class:ApfloatTests if the comparison withequalsandcompareToshould be done in the opposite order.Implementations should avoid infinite recursion.
- Overrides:
preferComparein classApfloat- Parameters:
x- The number to compare to.- Returns:
trueif this object should invokex.equals(this)and-x.compareTo(this)instead of comparing normally.
-
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
Apfloat.compareTo(Apfloat).
-
test
public boolean test(Aprational x)
Tests two aprational 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.- Since:
- 1.10.0
-
test
public boolean test(Apfloat x) throws ApfloatRuntimeException
Description copied from class:ApfloatTests 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 callingApfloat.equals(Object). This method is usually significantly faster than callingequals(Object).- Overrides:
testin classApfloat- Parameters:
x- The number to test against.- Returns:
falseif the numbers are definitely not equal,trueif unknown.- Throws:
ApfloatRuntimeException
-
hashCode
public int hashCode()
Returns a hash code for this aprational.
-
toString
public java.lang.String toString()
Returns a string representation of this aprational.
-
toString
public java.lang.String toString(boolean pretty) throws ApfloatRuntimeExceptionReturns a string representation of this aprational.- Overrides:
toStringin classApfloat- 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) throws java.io.IOException, ApfloatRuntimeExceptionWrite a string representation of this aprational to aWriter.- Overrides:
writeToin classApcomplex- Parameters:
out- The outputWriter.- Throws:
java.io.IOException- In case of I/O error writing to the stream.ApfloatRuntimeException
-
writeTo
public void writeTo(java.io.Writer out, boolean pretty) throws java.io.IOException, ApfloatRuntimeExceptionWrite a string representation of this aprational to aWriter.- Overrides:
writeToin classApfloat- 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.- Specified by:
formatToin interfacejava.util.Formattable- Overrides:
formatToin 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-1for no minimum.precision- The maximum number of characters to be written to the output, or-1for no maximum.- Since:
- 1.3
- See Also:
Apfloat.formatTo(Formatter,int,int,int)
-
getImpl
protected ApfloatImpl getImpl(long precision) throws ApfloatRuntimeException
Returns anApfloatImplrepresenting the approximation of this aprational up to the requested precision.- Overrides:
getImplin classApfloat- Parameters:
precision- Precision of theApfloatImplthat is needed.- Returns:
- An
ApfloatImplrepresenting this object to the requested precision. - Throws:
ApfloatRuntimeException
-
roundAway
Apint roundAway() throws ApfloatRuntimeException
- Overrides:
roundAwayin classApfloat- Throws:
ApfloatRuntimeException
-
scale
Aprational scale(long scale)
-
abs
Aprational abs()
-
compareToHalf
int compareToHalf()
- Overrides:
compareToHalfin classApfloat
-
checkDenominator
private void checkDenominator() throws java.lang.IllegalArgumentException- Throws:
java.lang.IllegalArgumentException
-
reduce
private Aprational reduce() throws java.lang.IllegalArgumentException, ApfloatRuntimeException
- Throws:
java.lang.IllegalArgumentExceptionApfloatRuntimeException
-
ensureApprox
private Apfloat ensureApprox(long precision) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
getApprox
private Apfloat getApprox(long precision)
-
setApprox
private void setApprox(Apfloat approx)
-
getInverseDen
private Apfloat getInverseDen()
-
setInverseDen
private void setInverseDen(Apfloat inverseDen)
-
-