math.fractions #
math.fractions
math.fractions provides exact rational arithmetic.
Existing i64 API
import math.fractions
f := fractions.fraction(4, 8)
assert f.reduce().str() == '1/2'
Generic backends
fractions.Rational[T] supports signed builtin integers and math.big.Integer.
import math.big
import math.fractions
huge := big.integer_from_string('1000000000000000000000000000000')!
f := fractions.big_fraction(huge, big.integer_from_int(3))
g := fractions.rational(i32(5), i32(10))
assert f.reduce().str() == '1000000000000000000000000000000/3'
assert g.reduce().str() == '1/2'
fn approximate #
fn approximate(val f64) Fraction
approximate returns a Fraction that approcimates the given value to within the default epsilon value (1.0e-4). This means the result will be accurate to 3 places after the decimal.
fn approximate_with_eps #
fn approximate_with_eps(val f64, eps f64) Fraction
approximate_with_eps returns a Fraction
fn big_fraction #
fn big_fraction(n big.Integer, d big.Integer) Rational[big.Integer]
big_fraction creates a math.big.Integer-backed Rational.
The denominator must be non-zero. Negative denominators are normalized so that the stored denominator is always positive.
fn fraction #
fn fraction(n i64, d i64) Fraction
fraction creates an i64-backed Fraction.
The denominator must be non-zero. Negative denominators are normalized so that the stored denominator is always positive.
fn rational #
fn rational[T](n T, d T) Rational[T]
rational creates a Rational backed by the concrete numeric type T.
Fractions created this way are not reduced automatically, but is_reduced reflects whether the input was already in lowest terms.
fn (Rational[T]) str #
fn (f Rational[T]) str() string
str returns the fraction in n/d form.
fn (Rational[T]) + #
fn (f1 Rational[T]) + (f2 Rational[T]) Rational[T]
Fraction add using operator overloading.
fn (Rational[T]) - #
fn (f1 Rational[T]) - (f2 Rational[T]) Rational[T]
Fraction subtract using operator overloading.
fn (Rational[T]) * #
fn (f1 Rational[T]) * (f2 Rational[T]) Rational[T]
Fraction multiply using operator overloading.
fn (Rational[T]) / #
fn (f1 Rational[T]) / (f2 Rational[T]) Rational[T]
Fraction divide using operator overloading.
fn (Rational[T]) negate #
fn (f Rational[T]) negate() Rational[T]
negate returns the additive inverse of the Rational.
fn (Rational[T]) reciprocal #
fn (f Rational[T]) reciprocal() Rational[T]
reciprocal returns the reciprocal of the Rational.
fn (Rational[T]) reduce #
fn (f Rational[T]) reduce() Rational[T]
reduce returns the Rational reduced to lowest terms.
fn (Rational[T]) f64 #
fn (f Rational[T]) f64() f64
f64 converts the Rational to 64-bit floating point.
fn (Rational[T]) == #
fn (f1 Rational[T]) == (f2 Rational[T]) bool
return true if f1 == f2.
fn (Rational[T]) < #
fn (f1 Rational[T]) < (f2 Rational[T]) bool
return true if f1 < f2.
struct Fraction #
struct Fraction {
pub:
n i64
d i64
is_reduced bool
}
Fraction stores a numerator and denominator using the existing i64 backend.
fn (Fraction) str #
fn (f Fraction) str() string
str returns the fraction in n/d form.
fn (Fraction) + #
fn (f1 Fraction) + (f2 Fraction) Fraction
Fraction add using operator overloading.
fn (Fraction) - #
fn (f1 Fraction) - (f2 Fraction) Fraction
Fraction subtract using operator overloading.
fn (Fraction) * #
fn (f1 Fraction) * (f2 Fraction) Fraction
Fraction multiply using operator overloading.
fn (Fraction) / #
fn (f1 Fraction) / (f2 Fraction) Fraction
Fraction divide using operator overloading.
fn (Fraction) negate #
fn (f Fraction) negate() Fraction
negate returns the additive inverse of the Fraction.
fn (Fraction) reciprocal #
fn (f Fraction) reciprocal() Fraction
reciprocal returns the reciprocal of the Fraction.
fn (Fraction) reduce #
fn (f Fraction) reduce() Fraction
reduce returns the Fraction reduced to lowest terms.
fn (Fraction) f64 #
fn (f Fraction) f64() f64
f64 converts the Fraction to 64-bit floating point.
fn (Fraction) == #
fn (f1 Fraction) == (f2 Fraction) bool
return true if f1 == f2.
fn (Fraction) < #
fn (f1 Fraction) < (f2 Fraction) bool
return true if f1 < f2.
struct Rational #
struct Rational[T] {
pub:
n T
d T
is_reduced bool
}
Rational stores a numerator and denominator using the backend type T.
Supported backend types are signed builtin integers and math.big.Integer.