Class Rational
- All Implemented Interfaces:
Serializable, Comparable<Rational>, AbelianGroup<Rational>, AbelianMonoid<Rational>, Group<Rational>, Magma<Rational>, Monoid<Rational>, Field<Rational>, FieldElement<Rational>, Ring<Rational>, RingElement<Rational>, Semiring<Rational>, Set<Rational>
Rational numbers form a Field under addition and multiplication. They are always stored in reduced form (gcd(numerator, denominator) = 1) with a positive denominator.
* @author Silvere Martin-Michiellot- Since:
- 1.0
- Author:
- Gemini AI (Google DeepMind)
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns the sum of this element and another.Returns the sum of two elements (additive notation).ceil()intReturns the characteristic of this field.intbooleanTests whether this set contains the specified element.Returns a human-readable description of this set.Returns this element divided by another.Returns the quotient of two elements (division).doublebooleanfloatfloor()inthashCode()intintValue()inverse()Returns the multiplicative inverse of this element.Returns the multiplicative inverse of a non-zero element.booleanisEmpty()Returnstrueif this set contains no elements.booleanFields always have commutative multiplication.longReturns the product of this element and another.Returns the product of two elements.negate()Returns the additive inverse of this element.Returns the additive inverse (negation) of an element.static Rationalof(int value) static Rationalof(int numerator, int denominator) static Rationalof(long value) static Rationalof(long numerator, long denominator) static Rationalone()Returns the multiplicative identity (one element).Performs the binary operation on two elements.Returns the difference of this element and another.Returns the difference of two elements.toString()zero()Returns the additive identity (zero element).Methods inherited from class Number
byteValue, shortValueMethods inherited from interface AbelianGroup
isCommutativeMethods inherited from interface AbelianMonoid
identity, isCommutativeMethods inherited from interface Monoid
isAssociativeMethods inherited from interface RingElement
isOne, isZero
-
Field Details
-
ZERO
-
ONE
-
-
Method Details
-
of
-
of
-
of
-
of
-
of
-
getNumerator
-
getDenominator
-
operate
Description copied from interface:MagmaPerforms the binary operation on two elements.This is the fundamental operation of a magma. The result must be an element of this magma (closure property).
Properties: None required (not necessarily associative or commutative)
-
add
Description copied from interface:AbelianMonoidReturns the sum of two elements (additive notation).Delegates to
Magma.operate(Object, Object).- Specified by:
addin interfaceAbelianMonoid<Rational>- Parameters:
a- the first addendb- the second addend- Returns:
- a + b
-
add
Description copied from interface:RingElementReturns the sum of this element and another.- Specified by:
addin interfaceRingElement<Rational>- Parameters:
other- the element to add- Returns:
- this + other
-
zero
Description copied from interface:AbelianMonoidReturns the additive identity (zero element).Delegates to
AbelianMonoid.identity().- Specified by:
zeroin interfaceAbelianMonoid<Rational>- Specified by:
zeroin interfaceRingElement<Rational>- Returns:
- the zero element
-
negate
Description copied from interface:AbelianGroupReturns the additive inverse (negation) of an element.Satisfies: a + (-a) = (-a) + a = 0
- Specified by:
negatein interfaceAbelianGroup<Rational>- Parameters:
a- the element to negate- Returns:
- -element
- See Also:
-
negate
Description copied from interface:RingElementReturns the additive inverse of this element.- Specified by:
negatein interfaceRingElement<Rational>- Returns:
- -this
-
subtract
Description copied from interface:AbelianGroupReturns the difference of two elements.Defined as: a - b = a + (-b)
- Specified by:
subtractin interfaceAbelianGroup<Rational>- Parameters:
a- the minuendb- the subtrahend- Returns:
- a - b
- See Also:
-
subtract
Description copied from interface:RingElementReturns the difference of this element and another.Default implementation:
this.add(other.negate())- Specified by:
subtractin interfaceRingElement<Rational>- Parameters:
other- the element to subtract- Returns:
- this - other
-
multiply
Description copied from interface:SemiringReturns the product of two elements.Multiplication must be associative and distribute over addition.
-
multiply
Description copied from interface:RingElementReturns the product of this element and another.- Specified by:
multiplyin interfaceRingElement<Rational>- Parameters:
other- the element to multiply- Returns:
- this * other
-
one
Description copied from interface:SemiringReturns the multiplicative identity (one element).Satisfies: 1 × a = a × 1 = a for all elements a.
-
isMultiplicationCommutative
public boolean isMultiplicationCommutative()Description copied from interface:FieldFields always have commutative multiplication.- Specified by:
isMultiplicationCommutativein interfaceField<Rational>- Specified by:
isMultiplicationCommutativein interfaceSemiring<Rational>- Returns:
- always
true
-
inverse
Description copied from interface:FieldReturns the multiplicative inverse of a non-zero element.For element a ≠0, returns a⻹ such that: a × a⻹ = a⻹ × a = 1
Examples:
- In ℚ: inverse(2/3) = 3/2
- In â„Â: inverse(5.0) = 0.2
- In â„‚: inverse(3+4i) = (3-4i)/25
-
inverse
Description copied from interface:FieldElementReturns the multiplicative inverse of this element.For any non-zero element e: e.multiply(e.inverse()) = e.one()
- Specified by:
inversein interfaceFieldElement<Rational>- Returns:
- 1/this
-
divide
Description copied from interface:FieldReturns the quotient of two elements (division).Defined as: a ÷ b = a × b⻹
-
divide
Description copied from interface:FieldElementReturns this element divided by another.Default implementation:
this.multiply(other.inverse())- Specified by:
dividein interfaceFieldElement<Rational>- Parameters:
other- the divisor- Returns:
- this / other
-
compareTo
- Specified by:
compareToin interfaceComparable<Rational>
-
intValue
-
longValue
-
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
toString
-
equals
-
hashCode
-
characteristic
public int characteristic()Description copied from interface:FieldReturns the characteristic of this field.The characteristic is the smallest positive integer n such that:
1 + 1 + ... + 1 (n times) = 0
If no such n exists, the characteristic is 0.Examples:
- char(ℚ) = char(â„Â) = char(â„‚) = 0
- char(ðÂâ€Â½Ã¢â€šÅ¡) = p (for prime p)
- Specified by:
characteristicin interfaceField<Rational>- Returns:
- the characteristic (0 for infinite fields, p for finite fields)
-
contains
Description copied from interface:SetTests whether this set contains the specified element.This is the fundamental operation of a set - membership testing.
-
description
Description copied from interface:SetReturns a human-readable description of this set.Examples:
- "â„ (Real Numbers)"
- "ℤ/12ℤ (Integers modulo 12)"
- "{1, 2, 3, 4, 5}"
- Specified by:
descriptionin interfaceSet<Rational>- Returns:
- a description of this set
-
isEmpty
public boolean isEmpty()Description copied from interface:SetReturnstrueif this set contains no elements.The empty set (∅) is a fundamental concept in set theory. It is the unique set containing no elements.
-
abs
-
floor
-
ceil
-
fractionalPart
-
mod
-