Package eu.bandm.tools.ops
Class Rational
java.lang.Object
java.lang.Number
eu.bandm.tools.ops.Rational
- All Implemented Interfaces:
Serializable,Comparable<Rational>
Rational numbers with unbounded precision.
All instances returned by this code are always cancelled and normalized. (Internal intermediate data not necessarily.)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final GenMonoid<BigInteger, Rational> evidentstatic final GenMonoid<BigInteger, Rational> evidentstatic final Rationalevidentstatic final Rationalevidentprotected static final Map<Integer, BigInteger> Cache for powers of ten.static final Rationalevidentstatic final Rationalevident -
Method Summary
Modifier and TypeMethodDescriptionabs()Return the absolute (positive) valueReturn the normalized addition result.additiveName(@Opt String separator) Shows integer part and fractional rest separately.Return rounded value.byteReturn rounded value.intdivide(long factor) Return the normalized division result.Return the normalized division result.doubleReturns division result.booleanevidentfloatReturns division result.final BigIntegerevidentfinal BigIntegerevidentinthashCode()evidentintintValue()Return rounded value.int[]Returns enumerator and denominator.inverse()Return the normalized inverse result.final booleanevidentfinal booleanevidentfinal booleanevidentfinal booleanisOne()evidentfinal booleanevidentfinal booleanisZero()evidentlongReturn rounded value.Return the larger of this and x.Return the smaller of this and x.Returns the fractional rest of the division by x, with "this/x" defined as with integer division, namely rounding towards zero, not towards negative infinity.multiply(long factor) Return the normalized multiplication result.Return the normalized multiplication result.negate()Return the normalized negation.static final RationalofAdditive(String n) Parser of the commonplace "additive" writing.power(int n) Return the normalized power of this to the exponent n.shortReturn rounded value.intsignum()Return the signum information -1,0,+1Return the normalized subtraction result.protected static BigIntegertenPower(int exp) Calculates 10^exp.toString()evidentstatic final RationalvalueOf(long n) evidentstatic final RationalvalueOf(long n, long d) evidentstatic final RationalParser.static final Rationalevidentstatic final Rationalevident.static final RationalvalueOf(BigInteger n, BigInteger d) evident
-
Field Details
-
ZERO
evident -
ONE
evident -
TWO
evident -
ONE_HALF
evident -
powerOfTen
Cache for powers of ten. -
addition
evident -
multiplication
evident
-
-
Method Details
-
valueOf
evident -
getNumerator
evident -
getDenominator
evident -
isInteger
public final boolean isInteger()evident -
isZero
public final boolean isZero()evident -
isOne
public final boolean isOne()evident -
isPositive
public final boolean isPositive()evident -
isNegative
public final boolean isNegative()evident -
isNatural
public final boolean isNatural()evident -
valueOf
Parser. The string must be either a plain integer, according toInteger.parseInt(String). Or it contains exactly one "/" sign, separating both such strings. The string after the "/" must not have a sign symbol.- Throws:
NumberFormatException
-
ofAdditive
Parser of the commonplace "additive" writing. The string must either be a rational representation corresponding tovalueOf(String), or must be a plain integer (according toInteger.parseInt(String)) followed by one single blank and a representation of a Rational which (a) contains a "/" sign, (b) has no sign symbol and (c) has a value v with0 < v < 1.- Throws:
NumberFormatException
-
valueOf
evident. -
tenPower
Calculates 10^exp. Uses the local cache. -
valueOf
evident -
valueOf
evident -
valueOf
evident -
bigIntegerValue
Return rounded value. Implies rounding towards zero, according to Java default rules. Relies onBigInteger.divide(BigInteger). See Java specification. -
byteValue
public byte byteValue()Return rounded value. -
shortValue
public short shortValue()Return rounded value.- Overrides:
shortValuein classNumber- See Also:
-
intValue
public int intValue()Return rounded value. -
intValues
public int[] intValues()Returns enumerator and denominator. For the conversion of the interal BigIntegers seeBigInteger.intValue(). -
longValue
public long longValue()Return rounded value. -
floatValue
public float floatValue()Returns division result. For the conversion of the internal BigIntegers seeBigInteger.floatValue().- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()Returns division result. For the conversion of the internal BigIntegers seeBigInteger.doubleValue().- Specified by:
doubleValuein classNumber
-
add
Return the normalized addition result. -
subtract
Return the normalized subtraction result. -
negate
Return the normalized negation. -
multiply
Return the normalized multiplication result. -
multiply
Return the normalized multiplication result. -
divide
Return the normalized division result. -
divide
Return the normalized division result. -
inverse
Return the normalized inverse result. -
power
Return the normalized power of this to the exponent n. -
modulo
Returns the fractional rest of the division by x, with "this/x" defined as with integer division, namely rounding towards zero, not towards negative infinity. This method behaves like java integer%, cf https://docs.oracle.com/javase/specs/jls/se22/html/jls-15.html#jls-15.17.3.5%3 produces 2 (note that 5/3 produces 1) 5%(-3) produces 2 (note that 5/(-3) produces -1) (-5)%3 produces -2 (note that (-5)/3 produces -1) (-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)
-
signum
public int signum()Return the signum information -1,0,+1 -
abs
Return the absolute (positive) value -
min
Return the smaller of this and x. -
max
Return the larger of this and x. -
equals
evident -
compareTo
- Specified by:
compareToin interfaceComparable<Rational>
-
hashCode
public int hashCode()evident -
toString
evident -
additiveName
Shows integer part and fractional rest separately. If integer part == 0, then the simpletoString()is returned. Otheriwse, if value < 0, then the represenation of the negation is inclosed in-(..).- Parameters:
separator- string set between integer and fractional part, defaults to » « (= one blank).
-