bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] GAWK 4.1.60 DIV() Remainder Bug


From: Katherine Wasserman
Subject: [bug-gawk] GAWK 4.1.60 DIV() Remainder Bug
Date: Sun, 03 Aug 2014 17:53:05 -0400

In version 4.1.60 of GAWK the div() function returns remainders that seem wrong, or at least inconsistent with the standard mod (%) function.
This only happens with negative numerators.  Programming languages vary on the proper way to deal with this, so I'm not going to suggest that one calculation or the other is correct.  However the behavior for bignums and regular integers should at least be the same.

Here's a demo of the problem:

Running this program (remainder.awk):
--------------------------------------------------------------------
BEGIN {
test(15,7)
test(-15,7)
test(15,-7)
test(-15,-7)
}


function test(x,y, r)
{
  print x,y ":"
  print "normal quotient " int(x/y)
  print "normal remainder " x%y

  div(x,y,r)
  for (i in r)
    print "bignum " i " " r[i]

}

--------------------------------------------------------------------
gawk -M -f remainder.awk

results in:
15 7:
normal quotient 2
normal remainder 1
bignum quotient 2
bignum remainder 1
-15 7:
normal quotient -2
normal remainder 6
bignum quotient -2
bignum remainder -1
15 -7:
normal quotient -2
normal remainder 1
bignum quotient -2
bignum remainder 1
-15 -7:
normal quotient 2
normal remainder 6
bignum quotient 2
bignum remainder -1


Thanks,
Katie

reply via email to

[Prev in Thread] Current Thread [Next in Thread]