summary refs log tree commit diff stats
path: root/lib/pure/rationals.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/rationals.nim')
-rw-r--r--lib/pure/rationals.nim95
1 files changed, 48 insertions, 47 deletions
diff --git a/lib/pure/rationals.nim b/lib/pure/rationals.nim
index 76891a830..b668a9f71 100644
--- a/lib/pure/rationals.nim
+++ b/lib/pure/rationals.nim
@@ -39,7 +39,8 @@ proc toRational*[T: SomeInteger](x: T): Rational[T] =
   result.num = x
   result.den = 1
 
-proc toRational*(x: float, n: int = high(int) shr (sizeof(int) div 2 * 8)): Rational[int] =
+proc toRational*(x: float,
+                 n: int = high(int) shr (sizeof(int) div 2 * 8)): Rational[int] =
   ## Calculates the best rational numerator and denominator
   ## that approximates to `x`, where the denominator is
   ## smaller than `n` (default is the largest possible
@@ -67,9 +68,9 @@ proc toRational*(x: float, n: int = high(int) shr (sizeof(int) div 2 * 8)): Rati
     swap m22, m21
     m11 = m12 * ai + m11
     m21 = m22 * ai + m21
-    if x == float(ai): break  # division by zero
+    if x == float(ai): break # division by zero
     x = 1/(x - float(ai))
-    if x > float(high(int32)): break  # representation failure
+    if x > float(high(int32)): break # representation failure
     ai = int(x)
   result = m11 // m21
 
@@ -282,69 +283,69 @@ proc hash*[T](x: Rational[T]): Hash =
 when isMainModule:
   var
     z = Rational[int](num: 0, den: 1)
-    o = initRational(num=1, den=1)
+    o = initRational(num = 1, den = 1)
     a = initRational(1, 2)
     b = -1 // -2
     m1 = -1 // 1
     tt = 10 // 2
 
-  assert( a == a )
-  assert( (a-a) == z )
-  assert( (a+b) == o )
-  assert( (a/b) == o )
-  assert( (a*b) == 1 // 4 )
-  assert( (3/a) == 6 // 1 )
-  assert( (a/3) == 1 // 6 )
-  assert( a*b == 1 // 4 )
-  assert( tt*z == z )
-  assert( 10*a == tt )
-  assert( a*10 == tt )
-  assert( tt/10 == a  )
-  assert( a-m1 == 3 // 2 )
-  assert( a+m1 == -1 // 2 )
-  assert( m1+tt == 16 // 4 )
-  assert( m1-tt == 6 // -1 )
-
-  assert( z < o )
-  assert( z <= o )
-  assert( z == z )
-  assert( cmp(z, o) < 0 )
-  assert( cmp(o, z) > 0 )
-
-  assert( o == o )
-  assert( o >= o )
-  assert( not(o > o) )
-  assert( cmp(o, o) == 0 )
-  assert( cmp(z, z) == 0 )
-  assert( hash(o) == hash(o) )
-
-  assert( a == b )
-  assert( a >= b )
-  assert( not(b > a) )
-  assert( cmp(a, b) == 0 )
-  assert( hash(a) == hash(b) )
+  assert(a == a)
+  assert( (a-a) == z)
+  assert( (a+b) == o)
+  assert( (a/b) == o)
+  assert( (a*b) == 1 // 4)
+  assert( (3/a) == 6 // 1)
+  assert( (a/3) == 1 // 6)
+  assert(a*b == 1 // 4)
+  assert(tt*z == z)
+  assert(10*a == tt)
+  assert(a*10 == tt)
+  assert(tt/10 == a)
+  assert(a-m1 == 3 // 2)
+  assert(a+m1 == -1 // 2)
+  assert(m1+tt == 16 // 4)
+  assert(m1-tt == 6 // -1)
+
+  assert(z < o)
+  assert(z <= o)
+  assert(z == z)
+  assert(cmp(z, o) < 0)
+  assert(cmp(o, z) > 0)
+
+  assert(o == o)
+  assert(o >= o)
+  assert(not(o > o))
+  assert(cmp(o, o) == 0)
+  assert(cmp(z, z) == 0)
+  assert(hash(o) == hash(o))
+
+  assert(a == b)
+  assert(a >= b)
+  assert(not(b > a))
+  assert(cmp(a, b) == 0)
+  assert(hash(a) == hash(b))
 
   var x = 1//3
 
   x *= 5//1
-  assert( x == 5//3 )
+  assert(x == 5//3)
   x += 2 // 9
-  assert( x == 17//9 )
+  assert(x == 17//9)
   x -= 9//18
-  assert( x == 25//18 )
+  assert(x == 25//18)
   x /= 1//2
-  assert( x == 50//18 )
+  assert(x == 50//18)
 
   var y = 1//3
 
   y *= 4
-  assert( y == 4//3 )
+  assert(y == 4//3)
   y += 5
-  assert( y == 19//3 )
+  assert(y == 19//3)
   y -= 2
-  assert( y == 13//3 )
+  assert(y == 13//3)
   y /= 9
-  assert( y == 13//27 )
+  assert(y == 13//27)
 
   assert toRational(5) == 5//1
   assert abs(toFloat(y) - 0.4814814814814815) < 1.0e-7