This module implements rational numbers, consisting of a numerator num and a denominator den, both of type int. The denominator can not be 0.
Types
Rational[T] = object num*, den*: T
- a rational number, consisting of a numerator and denominator Source
Procs
proc initRational[T](num, den: T): Rational[T]
- Create a new rational number. Source
proc `//`[T](num, den: T): Rational[T]
-
A friendlier version of initRational. Example usage:
var x = 1//3 + 1//5
Source proc `$`[T](x: Rational[T]): string
- Turn a rational number into a string. Source
proc toRational[T](x: T): Rational[T]
- Convert some integer x to a rational number. Source
proc toRational(x: float; n: int = high(int)): Rational[int] {.raises: [], tags: [].}
-
Calculate 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 Farey sequence named after John Farey
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
Source proc toFloat[T](x: Rational[T]): float
- Convert a rational number x to a float. Source
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. Source
proc reduce[T](x: var Rational[T])
- Reduce rational x. Source
proc `+`[T](x, y: Rational[T]): Rational[T]
- Add two rational numbers. Source
proc `+`[T](x: Rational[T]; y: T): Rational[T]
- Add rational x to int y. Source
proc `+`[T](x: T; y: Rational[T]): Rational[T]
- Add int x to rational y. Source
proc `+=`[T](x: var Rational[T]; y: Rational[T])
- Add rational y to rational x. Source
proc `+=`[T](x: var Rational[T]; y: T)
- Add int y to rational x. Source
proc `-`[T](x: Rational[T]): Rational[T]
- Unary minus for rational numbers. Source
proc `-`[T](x, y: Rational[T]): Rational[T]
- Subtract two rational numbers. Source
proc `-`[T](x: Rational[T]; y: T): Rational[T]
- Subtract int y from rational x. Source
proc `-`[T](x: T; y: Rational[T]): Rational[T]
- Subtract rational y from int x. Source
proc `-=`[T](x: var Rational[T]; y: Rational[T])
- Subtract rational y from rational x. Source
proc `-=`[T](x: var Rational[T]; y: T)
- Subtract int y from rational x. Source
proc `*`[T](x, y: Rational[T]): Rational[T]
- Multiply two rational numbers. Source
proc `*`[T](x: Rational[T]; y: T): Rational[T]
- Multiply rational x with int y. Source
proc `*`[T](x: T; y: Rational[T]): Rational[T]
- Multiply int x with rational y. Source
proc `*=`[T](x: var Rational[T]; y: Rational[T])
- Multiply rationals y to x. Source
proc `*=`[T](x: var Rational[T]; y: T)
- Multiply int y to rational x. Source
proc reciprocal[T](x: Rational[T]): Rational[T]
- Calculate the reciprocal of x. (1/x) Source
proc `/`[T](x, y: Rational[T]): Rational[T]
- Divide rationals x by y. Source
proc `/`[T](x: Rational[T]; y: T): Rational[T]
- Divide rational x by int y. Source
proc `/`[T](x: T; y: Rational[T]): Rational[T]
- Divide int x by Rational y. Source
proc `/=`[T](x: var Rational[T]; y: Rational[T])
- Divide rationals x by y in place. Source
proc `/=`[T](x: var Rational[T]; y: T)
- Divide rational x by int y in place. Source
proc cmp[Rational](x, y: Rational): int {.procvar.}
- Compares two rationals. Source
proc `<`[Rational](x, y: Rational): bool
- Source
proc `<=`[Rational](x, y: Rational): bool
- Source
proc `==`[Rational](x, y: Rational): bool
- Source
proc abs[T](x: Rational[T]): Rational[T]
- Source
proc hash[T](x: Rational[T]): Hash
- Computes hash for rational x Source