Skip to content

math.fractions #

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 fraction #

fn fraction(n i64, d i64) Fraction

A factory function for creating a Fraction, adds a boundary condition to ensure that the denominator is non-zero. It automatically converts the negative denominator to positive and adjusts the numerator.

Note: Fractions created are not reduced by default.

struct Fraction #

struct Fraction {
pub:
	n          i64
	d          i64
	is_reduced bool
}

Fraction Struct

A Fraction has a numerator (n) and a denominator (d). If the user uses the helper functions in this module, then the following are guaranteed:1. If the user provides n and d with gcd(n, d) > 1, the fraction willnot be reduced automatically.2. d cannot be set to zero. The factory function will panic.3. If provided d is negative, it will be made positive. n will change as well.

fn (Fraction) str #

fn (f Fraction) str() string

To String method

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

Fraction negate method

fn (Fraction) reciprocal #

fn (f Fraction) reciprocal() Fraction

Fraction reciprocal method

fn (Fraction) reduce #

fn (f Fraction) reduce() Fraction

Fraction method which reduces the fraction

fn (Fraction) f64 #

fn (f Fraction) f64() f64

f64 converts the Fraction to 64-bit floating point

fn (Fraction) equals #

deprecated: use f1 == f2 instead
fn (f1 Fraction) equals(f2 Fraction) bool

equals returns true if both the Fractions are equal

fn (Fraction) == #

fn (f1 Fraction) == (f2 Fraction) bool

return true if f1 == f2

fn (Fraction) ge #

deprecated: use f1 >= f2 instead
fn (f1 Fraction) ge(f2 Fraction) bool

ge returns true if f1 >= f2

fn (Fraction) gt #

deprecated: use f1 > f2 instead
fn (f1 Fraction) gt(f2 Fraction) bool

gt returns true if f1 > f2

fn (Fraction) le #

deprecated: use f1 <= f2 instead
fn (f1 Fraction) le(f2 Fraction) bool

le returns true if f1 <= f2

fn (Fraction) lt #

deprecated: use f1 < f2 instead
fn (f1 Fraction) lt(f2 Fraction) bool

lt returns true if f1 < f2

fn (Fraction) < #

fn (f1 Fraction) < (f2 Fraction) bool

return true if f1 < f2