modTest.frink

Download or view modTest.frink in plain text format


// This tests the modulus operator.

trouble = 2^31

for x = trouble - 5 to trouble + 5
   for y = trouble - 5 to trouble + 5
      test[x,y]

for x = -trouble - 5 to -trouble + 5
   for y = trouble - 5 to trouble + 5
      test[x,y]

for x = trouble - 5 to trouble + 5
   for y = -trouble - 5 to -trouble + 5
      test[x,y]

for x = -trouble - 5 to -trouble + 5
   for y = -trouble - 5 to -trouble + 5
      test[x,y]

for x = -trouble - 5 to -trouble + 5
   for y = -5 to 5
   {
      if y != 0
         test[x,y]
      if x != 0
         test[y,x]
   }      

setPrecision[24]

a = [1, 2, 10, 1000, 1000000000001, 1203923809824398, 23019832098432,
     2^32, 2^31-1, 2^31, 2^31+1, 1/2, 1/3, 2/3, 1/7, 3/5, 9/1000000000009, 100000000000034/5,
     23/10101111101010100101, 1023221329384084044/29, 1.2, 1/3.,
     1.109230002139802138, -3209432049832409e10, 329032.213908e-8, 1.4e30,
     3/1ee25 ] 

for n = a
   for m = a
   {
      test[ n, m]
      test[ n,-m]
      test[-n,-m]
      test[-n, m]
   }

test[x,y] :=
{
   r = x mod y
   if signum[r] != signum[y]  and signum[r] != 0
      println["ERROR in signum:   $x mod $y == $r"]
   
   if y > 0
   {
      if ! ((0 <= r) && (r <= y))
         println["ERROR in range:    $x mod $y == $r"]
   } else
   {
      if ! ((0 >= r) && (r >= y))
         println["ERROR in range:    $x mod $y == $r"]
   }
   
   if abs[(x div y) * y + r - x] > 1e-17 max[abs[x], abs[y]]
   {
      println["ERROR in reverse:  $x mod $y == $r,  $x div $y == " + (x div y)]
      println["  error is: " + ((x div y) * y + r) - x]
      println["  floor[x/y] = " + floor[x/y] + ", y * floor[x/y] = " + y floor[x/y]]
      println[]
   }
}


Download or view modTest.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 12 hours, 3 minutes ago.