Flusspferd API and module docs

class gmp.Rational

Description

Provides a bignum fractional class for more accurately representing numbers of the form numerator/denominator. See gmp’s mpq_class for the C++ API that is wrapped.

Includes

Constructor

new gmp.Rational(value)
 new gmp.Rational(str, base)
 new gmp.Rational(num, den)
  • value (Number | String | gmp.Base) – initial value
  • str (String) – value in a string
  • base (Number) – base to interpret str in
  • num (Integer) – numerator
  • den (Integer) – denominator

The str can be an integer like “41” or a fraction like “41/152”. The fraction must be in canonical form, or if not then gmp.Rational#canonicalize must be called.

The numerator and optional denominator are parsed the same as in constructor. White space is allowed in the string, and is simply ignored. The base can vary from 2 to 62, or if base is 0 then the leading characters are used: “0x” or “0X” for hex, “0b” or “0B” for binary, “0” for octal, or decimal otherwise. Note that this is done separately for the numerator and denominator, so for instance “0xEF/100” is “239/100”, whereas “0xEF/0x100” is “239/256”.

Instance methods

  • canonicalize #

    gmp.Rational#canonicalize() ⇒ undefined

    Put the rational into canonical form by removing any factors that are common to the numerator and denominator, and make the denominator positive.

  • get_den #

    gmp.Rational#get_den() ⇒ gmp.Integer

    Returns the denominator

  • get_num #

    gmp.Rational#get_num() ⇒ gmp.Integer

    Returns the numerator