W3cubDocs

/Nim

Module rationals

This module implements rational numbers, consisting of a numerator num and a denominator den, both of type int. The denominator can not be 0.

Imports

math, hashes

Types

Rational[T] = object
  num*, den*: T
a rational number, consisting of a numerator and denominator

Procs

proc initRational[T: SomeInteger](num, den: T): Rational[T]
Create a new rational number.
proc `//`[T](num, den: T): Rational[T]
A friendlier version of initRational. Example usage:
var x = 1//3 + 1//5
proc `$`[T](x: Rational[T]): string
Turn a rational number into a string.
proc toRational[T: SomeInteger](x: T): Rational[T]
Convert some integer x to a rational number.
proc toRational(x: float; n: int = high(int) shr 32): Rational[int] {...}{.raises: [], tags: [].}

Calculates the best rational numerator and denominator that approximates to x, where the denominator is smaller than n (default is the largest possible int to give maximum resolution).

The algorithm is based on the theory of continued fractions.

import math, rationals
for i in 1..10:
  let t = (10 ^ (i+3)).int
  let x = toRational(PI, t)
  let newPI = x.num / x.den
  echo x, " ", newPI, " error: ", PI - newPI, "  ", t
proc toFloat[T](x: Rational[T]): float
Convert a rational number x to a float.
proc toInt[T](x: Rational[T]): int
Convert a rational number x to an int. Conversion rounds towards 0 if x does not contain an integer value.
proc reduce[T: SomeInteger](x: var Rational[T])
Reduce rational x.
proc `+`[T](x, y: Rational[T]): Rational[T]
Add two rational numbers.
proc `+`[T](x: Rational[T]; y: T): Rational[T]
Add rational x to int y.
proc `+`[T](x: T; y: Rational[T]): Rational[T]
Add int x to rational y.
proc `+=`[T](x: var Rational[T]; y: Rational[T])
Add rational y to rational x.
proc `+=`[T](x: var Rational[T]; y: T)
Add int y to rational x.
proc `-`[T](x: Rational[T]): Rational[T]
Unary minus for rational numbers.
proc `-`[T](x, y: Rational[T]): Rational[T]
Subtract two rational numbers.
proc `-`[T](x: Rational[T]; y: T): Rational[T]
Subtract int y from rational x.
proc `-`[T](x: T; y: Rational[T]): Rational[T]
Subtract rational y from int x.
proc `-=`[T](x: var Rational[T]; y: Rational[T])
Subtract rational y from rational x.
proc `-=`[T](x: var Rational[T]; y: T)
Subtract int y from rational x.
proc `*`[T](x, y: Rational[T]): Rational[T]
Multiply two rational numbers.
proc `*`[T](x: Rational[T]; y: T): Rational[T]
Multiply rational x with int y.
proc `*`[T](x: T; y: Rational[T]): Rational[T]
Multiply int x with rational y.
proc `*=`[T](x: var Rational[T]; y: Rational[T])
Multiply rationals y to x.
proc `*=`[T](x: var Rational[T]; y: T)
Multiply int y to rational x.
proc reciprocal[T](x: Rational[T]): Rational[T]
Calculate the reciprocal of x. (1/x)
proc `/`[T](x, y: Rational[T]): Rational[T]
Divide rationals x by y.
proc `/`[T](x: Rational[T]; y: T): Rational[T]
Divide rational x by int y.
proc `/`[T](x: T; y: Rational[T]): Rational[T]
Divide int x by Rational y.
proc `/=`[T](x: var Rational[T]; y: Rational[T])
Divide rationals x by y in place.
proc `/=`[T](x: var Rational[T]; y: T)
Divide rational x by int y in place.
proc cmp(x, y: Rational): int {...}{.procvar.}
Compares two rationals.
proc `<`(x, y: Rational): bool
proc `<=`(x, y: Rational): bool
proc `==`(x, y: Rational): bool
proc abs[T](x: Rational[T]): Rational[T]
proc `div`[T: SomeInteger](x, y: Rational[T]): T
Computes the rational truncated division.
proc `mod`[T: SomeInteger](x, y: Rational[T]): Rational[T]
Computes the rational modulo by truncated division (remainder). This is same as x - (x div y) * y.
proc floorDiv[T: SomeInteger](x, y: Rational[T]): T

Computes the rational floor division.

Floor division is conceptually defined as floor(x / y). This is different from the div operator, which is defined as trunc(x / y). That is, div rounds towards 0 and floorDiv rounds down.

proc floorMod[T: SomeInteger](x, y: Rational[T]): Rational[T]

Computes the rational modulo by floor division (modulo).

This is same as x - floorDiv(x, y) * y. This proc behaves the same as the % operator in python.

proc hash[T](x: Rational[T]): Hash
Computes hash for rational x

© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/rationals.html