On this page
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
Types
-
Rational[T] = object num*, den*: T - a rational number, consisting of a numerator and denominator Source Edit
Procs
-
proc initRational[T: SomeInteger](num, den: T): Rational[T] - Create a new rational number. Source Edit
-
proc `//`[T](num, den: T): Rational[T] -
A friendlier version of
initRational. Example usage:
Source Editvar x = 1//3 + 1//5 -
proc `$`[T](x: Rational[T]): string - Turn a rational number into a string. Source Edit
-
proc toRational[T: SomeInteger](x: T): Rational[T] -
Convert some integer
xto a rational number. Source Edit -
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 thann(default is the largest possible int to give maximum resolution).The algorithm is based on the theory of continued fractions.
Source Editimport 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
xto a float. Source Edit -
proc toInt[T](x: Rational[T]): int -
Convert a rational number
xto an int. Conversion rounds towards 0 ifxdoes not contain an integer value. Source Edit -
proc reduce[T: SomeInteger](x: var Rational[T]) -
Reduce rational
x. Source Edit -
proc `+`[T](x, y: Rational[T]): Rational[T] - Add two rational numbers. Source Edit
-
proc `+`[T](x: Rational[T]; y: T): Rational[T] -
Add rational
xto inty. Source Edit -
proc `+`[T](x: T; y: Rational[T]): Rational[T] -
Add int
xto rationaly. Source Edit -
proc `+=`[T](x: var Rational[T]; y: Rational[T]) -
Add rational
yto rationalx. Source Edit -
proc `+=`[T](x: var Rational[T]; y: T) -
Add int
yto rationalx. Source Edit -
proc `-`[T](x: Rational[T]): Rational[T] - Unary minus for rational numbers. Source Edit
-
proc `-`[T](x, y: Rational[T]): Rational[T] - Subtract two rational numbers. Source Edit
-
proc `-`[T](x: Rational[T]; y: T): Rational[T] -
Subtract int
yfrom rationalx. Source Edit -
proc `-`[T](x: T; y: Rational[T]): Rational[T] -
Subtract rational
yfrom intx. Source Edit -
proc `-=`[T](x: var Rational[T]; y: Rational[T]) -
Subtract rational
yfrom rationalx. Source Edit -
proc `-=`[T](x: var Rational[T]; y: T) -
Subtract int
yfrom rationalx. Source Edit -
proc `*`[T](x, y: Rational[T]): Rational[T] - Multiply two rational numbers. Source Edit
-
proc `*`[T](x: Rational[T]; y: T): Rational[T] -
Multiply rational
xwith inty. Source Edit -
proc `*`[T](x: T; y: Rational[T]): Rational[T] -
Multiply int
xwith rationaly. Source Edit -
proc `*=`[T](x: var Rational[T]; y: Rational[T]) -
Multiply rationals
ytox. Source Edit -
proc `*=`[T](x: var Rational[T]; y: T) -
Multiply int
yto rationalx. Source Edit -
proc reciprocal[T](x: Rational[T]): Rational[T] -
Calculate the reciprocal of
x. (1/x) Source Edit -
proc `/`[T](x, y: Rational[T]): Rational[T] -
Divide rationals
xbyy. Source Edit -
proc `/`[T](x: Rational[T]; y: T): Rational[T] -
Divide rational
xby inty. Source Edit -
proc `/`[T](x: T; y: Rational[T]): Rational[T] -
Divide int
xby Rationaly. Source Edit -
proc `/=`[T](x: var Rational[T]; y: Rational[T]) -
Divide rationals
xbyyin place. Source Edit -
proc `/=`[T](x: var Rational[T]; y: T) -
Divide rational
xby intyin place. Source Edit -
proc cmp(x, y: Rational): int - Compares two rationals. Source Edit
-
proc `<`(x, y: Rational): bool - Source Edit
-
proc `<=`(x, y: Rational): bool - Source Edit
-
proc `==`(x, y: Rational): bool - Source Edit
-
proc abs[T](x: Rational[T]): Rational[T] - Source Edit
-
proc `div`[T: SomeInteger](x, y: Rational[T]): T - Computes the rational truncated division. Source Edit
-
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. Source Edit -
proc floorDiv[T: SomeInteger](x, y: Rational[T]): T -
Computes the rational floor division.
Floor division is conceptually defined as
Source Editfloor(x / y). This is different from thedivoperator, which is defined astrunc(x / y). That is,divrounds towards0andfloorDivrounds down. -
proc floorMod[T: SomeInteger](x, y: Rational[T]): Rational[T] -
Computes the rational modulo by floor division (modulo).
This is same as
Source Editx - 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
xSource Edit
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/rationals.html