GMP.Q
Defined in gmp-fix@0.12.0
Provides multiple-precision rational number type MPQ and related functions.
Create an MPQ by mpq from an I64 numerator and denominator, by init_set_str from a string, or
by init_set_z from an MPZ. To make the ratio of two MPZ values, make each an MPQ and divide.
Every function returns an MPQ in canonical form: the numerator and the denominator have no common
factor, and the denominator is positive.
MPQ implements Add, Sub, Mul, Div, Eq, LessThan, ToString and Format, so the usual
operators work and format writes it as to_string does.
let x = mpq(1, 3) + mpq(1, 6);
assert_eq(|_|"", x, mpq(1, 2));;
assert_eq(|_|"", x.to_string, "1/2");;
assert_eq(|_|"", mpq(4, -6).to_string, "-2/3");;
assert_eq(|_|"", "x = {}".format((x,)), "x = 1/2");;
assert_eq(|_|"", (x / mpq(3, 4)).get_num, mpz(2));;
assert_eq(|_|"", init_set_z(mpz(6)) / init_set_z(mpz(4)), mpq(3, 2));;
assert(|_|"", mpq(2, 3) < mpq(3, 4))
Repository: https://github.com/tttmmmyyyy/fixlang-gmp
Values
namespace GMP.Q
c_SiZE_OF_MPQ
Type: Std::FFI::CSizeT
Size of __mpq_struct.
namespace GMP.Q::MPQ
abs
Type: GMP.Q::MPQ -> GMP.Q::MPQ
Returns the absolute value of the MPQ value.
Parameters
num: TheMPQvalue to be converted to its absolute value
cmp
Type: GMP.Q::MPQ -> GMP.Q::MPQ -> Std::I64
Compares two MPQ values.
Returns
- a negative if
lhsis less thanrhs, - zero if they are equal,
- a positive if
lhsis greater thanrhs.
Parameters
lhs: The left-hand sideMPQvalue.rhs: The right-hand sideMPQvalue.
cmp_z
Type: GMP.Q::MPQ -> GMP.Z::MPZ -> Std::I64
Compares an MPQ value with an MPZ value.
Returns
- a negative if
lhsis less thanrhs, - zero if they are equal,
- a positive if
lhsis greater thanrhs.
Parameters
lhs: The left-hand sideMPQvalue.rhs: The right-hand sideMPZvalue.
div_2exp
Type: Std::U64 -> GMP.Q::MPQ -> GMP.Q::MPQ
Divides the MPQ value by 2^exp.
Parameters
exp: The exponent of 2.num: TheMPQvalue to be divided.
equal
Type: GMP.Q::MPQ -> GMP.Q::MPQ -> Std::Bool
Checks if two MPQ values are equal.
Parameters
lhs: The left-hand sideMPQvalue.rhs: The right-hand sideMPQvalue.
get_d
Type: GMP.Q::MPQ -> Std::F64
Converts a MPZ value to an F64 value.
For details, see the document of mpz_get_d in GMP.
Parameters
num: TheMPQvalue to be converted to F64.
get_den
Type: GMP.Q::MPQ -> GMP.Z::MPZ
Gets the denominator of the MPQ value.
Parameters
num: TheMPQvalue from which to get the denominator.
get_num
Type: GMP.Q::MPQ -> GMP.Z::MPZ
Gets the numerator of the MPQ value.
Parameters
num: TheMPQvalue from which to get the numerator.
get_str
Type: Std::I64 -> GMP.Q::MPQ -> Std::String
Get the string representation of a MPQ value in the given base.
init_set_d
Type: Std::F64 -> GMP.Q::MPQ
Creates a new MPQ value from a floating-point number.
Parameters
f: The floating-point number to be converted toMPQ.
init_set_si
Type: Std::I64 -> Std::U64 -> GMP.Q::MPQ
Creates a new MPQ value from I64 numerator and denominator.
Parameters
num: The numerator as anI64value.den: The denominator as anU64value.
init_set_str
Type: Std::String -> Std::I64 -> Std::Option GMP.Q::MPQ
Creates new MPQ value from a string representation.
Parameters
str: The string representation of the rational number.base: The base of the string representation.
Examples
assert_eq(|_|"", init_set_str("6/4", 10).as_some, mpq(3, 2));;
assert_eq(|_|"", init_set_str("-5", 10).as_some, mpq(-5, 1));;
assert(|_|"", init_set_str("1/2/3", 10).is_none)
init_set_ui
Type: Std::U64 -> Std::U64 -> GMP.Q::MPQ
Creates a new MPQ value from an U64 value.
Parameters
num: The numerator as anU64value.den: The denominator as anU64value.
init_set_z
Type: GMP.Z::MPZ -> GMP.Q::MPQ
Creates a MPQ value by an MPZ value.
Parameters
val: TheMPZvalue to be converted to `MPQ
inv
Type: GMP.Q::MPQ -> GMP.Q::MPQ
Returns the multiplicative inverse of the MPQ value.
Parameters
num: TheMPQvalue to be inverted.
mpq
Type: Std::I64 -> Std::I64 -> GMP.Q::MPQ
Creates a new MPQ value from I64 numerator and denominator.
Parameters
num: The numerator as anI64value.den: The denominator as anI64value.
mul_2exp
Type: Std::U64 -> GMP.Q::MPQ -> GMP.Q::MPQ
Multiplies the MPQ value by 2^exp.
Parameters
exp: The exponent of 2.num: TheMPQvalue to be multiplied.
set_d
Type: Std::F64 -> GMP.Q::MPQ -> GMP.Q::MPQ
Sets the MPQ value to a floating-point number.
Parameters
f: The floating-point number to set.q: TheMPQvalue to be set.
set_si
Type: Std::I64 -> Std::U64 -> GMP.Q::MPQ -> GMP.Q::MPQ
Sets the MPQ value to a rational number represented by an I64 numerator and an U64 denominator.
Parameters
num: The numerator as anI64value.den: The denominator as anU64value.q: TheMPQvalue to be set.
set_str
Type: Std::String -> Std::I64 -> GMP.Q::MPQ -> Std::Option GMP.Q::MPQ
Sets the MPQ value from a string representation.
Parameters
str: The string representation of the rational number.base: The base of the string representation.q: TheMPQvalue to be set.
set_ui
Type: Std::U64 -> Std::U64 -> GMP.Q::MPQ -> GMP.Q::MPQ
Sets the MPQ value to a rational number represented by an U64 numerator and an U64 denominator.
Parameters
num: The numerator as anU64value.den: The denominator as anU64value.q: TheMPQvalue to be set.
set_z
Type: GMP.Z::MPZ -> GMP.Q::MPQ -> GMP.Q::MPQ
Sets the MPQ value to an MPZ value.
Parameters
z: TheMPZvalue to be set.q: TheMPQvalue to be set.
sgn
Type: GMP.Q::MPQ -> Std::I64
Returns the sign of a MPQ value.
Returns
-1ifn < 00ifn == 01ifn > 0
Types and aliases
namespace GMP.Q
MPQ
Defined as: type MPQ = unbox struct { ...fields... }
Rational number type, represented by a pair of MPZ values (numerator and denominator).
MPQHandle
Defined as: type MPQHandle = Std::Ptr
A pointer to struct __mpq_struct.
Traits and aliases
Trait implementations
impl GMP.Q::MPQ : Hash::Hash
impl GMP.Q::MPQ : Std::Add
impl GMP.Q::MPQ : Std::Div
impl GMP.Q::MPQ : Std::Eq
impl GMP.Q::MPQ : Std::Format
Writes the decimal text, such as -2/3, as to_string does.