Class ERational
- java.lang.Object
-
- com.upokecenter.numbers.ERational
-
- All Implemented Interfaces:
java.lang.Comparable<ERational>
public final class ERational extends java.lang.Object implements java.lang.Comparable<ERational>
Represents an arbitrary-precision rational number. This class can't be inherited. (The "E" stands for "extended", meaning that instances of this class can be values other than numbers proper, such as infinity and not-a-number.) In this class, a rational number consists of a numerator and denominator, each an arbitrary-precision integer (EInteger), and this class does not automatically convert rational numbers to lowest terms.Thread safety: Instances of this class are immutable, so they are inherently safe for use by multiple threads. Multiple instances of this object with the same properties are interchangeable, so they should not be compared using the "==" operator (which might only check if each side of the operator is the same instance).
-
-
Field Summary
Fields Modifier and Type Field Description static ERationalNaNA not-a-number value.static ERationalNegativeInfinityNegative infinity, less than any other number.static ERationalNegativeZeroA rational number for negative zero.static ERationalOneThe rational number one.static ERationalPositiveInfinityPositive infinity, greater than any other number.static ERationalSignalingNaNA signaling not-a-number value.static ERationalTenThe rational number ten.static ERationalZeroA rational number for zero.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ERationalAbs()Returns the absolute value of this rational number, that is, a number with the same value as this one but as a nonnegative number.ERationalAdd(int v)Adds this arbitrary-precision rational number and a 32-bit signed integer and returns the result.ERationalAdd(long v)Adds this arbitrary-precision rational number and a 64-bit signed integer and returns the result.ERationalAdd(ERational otherValue)Adds this arbitrary-precision rational number and another arbitrary-precision rational number and returns the result.intcompareTo(int intOther)Compares the mathematical value of an arbitrary-precision rational number with that of this instance.intcompareTo(long intOther)Compares the mathematical values of this object and another object, accepting NaN values.intcompareTo(ERational other)Compares the mathematical value of an arbitrary-precision rational number with that of this instance.intCompareToBinary(EFloat other)Compares an arbitrary-precision binary floating-point number with this instance.intCompareToDecimal(EDecimal other)Compares an arbitrary-precision decimal number with this instance.intCompareToTotal(ERational other)Compares the values of this object and another object, imposing a total ordering on all possible values.intCompareToTotalMagnitude(ERational other)Compares the absolute values of this object and another object, imposing a total ordering on all possible values (ignoring their signs).intCompareToValue(int intOther)Compares the mathematical value of an arbitrary-precision rational number with that of this instance.intCompareToValue(long intOther)Compares the mathematical values of this object and another object, accepting NaN values.intCompareToValue(ERational other)Compares the mathematical value of an arbitrary-precision rational number with that of this instance.ERationalCopy()Creates a copy of this arbitrary-precision rational number.ERationalCopySign(ERational other)Returns a number with the same value as this one, but copying the sign (positive or negative) of another number.static ERationalCreate(int numeratorSmall, int denominatorSmall)Creates a rational number with the given numerator and denominator.static ERationalCreate(long numeratorLong, long denominatorLong)Creates a rational number with the given numerator and denominator.static ERationalCreate(EInteger numerator, EInteger denominator)Creates a rational number with the given numerator and denominator.static ERationalCreateNaN(EInteger diag)Creates a not-a-number arbitrary-precision rational number.static ERationalCreateNaN(EInteger diag, boolean signaling, boolean negative)Creates a not-a-number arbitrary-precision rational number.ERationalDecrement()Subtracts one from an arbitrary-precision rational number.ERationalDivide(int v)Divides this arbitrary-precision rational number by a 32-bit signed integer and returns the result.ERationalDivide(long v)Divides this arbitrary-precision rational number by a 64-bit signed integer and returns the result.ERationalDivide(ERational otherValue)Divides this arbitrary-precision rational number by another arbitrary-precision rational number and returns the result.booleanequals(ERational other)Determines whether this object's numerator, denominator, and properties are equal to those of another object.booleanequals(java.lang.Object obj)Determines whether this object's numerator, denominator, and properties are equal to those of another object and that other object is an arbitrary-precision rational number.static ERationalFromBoolean(boolean boolValue)Converts a boolean value (true or false) to an arbitrary-precision rational number.static ERationalFromByte(byte inputByte)Converts a byte (from 0 to 255) to an arbitrary-precision rational number.static ERationalFromDouble(double flt)Converts a 64-bit floating-point number to a rational number.static ERationalFromDoubleBits(long value)Creates a binary rational number from a 64-bit floating-point number encoded in the IEEE 754 binary64 format.static ERationalFromEDecimal(EDecimal ef)Converts an arbitrary-precision decimal number to a rational number.static ERationalFromEFloat(EFloat ef)Converts an arbitrary-precision binary floating-point number to a rational number.static ERationalFromEInteger(EInteger bigint)Converts an arbitrary-precision integer to a rational number.static ERationalFromExtendedDecimal(EDecimal ef)Deprecated.Renamed to FromEDecimal.static ERationalFromExtendedFloat(EFloat ef)Deprecated.Renamed to FromEFloat.static ERationalFromInt16(short inputInt16)Converts a 16-bit signed integer to an arbitrary-precision rational number.static ERationalFromInt32(int inputInt32)Converts a 32-bit signed integer to an arbitrary-precision rational number.static ERationalFromInt64(long inputInt64)Converts a 64-bit signed integer to an arbitrary-precision rational number.static ERationalFromInt64AsUnsigned(long longerValue)Converts an unsigned integer expressed as a 64-bit signed integer to an arbitrary-precision rational number.static ERationalFromSingle(float flt)Converts a 32-bit binary floating-point number to a rational number.static ERationalFromSingleBits(int value)Creates a binary rational number from a 32-bit floating-point number encoded in the IEEE 754 binary32 format.static ERationalFromString(byte[] bytes)Creates a rational number from a sequence of bytes that represents a number.static ERationalFromString(byte[] bytes, int offset, int length)Creates a rational number from a sequence of bytes that represents a number.static ERationalFromString(char[] chars)Creates a rational number from a sequence ofchars that represents a number.static ERationalFromString(char[] chars, int offset, int length)Creates a rational number from a sequence ofchars that represents a number.static ERationalFromString(java.lang.String str)Creates a rational number from a text string that represents a number.static ERationalFromString(java.lang.String str, int offset, int length)Creates a rational number from a text string that represents a number.EIntegergetDenominator()Gets this object's denominator.EIntegergetNumerator()Gets this object's numerator.EIntegergetUnsignedNumerator()Gets this object's numerator with the sign removed.inthashCode()Returns the hash code for this instance.ERationalIncrement()Adds one to an arbitrary-precision rational number.booleanisFinite()Gets a value indicating whether this object is finite (not infinity or NaN).booleanIsInfinity()Gets a value indicating whether this object's value is infinity.booleanIsInteger()Returns whether this object's value is an integer.booleanIsNaN()Returns whether this object is a not-a-number value.booleanisNegative()Gets a value indicating whether this object's value is negative (including negative zero).booleanIsNegativeInfinity()Returns whether this object is negative infinity.booleanIsPositiveInfinity()Returns whether this object is positive infinity.booleanIsQuietNaN()Returns whether this object is a quiet not-a-number value.booleanIsSignalingNaN()Returns whether this object is a signaling not-a-number value (which causes an error if the value is passed to any arithmetic operation in this class).booleanisZero()Gets a value indicating whether this object's value equals 0.static ERationalMax(ERational first, ERational second)Gets the greater value between two rational numbers.static ERationalMaxMagnitude(ERational first, ERational second)Gets the greater value between two values, ignoring their signs.static ERationalMin(ERational first, ERational second)Gets the lesser value between two rational numbers.static ERationalMinMagnitude(ERational first, ERational second)Gets the lesser value between two values, ignoring their signs.ERationalMultiply(int v)Multiplies this arbitrary-precision rational number by a 32-bit signed integer and returns the result.ERationalMultiply(long v)Multiplies this arbitrary-precision rational number by a 64-bit signed integer and returns the result.ERationalMultiply(ERational otherValue)Multiplies this arbitrary-precision rational number by another arbitrary-precision rational number and returns the result.ERationalNegate()Returns a rational number with the same value as this one but with the sign reversed.ERationalRemainder(int v)Returns the remainder that would result when this arbitrary-precision rational number is divided by a 32-bit signed integer.ERationalRemainder(long v)Returns the remainder that would result when this arbitrary-precision rational number is divided by a 64-bit signed integer.ERationalRemainder(ERational otherValue)Returns the remainder that would result when this arbitrary-precision rational number is divided by another arbitrary-precision rational number.intsignum()Gets the sign of this rational number.ERationalSubtract(int v)Subtracts a 32-bit signed integer from this arbitrary-precision rational number and returns the result.ERationalSubtract(long v)Subtracts a 64-bit signed integer from this arbitrary-precision rational number and returns the result.ERationalSubtract(ERational otherValue)Subtracts an arbitrary-precision rational number from this arbitrary-precision rational number and returns the result.byteToByteChecked()Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) after converting it to an integer by discarding its fractional part.byteToByteIfExact()Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) without rounding to a different numerical value.byteToByteUnchecked()Converts this number's value to an integer (using ToEInteger), and returns the least-significant bits of that integer's two's-complement form as a byte (from 0 to 255).doubleToDouble()Converts this value to a 64-bit floating-point number.longToDoubleBits()Converts this value to its closest equivalent as a 64-bit floating-point number, expressed as an integer in the IEEE 754 binary64 format.EDecimalToEDecimal()Converts this rational number to an arbitrary-precision decimal number.EDecimalToEDecimal(EContext ctx)Converts this rational number to an arbitrary-precision decimal number and rounds the result to the given precision.EDecimalToEDecimalExactIfPossible(EContext ctx)Converts this rational number to an arbitrary-precision decimal number, but if the result would have a nonterminating decimal expansion, rounds that result to the given precision.EFloatToEFloat()Converts this rational number to a binary floating-point number.EFloatToEFloat(EContext ctx)Converts this rational number to a binary floating-point number and rounds that result to the given precision.EFloatToEFloatExactIfPossible(EContext ctx)Converts this rational number to a binary floating-point number, but if the result would have a nonterminating binary expansion, rounds that result to the given precision.EIntegerToEInteger()Converts this value to an arbitrary-precision integer by dividing the numerator by the denominator and discarding the fractional part of the result.EIntegerToEIntegerExact()Deprecated.Renamed to ToEIntegerIfExact.EIntegerToEIntegerIfExact()Converts this value to an arbitrary-precision integer, checking whether the value is an exact integer.EDecimalToExtendedDecimal()Deprecated.Renamed to ToEDecimal.EDecimalToExtendedDecimal(EContext ctx)Deprecated.Renamed to ToEDecimal.EDecimalToExtendedDecimalExactIfPossible(EContext ctx)Deprecated.Renamed to ToEDecimalExactIfPossible.EFloatToExtendedFloat()Deprecated.Renamed to ToEFloat.EFloatToExtendedFloat(EContext ctx)Deprecated.Renamed to ToEFloat.EFloatToExtendedFloatExactIfPossible(EContext ctx)Deprecated.Renamed to ToEFloatExactIfPossible.shortToInt16Checked()Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer after converting it to an integer by discarding its fractional part.shortToInt16IfExact()Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer without rounding to a different numerical value.shortToInt16Unchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 16-bit signed integer.intToInt32Checked()Converts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer after converting it to an integer by discarding its fractional part.intToInt32IfExact()Converts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer without rounding to a different numerical value.intToInt32Unchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 32-bit signed integer.longToInt64Checked()Converts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer after converting it to an integer by discarding its fractional part.longToInt64IfExact()Converts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer without rounding to a different numerical value.longToInt64Unchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 64-bit signed integer.ERationalToLowestTerms()Converts this value to its form in lowest terms.floatToSingle()Converts this value to a 32-bit binary floating-point number.intToSingleBits()Converts this value to its closest equivalent as 32-bit floating-point number, expressed as an integer in the IEEE 754 binary32 format.EIntegerToSizedEInteger(int maxBitLength)Converts this value to an arbitrary-precision integer by dividing the numerator by the denominator, discarding its fractional part, and checking whether the resulting integer overflows the given signed bit count.EIntegerToSizedEIntegerIfExact(int maxBitLength)Converts this value to an arbitrary-precision integer, only if this number's value is an exact integer and that integer does not overflow the given signed bit count.java.lang.StringtoString()Converts this object to a text string.
-
-
-
Field Detail
-
NaN
public static final ERational NaN
A not-a-number value.
-
NegativeInfinity
public static final ERational NegativeInfinity
Negative infinity, less than any other number.
-
NegativeZero
public static final ERational NegativeZero
A rational number for negative zero.
-
One
public static final ERational One
The rational number one.
-
PositiveInfinity
public static final ERational PositiveInfinity
Positive infinity, greater than any other number.
-
SignalingNaN
public static final ERational SignalingNaN
A signaling not-a-number value.
-
Ten
public static final ERational Ten
The rational number ten.
-
Zero
public static final ERational Zero
A rational number for zero.
-
-
Constructor Detail
-
ERational
@Deprecated public ERational(EInteger numerator, EInteger denominator)
Deprecated.Use the Create method instead.Initializes a new instance of theERationalclass.- Parameters:
numerator- An arbitrary-precision integer serving as the numerator.denominator- An arbitrary-precision integer serving as the denominator.- Throws:
java.lang.NullPointerException- The parameternumeratorordenominatoris null.java.lang.IllegalArgumentException- Denominator is zero.
-
-
Method Detail
-
Copy
public ERational Copy()
Creates a copy of this arbitrary-precision rational number.- Returns:
- An arbitrary-precision rational number.
-
getDenominator
public final EInteger getDenominator()
Gets this object's denominator.- Returns:
- This object's denominator.
-
isFinite
public final boolean isFinite()
Gets a value indicating whether this object is finite (not infinity or NaN).- Returns:
trueif this object is finite (not infinity or NaN); otherwise,false.
-
isNegative
public final boolean isNegative()
Gets a value indicating whether this object's value is negative (including negative zero).- Returns:
trueif this object's value is negative (including negative zero); otherwise,false.trueif this object's value is negative; otherwise,false.
-
isZero
public final boolean isZero()
Gets a value indicating whether this object's value equals 0.- Returns:
trueif this object's value equals 0; otherwise,false.trueif this object's value equals 0; otherwise,false.
-
IsInteger
public boolean IsInteger()
Returns whether this object's value is an integer.- Returns:
trueif this object's value is an integer; otherwise,false.
-
getNumerator
public final EInteger getNumerator()
Gets this object's numerator.- Returns:
- This object's numerator. If this object is a not-a-number value, returns the diagnostic information (which will be negative if this object is negative).
-
signum
public final int signum()
Gets the sign of this rational number.- Returns:
- The sign of this rational number.
-
getUnsignedNumerator
public final EInteger getUnsignedNumerator()
Gets this object's numerator with the sign removed.- Returns:
- This object's numerator. If this object is a not-a-number value, returns the diagnostic information.
-
Create
public static ERational Create(int numeratorSmall, int denominatorSmall)
Creates a rational number with the given numerator and denominator.- Parameters:
numeratorSmall- The numerator.denominatorSmall- The denominator.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.IllegalArgumentException- The denominator is zero.
-
Create
public static ERational Create(long numeratorLong, long denominatorLong)
Creates a rational number with the given numerator and denominator.- Parameters:
numeratorLong- The numerator.denominatorLong- The denominator.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.IllegalArgumentException- The denominator is zero.
-
Create
public static ERational Create(EInteger numerator, EInteger denominator)
Creates a rational number with the given numerator and denominator.- Parameters:
numerator- The numerator.denominator- The denominator.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.IllegalArgumentException- The denominator is zero.java.lang.NullPointerException- The parameternumeratorordenominatoris null.
-
CreateNaN
public static ERational CreateNaN(EInteger diag)
Creates a not-a-number arbitrary-precision rational number.- Parameters:
diag- An integer, 0 or greater, to use as diagnostic information associated with this object. If none is needed, should be zero. To get the diagnostic information from another arbitrary-precision rational number, use that object'sUnsignedNumeratorproperty.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.IllegalArgumentException- The parameterdiagis less than 0.
-
CreateNaN
public static ERational CreateNaN(EInteger diag, boolean signaling, boolean negative)
Creates a not-a-number arbitrary-precision rational number.- Parameters:
diag- An integer, 0 or greater, to use as diagnostic information associated with this object. If none is needed, should be zero. To get the diagnostic information from another arbitrary-precision rational number, use that object'sUnsignedNumeratorproperty.signaling- Whether the return value will be signaling (true) or quiet (false).negative- Whether the return value is negative.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.IllegalArgumentException- The parameterdiagis less than 0.java.lang.NullPointerException- The parameterdiagis null.
-
FromDouble
public static ERational FromDouble(double flt)
Converts a 64-bit floating-point number to a rational number. This method computes the exact value of the floating point number, not an approximation, as is often the case by converting the number to a string.The input value can be a not-a-number (NaN) value (such as
Double.NaN); however, NaN values have multiple forms that are equivalent for many applications' purposes, andDouble.NaNis only one of these equivalent forms. In fact,ERational.FromDouble(Double.NaN)could produce an object that is represented differently between DotNet and Java, becauseDouble.NaNmay have a different form in DotNet and Java (for example, the NaN value's sign may be negative in DotNet, but positive in Java). Use `IsNaN()` to determine whether an object from this class stores a NaN value of any form.- Parameters:
flt- The parameterfltis a 64-bit floating-point number.- Returns:
- A rational number with the same value as
flt.
-
FromExtendedDecimal
@Deprecated public static ERational FromExtendedDecimal(EDecimal ef)
Deprecated.Renamed to FromEDecimal.Converts an arbitrary-precision decimal number to a rational number.- Parameters:
ef- The number to convert as an arbitrary-precision decimal number.- Returns:
- An arbitrary-precision rational number.
-
FromExtendedFloat
@Deprecated public static ERational FromExtendedFloat(EFloat ef)
Deprecated.Renamed to FromEFloat.Converts an arbitrary-precision binary floating-point number to a rational number.- Parameters:
ef- The number to convert as an arbitrary-precision binary floating-point number.- Returns:
- An arbitrary-precision rational number.
-
FromEDecimal
public static ERational FromEDecimal(EDecimal ef)
Converts an arbitrary-precision decimal number to a rational number.- Parameters:
ef- The number to convert as an arbitrary-precision decimal number.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterefis null.java.lang.IllegalArgumentException- doesn't satisfy den.signum() >= 0.
-
FromEFloat
public static ERational FromEFloat(EFloat ef)
Converts an arbitrary-precision binary floating-point number to a rational number.- Parameters:
ef- The number to convert as an arbitrary-precision binary floating-point number.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterefis null.
-
FromEInteger
public static ERational FromEInteger(EInteger bigint)
Converts an arbitrary-precision integer to a rational number.- Parameters:
bigint- The number to convert as an arbitrary-precision integer.- Returns:
- The exact value of the integer as a rational number.
-
FromSingle
public static ERational FromSingle(float flt)
Converts a 32-bit binary floating-point number to a rational number. This method computes the exact value of the floating point number, not an approximation, as is often the case by converting the number to a string.The input value can be a not-a-number (NaN) value (such as
Float.NaNin DotNet or Float.NaN in Java); however, NaN values have multiple forms that are equivalent for many applications' purposes, andFloat.NaN/Float.NaNis only one of these equivalent forms. In fact,ERational.FromSingle(Float.NaN)orERational.FromSingle(Float.NaN)could produce an object that is represented differently between DotNet and Java, becauseFloat.NaN/Float.NaNmay have a different form in DotNet and Java (for example, the NaN value's sign may be negative in DotNet, but positive in Java). Use `IsNaN()` to determine whether an object from this class stores a NaN value of any form.- Parameters:
flt- The parameterfltis a 32-bit binary floating-point number.- Returns:
- A rational number with the same value as
flt.
-
FromSingleBits
public static ERational FromSingleBits(int value)
Creates a binary rational number from a 32-bit floating-point number encoded in the IEEE 754 binary32 format. This method computes the exact value of the floating point number, not an approximation, as is often the case by converting the number to a string.- Parameters:
value- A 32-bit integer encoded in the IEEE 754 binary32 format.- Returns:
- A rational number with the same floating-point value as
value.
-
FromDoubleBits
public static ERational FromDoubleBits(long value)
Creates a binary rational number from a 64-bit floating-point number encoded in the IEEE 754 binary64 format. This method computes the exact value of the floating point number, not an approximation, as is often the case by converting the number to a string.- Parameters:
value- A 64-bit integer encoded in the IEEE 754 binary64 format.- Returns:
- A rational number with the same floating-point value as
value.
-
FromString
public static ERational FromString(java.lang.String str)
Creates a rational number from a text string that represents a number. SeeFromString(string, int, int)for more information.- Parameters:
str- A string that represents a number.- Returns:
- An arbitrary-precision rational number with the same value as the given string.
- Throws:
java.lang.NumberFormatException- The parameterstris not a correctly formatted number string.
-
FromString
public static ERational FromString(java.lang.String str, int offset, int length)
Creates a rational number from a text string that represents a number.
The format of the string generally consists of:
- An optional plus sign ("+" , U+002B) or minus sign ("-", U+002D) (if '-' , the value is negative.)
- The numerator in the form of one or more digits (these digits may begin with any number of zeros).
- Optionally, "/" followed by the denominator in the form of one or more digits (these digits may begin with any number of zeros). If a denominator is not given, it's equal to 1.
The string can also be "-INF", "-Infinity", "Infinity", "INF", quiet NaN ("NaN" /"-NaN") followed by any number of digits, or signaling NaN ("sNaN" /"-sNaN") followed by any number of digits, all in any combination of upper and lower case.
All characters mentioned above are the corresponding characters in the Basic Latin range. In particular, the digits must be the basic digits 0 to 9 (U+0030 to U+0039). The string is not allowed to contain white space characters, including spaces.
- Parameters:
str- A text string, a portion of which represents a number.offset- An index starting at 0 showing where the desired portion ofstrbegins.length- The length, in code units, of the desired portion ofstr(but not more thanstr's length).- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.NumberFormatException- The parameterstris not a correctly formatted number string.java.lang.NullPointerException- The parameterstris null.java.lang.IllegalArgumentException- Eitheroffsetorlengthis less than 0 or greater thanstr's length, orstr's length minusoffsetis less thanlength.
-
FromString
public static ERational FromString(char[] chars)
Creates a rational number from a sequence ofchars that represents a number. SeeFromString(string, int, int)for more information.- Parameters:
chars- A sequence ofchars that represents a number.- Returns:
- An arbitrary-precision rational number with the same value as the
given sequence of
chars. - Throws:
java.lang.NumberFormatException- The parametercharsis not a correctly formatted sequence ofchars.
-
FromString
public static ERational FromString(char[] chars, int offset, int length)
Creates a rational number from a sequence of
chars that represents a number.The format of the sequence of
chars generally consists of:- An optional plus sign ("+" , U+002B) or minus sign ("-", U+002D) (if '-' , the value is negative.)
- The numerator in the form of one or more digits (these digits may begin with any number of zeros).
- Optionally, "/" followed by the denominator in the form of one or more digits (these digits may begin with any number of zeros). If a denominator is not given, it's equal to 1.
The sequence of
chars can also be "-INF", "-Infinity", "Infinity", "INF", quiet NaN ("NaN" /"-NaN") followed by any number of digits, or signaling NaN ("sNaN" /"-sNaN") followed by any number of digits, all in any combination of upper and lower case.All characters mentioned above are the corresponding characters in the Basic Latin range. In particular, the digits must be the basic digits 0 to 9 (U+0030 to U+0039). The sequence of
chars is not allowed to contain white space characters, including spaces.- Parameters:
chars- A sequence ofchars, a portion of which represents a number.offset- An index starting at 0 showing where the desired portion ofcharsbegins.length- The length, in code units, of the desired portion ofchars(but not more thanchars's length).- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.NumberFormatException- The parametercharsis not a correctly formatted sequence ofchars.java.lang.NullPointerException- The parametercharsis null.java.lang.IllegalArgumentException- Eitheroffsetorlengthis less than 0 or greater thanchars's length, orchars's length minusoffsetis less thanlength.
-
FromString
public static ERational FromString(byte[] bytes)
Creates a rational number from a sequence of bytes that represents a number. SeeFromString(string, int, int)for more information.- Parameters:
bytes- A sequence of bytes that represents a number.- Returns:
- An arbitrary-precision rational number with the same value as the given sequence of bytes.
- Throws:
java.lang.NumberFormatException- The parameterbytesis not a correctly formatted sequence of bytes.
-
FromString
public static ERational FromString(byte[] bytes, int offset, int length)
Creates a rational number from a sequence of bytes that represents a number.
The format of the sequence of bytes generally consists of:
- An optional plus sign ("+" , U+002B) or minus sign ("-", U+002D) (if '-' , the value is negative.)
- The numerator in the form of one or more digits (these digits may begin with any number of zeros).
- Optionally, "/" followed by the denominator in the form of one or more digits (these digits may begin with any number of zeros). If a denominator is not given, it's equal to 1.
The sequence of bytes can also be "-INF", "-Infinity", "Infinity", "INF", quiet NaN ("NaN" /"-NaN") followed by any number of digits, or signaling NaN ("sNaN" /"-sNaN") followed by any number of digits, all in any combination of upper and lower case.
All characters mentioned above are the corresponding characters in the Basic Latin range. In particular, the digits must be the basic digits 0 to 9 (U+0030 to U+0039). The sequence of bytes is not allowed to contain white space characters, including spaces.
- Parameters:
bytes- A sequence of bytes, a portion of which represents a number.offset- An index starting at 0 showing where the desired portion ofbytesbegins.length- The length, in code units, of the desired portion ofbytes(but not more thanbytes's length).- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.NumberFormatException- The parameterbytesis not a correctly formatted sequence of bytes.java.lang.NullPointerException- The parameterbytesis null.java.lang.IllegalArgumentException- Eitheroffsetorlengthis less than 0 or greater thanbytes's length, orbytes's length minusoffsetis less thanlength.
-
CompareToTotalMagnitude
public int CompareToTotalMagnitude(ERational other)
Compares the absolute values of this object and another object, imposing a total ordering on all possible values (ignoring their signs). In this method:- For objects with the same value, the one with the higher denominator has a greater "absolute value".
- Negative zero and positive zero are considered equal.
- Quiet NaN has a higher "absolute value" than signaling NaN. If both objects are quiet NaN or both are signaling NaN, the one with the higher diagnostic information has a greater "absolute value".
- NaN has a higher "absolute value" than infinity.
- Infinity has a higher "absolute value" than any finite number.
- Parameters:
other- An arbitrary-precision rational number to compare with this one.- Returns:
- The number 0 if both objects have the same value, or -1 if this object is less than the other value, or 1 if this object is greater. This implementation returns a positive number if.
-
CompareToTotal
public int CompareToTotal(ERational other)
Compares the values of this object and another object, imposing a total ordering on all possible values. In this method:- For objects with the same value, the one with the higher denominator has a greater "absolute value".
- Negative zero is less than positive zero.
- Quiet NaN has a higher "absolute value" than signaling NaN. If both objects are quiet NaN or both are signaling NaN, the one with the higher diagnostic information has a greater "absolute value".
- NaN has a higher "absolute value" than infinity.
- Infinity has a higher "absolute value" than any finite number.
- Negative numbers are less than positive numbers.
- Parameters:
other- An arbitrary-precision rational number to compare with this one.- Returns:
- The number 0 if both objects have the same value, or -1 if this object is less than the other value, or 1 if this object is greater. This implementation returns a positive number if.
-
Abs
public ERational Abs()
Returns the absolute value of this rational number, that is, a number with the same value as this one but as a nonnegative number.- Returns:
- An arbitrary-precision rational number.
-
Add
public ERational Add(ERational otherValue)
Adds this arbitrary-precision rational number and another arbitrary-precision rational number and returns the result.- Parameters:
otherValue- Another arbitrary-precision rational number.- Returns:
- The sum of the two numbers, that is, this arbitrary-precision rational number plus another arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterotherValueis null.
-
compareTo
public int compareTo(ERational other)
Compares the mathematical value of an arbitrary-precision rational number with that of this instance. This method currently uses the rules given in the CompareToValue method, so that it it is not consistent with the Equals method, but it may change in a future version to use the rules for the CompareToTotal method instead.- Specified by:
compareToin interfacejava.lang.Comparable<ERational>- Parameters:
other- An arbitrary-precision rational number.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater. This implementation returns a positive number if.
-
CompareToValue
public int CompareToValue(ERational other)
Compares the mathematical value of an arbitrary-precision rational number with that of this instance. In this method, NaN values are greater than any other ERational value, and two NaN values (even if their payloads differ) are treated as equal by this method. This method is not consistent with the Equals method.- Parameters:
other- An arbitrary-precision rational number.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater. This implementation returns a positive number if.
-
Max
public static ERational Max(ERational first, ERational second)
Gets the greater value between two rational numbers.- Parameters:
first- An arbitrary-precision rational number.second- Another arbitrary-precision rational number.- Returns:
- The larger value of the two numbers. If one is positive zero and the other is negative zero, returns the positive zero. If the two numbers are positive and have the same value, returns the one with the larger denominator. If the two numbers are negative and have the same value, returns the one with the smaller denominator.
- Throws:
java.lang.NullPointerException- The parameterfirstorsecondis null.
-
MaxMagnitude
public static ERational MaxMagnitude(ERational first, ERational second)
Gets the greater value between two values, ignoring their signs. If the absolute values are equal, has the same effect as Max.- Parameters:
first- The first value to compare.second- The second value to compare.- Returns:
- The larger value of the two numbers, ignoring their signs.
- Throws:
java.lang.NullPointerException- The parameterfirstorsecondis null.
-
Min
public static ERational Min(ERational first, ERational second)
Gets the lesser value between two rational numbers.- Parameters:
first- The first value to compare.second- The second value to compare.- Returns:
- The smaller value of the two numbers. If one is positive zero and the other is negative zero, returns the negative zero. If the two numbers are positive and have the same value, returns the one with the smaller denominator. If the two numbers are negative and have the same value, returns the one with the larger denominator.
- Throws:
java.lang.NullPointerException- The parameterfirstorsecondis null.
-
MinMagnitude
public static ERational MinMagnitude(ERational first, ERational second)
Gets the lesser value between two values, ignoring their signs. If the absolute values are equal, has the same effect as Min.- Parameters:
first- The first value to compare.second- The second value to compare.- Returns:
- The smaller value of the two numbers, ignoring their signs.
- Throws:
java.lang.NullPointerException- The parameterfirstorsecondis null.
-
compareTo
public int compareTo(int intOther)
Compares the mathematical value of an arbitrary-precision rational number with that of this instance. This method currently uses the rules given in the CompareToValue method, so that it it is not consistent with the Equals method, but it may change in a future version to use the rules for the CompareToTotal method instead.- Parameters:
intOther- The parameterintOtheris a 32-bit signed integer.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater.
-
CompareToValue
public int CompareToValue(int intOther)
Compares the mathematical value of an arbitrary-precision rational number with that of this instance. In this method, NaN values are greater than any other ERational value, and two NaN values (even if their payloads differ) are treated as equal by this method. This method is not consistent with the Equals method.- Parameters:
intOther- The parameterintOtheris a 32-bit signed integer.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater.
-
CompareToValue
public int CompareToValue(long intOther)
Compares the mathematical values of this object and another object, accepting NaN values.This method is not consistent with the Equals method because two different numbers with the same mathematical value, but different exponents, will compare as equal.
In this method, negative zero and positive zero are considered equal.
If this object is a quiet NaN or signaling NaN, this method will not trigger an error. Instead, NaN will compare greater than any other number, including infinity.
- Parameters:
intOther- The parameterintOtheris a 64-bit signed integer.- Returns:
- Less than 0 if this object's value is less than the other value, or greater than 0 if this object's value is greater than the other value, or 0 if both values are equal.
-
compareTo
public int compareTo(long intOther)
Compares the mathematical values of this object and another object, accepting NaN values. This method currently uses the rules given in the CompareToValue method, so that it it is not consistent with the Equals method, but it may change in a future version to use the rules for the CompareToTotal method instead.- Parameters:
intOther- The parameterintOtheris a 64-bit signed integer.- Returns:
- Less than 0 if this object's value is less than the other value, or greater than 0 if this object's value is greater than the other value, or 0 if both values are equal.
-
CompareToBinary
public int CompareToBinary(EFloat other)
Compares an arbitrary-precision binary floating-point number with this instance. In this method, NaN values are greater than any other ERational or EFloat value, and two NaN values (even if their payloads differ) are treated as equal by this method.- Parameters:
other- An arbitrary-precision binary floating-point number.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater. This implementation returns a positive number if.
-
CompareToDecimal
public int CompareToDecimal(EDecimal other)
Compares an arbitrary-precision decimal number with this instance.- Parameters:
other- An arbitrary-precision decimal number.- Returns:
- Zero if the values are equal; a negative number if this instance is less, or a positive number if this instance is greater. This implementation returns a positive number if.
-
CopySign
public ERational CopySign(ERational other)
Returns a number with the same value as this one, but copying the sign (positive or negative) of another number.- Parameters:
other- A number whose sign will be copied.- Returns:
- An arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterotheris null.
-
Divide
public ERational Divide(ERational otherValue)
Divides this arbitrary-precision rational number by another arbitrary-precision rational number and returns the result.- Parameters:
otherValue- An arbitrary-precision rational number.- Returns:
- The result of dividing this arbitrary-precision rational number by another arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterotherValueis null.
-
equals
public boolean equals(java.lang.Object obj)
Determines whether this object's numerator, denominator, and properties are equal to those of another object and that other object is an arbitrary-precision rational number. Not-a-number values are considered equal if the rest of their properties are equal. This is not the same as value equality. Notably, two ERationals with the same value, but of which one is in lowest terms and the other is not, are compared as unequal by this method (example: 1/2 vs. 5/10).- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- The parameterobjis an arbitrary object.- Returns:
trueif the objects are equal; otherwise,false. In this method, two objects are not equal if they don't have the same type or if one is null and the other isn't.
-
equals
public boolean equals(ERational other)
Determines whether this object's numerator, denominator, and properties are equal to those of another object. Not-a-number values are considered equal if the rest of their properties are equal.- Parameters:
other- An arbitrary-precision rational number to compare to.- Returns:
- Either
trueorfalse.
-
hashCode
public int hashCode()
Returns the hash code for this instance. No application or process IDs are used in the hash code calculation.- Overrides:
hashCodein classjava.lang.Object- Returns:
- A 32-bit signed integer.
-
IsInfinity
public boolean IsInfinity()
Gets a value indicating whether this object's value is infinity.- Returns:
trueif this object's value is infinity; otherwise,false.
-
IsNaN
public boolean IsNaN()
Returns whether this object is a not-a-number value.- Returns:
trueif this object is a not-a-number value; otherwise,false.
-
IsNegativeInfinity
public boolean IsNegativeInfinity()
Returns whether this object is negative infinity.- Returns:
trueif this object is negative infinity; otherwise,false.
-
IsPositiveInfinity
public boolean IsPositiveInfinity()
Returns whether this object is positive infinity.- Returns:
trueif this object is positive infinity; otherwise,false.
-
IsQuietNaN
public boolean IsQuietNaN()
Returns whether this object is a quiet not-a-number value.- Returns:
trueif this object is a quiet not-a-number value; otherwise,false.
-
IsSignalingNaN
public boolean IsSignalingNaN()
Returns whether this object is a signaling not-a-number value (which causes an error if the value is passed to any arithmetic operation in this class).- Returns:
trueif this object is a signaling not-a-number value (which causes an error if the value is passed to any arithmetic operation in this class); otherwise,false.
-
Multiply
public ERational Multiply(ERational otherValue)
Multiplies this arbitrary-precision rational number by another arbitrary-precision rational number and returns the result.- Parameters:
otherValue- An arbitrary-precision rational number.- Returns:
- The product of the two numbers, that is, this arbitrary-precision rational number times another arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterotherValueis null.
-
Negate
public ERational Negate()
Returns a rational number with the same value as this one but with the sign reversed.- Returns:
- An arbitrary-precision rational number.
-
Remainder
public ERational Remainder(ERational otherValue)
Returns the remainder that would result when this arbitrary-precision rational number is divided by another arbitrary-precision rational number.- Parameters:
otherValue- An arbitrary-precision rational number.- Returns:
- The remainder that would result when this arbitrary-precision rational number is divided by another arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterotherValueis null.
-
Subtract
public ERational Subtract(ERational otherValue)
Subtracts an arbitrary-precision rational number from this arbitrary-precision rational number and returns the result.- Parameters:
otherValue- An arbitrary-precision rational number.- Returns:
- The difference between the two numbers, that is, this arbitrary-precision rational number minus another arbitrary-precision rational number.
- Throws:
java.lang.NullPointerException- The parameterotherValueis null.
-
ToDouble
public double ToDouble()
Converts this value to a 64-bit floating-point number. The half-even rounding mode is used.- Returns:
- The closest 64-bit floating-point number to this value. The return value can be positive infinity or negative infinity if this value exceeds the range of a 64-bit floating point number.
-
ToDoubleBits
public long ToDoubleBits()
Converts this value to its closest equivalent as a 64-bit floating-point number, expressed as an integer in the IEEE 754 binary64 format. The half-even rounding mode is used.If this value is a NaN, sets the high bit of the 64-bit floating point number's significand area for a quiet NaN, and clears it for a signaling NaN. Then the other bits of the significand area are set to the lowest bits of this object's unsigned significand, and the next-highest bit of the significand area is set if those bits are all zeros and this is a signaling NaN.
- Returns:
- The closest 64-bit binary floating-point number to this value, expressed as an integer in the IEEE 754 binary64 format. The return value can be positive infinity or negative infinity if this value exceeds the range of a 64-bit floating point number.
-
ToSingleBits
public int ToSingleBits()
Converts this value to its closest equivalent as 32-bit floating-point number, expressed as an integer in the IEEE 754 binary32 format. The half-even rounding mode is used.If this value is a NaN, sets the high bit of the 32-bit floating point number's significand area for a quiet NaN, and clears it for a signaling NaN. Then the other bits of the significand area are set to the lowest bits of this object's unsigned significand, and the next-highest bit of the significand area is set if those bits are all zeros and this is a signaling NaN.
- Returns:
- The closest 32-bit binary floating-point number to this value, expressed as an integer in the IEEE 754 binary32 format. The return value can be positive infinity or negative infinity if this value exceeds the range of a 32-bit floating point number.
-
ToLowestTerms
public ERational ToLowestTerms()
Converts this value to its form in lowest terms. For example, (8/4) becomes (4/1).- Returns:
- An arbitrary-precision rational with the same value as this one but in lowest terms. Returns this object if it is infinity or NaN. Returns ERational.NegativeZero if this object is a negative zero. Returns ERational.Zero if this object is a positive zero.
- Throws:
java.lang.ArithmeticException- This object's value is infinity or not-a-number (NaN).
-
ToSizedEInteger
public EInteger ToSizedEInteger(int maxBitLength)
Converts this value to an arbitrary-precision integer by dividing the numerator by the denominator, discarding its fractional part, and checking whether the resulting integer overflows the given signed bit count.- Parameters:
maxBitLength- The maximum number of signed bits the integer can have. The integer's value may not be less than -(2^maxBitLength) or greater than (2^maxBitLength) - 1.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.ArithmeticException- This object's value is infinity or not-a-number (NaN), or this number's value, once converted to an integer by dividing the numerator by the denominator and discarding its fractional part, is less than -(2^maxBitLength) or greater than (2^maxBitLength) - 1.
-
ToSizedEIntegerIfExact
public EInteger ToSizedEIntegerIfExact(int maxBitLength)
Converts this value to an arbitrary-precision integer, only if this number's value is an exact integer and that integer does not overflow the given signed bit count.- Parameters:
maxBitLength- The maximum number of signed bits the integer can have. The integer's value may not be less than -(2^maxBitLength) or greater than (2^maxBitLength) - 1.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.ArithmeticException- This object's value is infinity or not-a-number (NaN), or this number's value as an integer is less than -(2^maxBitLength) or greater than (2^maxBitLength) - 1.java.lang.ArithmeticException- This object's value is not an exact integer.
-
ToEInteger
public EInteger ToEInteger()
Converts this value to an arbitrary-precision integer by dividing the numerator by the denominator and discarding the fractional part of the result.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.ArithmeticException- This object's value is infinity or not-a-number (NaN).
-
ToEIntegerExact
@Deprecated public EInteger ToEIntegerExact()
Deprecated.Renamed to ToEIntegerIfExact.Converts this value to an arbitrary-precision integer, checking whether the value is an exact integer.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.ArithmeticException- This object's value is infinity or not-a-number (NaN).
-
ToEIntegerIfExact
public EInteger ToEIntegerIfExact()
Converts this value to an arbitrary-precision integer, checking whether the value is an exact integer.- Returns:
- An arbitrary-precision integer.
- Throws:
java.lang.ArithmeticException- This object's value is infinity or not-a-number (NaN).
-
ToEDecimal
public EDecimal ToEDecimal()
Converts this rational number to an arbitrary-precision decimal number.- Returns:
- The exact value of the rational number, or not-a-number (NaN) if the result can't be exact because it has a nonterminating decimal expansion.
-
ToEDecimal
public EDecimal ToEDecimal(EContext ctx)
Converts this rational number to an arbitrary-precision decimal number and rounds the result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The value of the rational number, rounded to the given precision. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating decimal expansion.
-
ToEDecimalExactIfPossible
public EDecimal ToEDecimalExactIfPossible(EContext ctx)
Converts this rational number to an arbitrary-precision decimal number, but if the result would have a nonterminating decimal expansion, rounds that result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. This context will be used only if the exact result would have a nonterminating decimal expansion. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The exact value of the rational number if possible; otherwise, the rounded version of the result if a context is given. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating decimal expansion.
-
ToExtendedDecimal
@Deprecated public EDecimal ToExtendedDecimal()
Deprecated.Renamed to ToEDecimal.Converts this rational number to an arbitrary-precision decimal number.- Returns:
- The exact value of the rational number, or not-a-number (NaN) if the result can't be exact because it has a nonterminating decimal expansion.
-
ToExtendedDecimal
@Deprecated public EDecimal ToExtendedDecimal(EContext ctx)
Deprecated.Renamed to ToEDecimal.Converts this rational number to an arbitrary-precision decimal number and rounds the result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The value of the rational number, rounded to the given precision. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating decimal expansion.
-
ToExtendedDecimalExactIfPossible
@Deprecated public EDecimal ToExtendedDecimalExactIfPossible(EContext ctx)
Deprecated.Renamed to ToEDecimalExactIfPossible.Converts this rational number to an arbitrary-precision decimal number, but if the result would have a nonterminating decimal expansion, rounds that result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. This context will be used only if the exact result would have a nonterminating decimal expansion. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The exact value of the rational number if possible; otherwise, the rounded version of the result if a context is given. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating decimal expansion.
-
ToEFloat
public EFloat ToEFloat()
Converts this rational number to a binary floating-point number.- Returns:
- The exact value of the rational number, or not-a-number (NaN) if the result can't be exact because it has a nonterminating binary expansion.
-
ToEFloat
public EFloat ToEFloat(EContext ctx)
Converts this rational number to a binary floating-point number and rounds that result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The value of the rational number, rounded to the given precision. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating binary expansion.
-
ToEFloatExactIfPossible
public EFloat ToEFloatExactIfPossible(EContext ctx)
Converts this rational number to a binary floating-point number, but if the result would have a nonterminating binary expansion, rounds that result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. This context will be used only if the exact result would have a nonterminating binary expansion. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The exact value of the rational number if possible; otherwise, the rounded version of the result if a context is given. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating binary expansion.
-
ToExtendedFloat
@Deprecated public EFloat ToExtendedFloat()
Deprecated.Renamed to ToEFloat.Converts this rational number to a binary floating-point number.- Returns:
- The exact value of the rational number, or not-a-number (NaN) if the result can't be exact because it has a nonterminating binary expansion.
-
ToExtendedFloat
@Deprecated public EFloat ToExtendedFloat(EContext ctx)
Deprecated.Renamed to ToEFloat.Converts this rational number to a binary floating-point number and rounds that result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The value of the rational number, rounded to the given precision. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating binary expansion.
-
ToExtendedFloatExactIfPossible
@Deprecated public EFloat ToExtendedFloatExactIfPossible(EContext ctx)
Deprecated.Renamed to ToEFloatExactIfPossible.Converts this rational number to a binary floating-point number, but if the result would have a nonterminating binary expansion, rounds that result to the given precision.- Parameters:
ctx- An arithmetic context object to control the precision, rounding, and exponent range of the result. This context will be used only if the exact result would have a nonterminating binary expansion. If HasFlags of the context is true, will also store the flags resulting from the operation (the flags are in addition to the pre-existing flags). Can be null, in which case the precision is unlimited and no rounding is needed.- Returns:
- The exact value of the rational number if possible; otherwise, the rounded version of the result if a context is given. Returns not-a-number (NaN) if the context is null and the result can't be exact because it has a nonterminating binary expansion.
-
ToSingle
public float ToSingle()
Converts this value to a 32-bit binary floating-point number. The half-even rounding mode is used.- Returns:
- The closest 32-bit binary floating-point number to this value. The return value can be positive infinity or negative infinity if this value exceeds the range of a 32-bit floating point number.
-
toString
public java.lang.String toString()
Converts this object to a text string.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of this object. If this object's value is
infinity or not-a-number, the result is the analogous return value
of the
EDecimal.toStringmethod. Otherwise, the return value has the following form:[-]numerator.Divide(denominator).
-
Increment
public ERational Increment()
Adds one to an arbitrary-precision rational number.- Returns:
- The given arbitrary-precision rational number plus one.
-
Decrement
public ERational Decrement()
Subtracts one from an arbitrary-precision rational number.- Returns:
- The given arbitrary-precision rational number minus one.
-
Add
public ERational Add(int v)
Adds this arbitrary-precision rational number and a 32-bit signed integer and returns the result.- Parameters:
v- A 32-bit signed integer.- Returns:
- The sum of the two numbers, that is, this arbitrary-precision rational number plus a 32-bit signed integer.
-
Subtract
public ERational Subtract(int v)
Subtracts a 32-bit signed integer from this arbitrary-precision rational number and returns the result.- Parameters:
v- The parametervis a 32-bit signed integer.- Returns:
- The difference between the two numbers, that is, this arbitrary-precision rational number minus a 32-bit signed integer.
-
Multiply
public ERational Multiply(int v)
Multiplies this arbitrary-precision rational number by a 32-bit signed integer and returns the result.- Parameters:
v- The parametervis a 32-bit signed integer.- Returns:
- The product of the two numbers, that is, this arbitrary-precision rational number times a 32-bit signed integer.
-
Divide
public ERational Divide(int v)
Divides this arbitrary-precision rational number by a 32-bit signed integer and returns the result.- Parameters:
v- The parametervis a 32-bit signed integer.- Returns:
- The result of dividing this arbitrary-precision rational number by a 32-bit signed integer.
- Throws:
java.lang.ArithmeticException- The parametervis zero.
-
Remainder
public ERational Remainder(int v)
Returns the remainder that would result when this arbitrary-precision rational number is divided by a 32-bit signed integer.- Parameters:
v- The divisor.- Returns:
- The remainder that would result when this arbitrary-precision rational number is divided by a 32-bit signed integer.
- Throws:
java.lang.IllegalArgumentException- The parametervis zero.
-
Add
public ERational Add(long v)
Adds this arbitrary-precision rational number and a 64-bit signed integer and returns the result.- Parameters:
v- A 64-bit signed integer.- Returns:
- The sum of the two numbers, that is, this arbitrary-precision rational number plus a 64-bit signed integer.
-
Subtract
public ERational Subtract(long v)
Subtracts a 64-bit signed integer from this arbitrary-precision rational number and returns the result.- Parameters:
v- The parametervis a 64-bit signed integer.- Returns:
- The difference between the two numbers, that is, this arbitrary-precision rational number minus a 64-bit signed integer.
-
Multiply
public ERational Multiply(long v)
Multiplies this arbitrary-precision rational number by a 64-bit signed integer and returns the result.- Parameters:
v- The parametervis a 64-bit signed integer.- Returns:
- The product of the two numbers, that is, this arbitrary-precision rational number times a 64-bit signed integer.
-
Divide
public ERational Divide(long v)
Divides this arbitrary-precision rational number by a 64-bit signed integer and returns the result.- Parameters:
v- The parametervis a 64-bit signed integer.- Returns:
- The result of dividing this arbitrary-precision rational number by a 64-bit signed integer.
- Throws:
java.lang.ArithmeticException- The parametervis zero.
-
Remainder
public ERational Remainder(long v)
Returns the remainder that would result when this arbitrary-precision rational number is divided by a 64-bit signed integer.- Parameters:
v- The divisor.- Returns:
- The remainder that would result when this arbitrary-precision rational number is divided by a 64-bit signed integer.
- Throws:
java.lang.IllegalArgumentException- The parametervis zero.
-
ToByteChecked
public byte ToByteChecked()
Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a byte (from 0 to 255).
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than 0 or greater than 255.
-
ToByteUnchecked
public byte ToByteUnchecked()
Converts this number's value to an integer (using ToEInteger), and returns the least-significant bits of that integer's two's-complement form as a byte (from 0 to 255).- Returns:
- This number, converted to a byte (from 0 to 255). Returns 0 if this value is infinity or not-a-number.
-
ToByteIfExact
public byte ToByteIfExact()
Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) without rounding to a different numerical value.- Returns:
- This number's value as a byte (from 0 to 255).
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than 0 or greater than 255.
-
FromByte
public static ERational FromByte(byte inputByte)
Converts a byte (from 0 to 255) to an arbitrary-precision rational number.- Parameters:
inputByte- The number to convert as a byte (from 0 to 255).- Returns:
- This number's value as an arbitrary-precision rational number.
-
ToInt16Checked
public short ToInt16Checked()
Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a 16-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than -32768 or greater than 32767.
-
ToInt16Unchecked
public short ToInt16Unchecked()
Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 16-bit signed integer.- Returns:
- This number, converted to a 16-bit signed integer. Returns 0 if this value is infinity or not-a-number.
-
ToInt16IfExact
public short ToInt16IfExact()
Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer without rounding to a different numerical value.- Returns:
- This number's value as a 16-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than -32768 or greater than 32767.
-
FromInt16
public static ERational FromInt16(short inputInt16)
Converts a 16-bit signed integer to an arbitrary-precision rational number.- Parameters:
inputInt16- The number to convert as a 16-bit signed integer.- Returns:
- This number's value as an arbitrary-precision rational number.
-
ToInt32Checked
public int ToInt32Checked()
Converts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a 32-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than -2147483648 or greater than 2147483647.
-
ToInt32Unchecked
public int ToInt32Unchecked()
Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 32-bit signed integer.- Returns:
- This number, converted to a 32-bit signed integer. Returns 0 if this value is infinity or not-a-number.
-
ToInt32IfExact
public int ToInt32IfExact()
Converts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer without rounding to a different numerical value.- Returns:
- This number's value as a 32-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than -2147483648 or greater than 2147483647.
-
FromBoolean
public static ERational FromBoolean(boolean boolValue)
Converts a boolean value (true or false) to an arbitrary-precision rational number.- Parameters:
boolValue- Either true or false.- Returns:
- The number 1 if
boolValueis true; otherwise, 0.
-
FromInt32
public static ERational FromInt32(int inputInt32)
Converts a 32-bit signed integer to an arbitrary-precision rational number.- Parameters:
inputInt32- The number to convert as a 32-bit signed integer.- Returns:
- This number's value as an arbitrary-precision rational number.
-
ToInt64Checked
public long ToInt64Checked()
Converts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a 64-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than -9223372036854775808 or greater than 9223372036854775807.
-
ToInt64Unchecked
public long ToInt64Unchecked()
Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 64-bit signed integer.- Returns:
- This number, converted to a 64-bit signed integer. Returns 0 if this value is infinity or not-a-number.
-
ToInt64IfExact
public long ToInt64IfExact()
Converts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer without rounding to a different numerical value.- Returns:
- This number's value as a 64-bit signed integer.
- Throws:
java.lang.ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than -9223372036854775808 or greater than 9223372036854775807.
-
FromInt64AsUnsigned
public static ERational FromInt64AsUnsigned(long longerValue)
Converts an unsigned integer expressed as a 64-bit signed integer to an arbitrary-precision rational number.- Parameters:
longerValue- A 64-bit signed integer. If this value is 0 or greater, the return value will represent it. If this value is less than 0, the return value will store 2^64 plus this value instead.- Returns:
- An arbitrary-precision rational number. If
longerValueis 0 or greater, the return value will represent it. IflongerValueis less than 0, the return value will store 2^64 plus this value instead.
-
FromInt64
public static ERational FromInt64(long inputInt64)
Converts a 64-bit signed integer to an arbitrary-precision rational number.- Parameters:
inputInt64- The number to convert as a 64-bit signed integer.- Returns:
- This number's value as an arbitrary-precision rational number.
-
-