IsOdd: Bug?

Raphael Collet raphael.collet at uclouvain.be
Tue Mar 18 08:56:59 CET 2008


Dear Torsten,

What you observe is a feature of the operator 'mod', which may return a 
negative value.  In math, X mod N is always in the range [0, N-1], even 
for negative values of X.  But most computers don't follow that rule, 
and return -1 for -5 mod 2, for instance.

There is, however, a simple fix in the case of IsOdd.  Just compare the 
result to 0 instead of 1 (and ~1):

    fun {IsOdd X} X mod 2 \= 0 end

Cheers,
raph

Torsten Anders wrote:
> Dear all,
> 
> the function IsOdd currently always returns false for negative numbers. 
> Is there some reason for this? Otherwise I would change the current 
> implementation
> 
>   fun {IsOdd X}  X mod 2 == 1 end
> 
> into
> 
>  fun {IsOdd X}  {Abs X mod 2} == 1 end
> 
> Thanks!
> 
> Best
> Torsten
> 
> PS: thanks to Wolfgang for his code navigator: finding the source of 
> IsOdd only took a single key stroke 
> :)_________________________________________________________________________________ 
> 
> mozart-hackers mailing list                           
> mozart-hackers at mozart-oz.org     
> http://www.mozart-oz.org/mailman/listinfo/mozart-hackers



More information about the mozart-hackers mailing list