I’m porting Smalltalk to Motoko and I’ve found a semantic difference between Motoko’s %
operator and the same %
operator in Smalltalk. For Motoko’s %
operator to return the same result as Smalltalk’s it should, for integers, return a % b = a - (if (a/b > 0) a / b else a/b - 1) * b
and for Floats
a % b = a - Float.floor(a/b)*b
. It seems to me that the definition in Smalltalk is more consistent and coherent than Motoko’s.
Thanks for reporting this.
I’m actually not sure what the correct answer should be but I’ve opened an issue below.
We probably just expose the Wasm semantics, but would need to check.
%
is supposed to make sense in rings, but Float
is assumed to be a field so every division is without remainder. So I am a bit surprised we have that for Float
s at all
In both Smalltalk and Motoko you can write: 9.2 % 3.9
and, in this case, the result is the same: 1.3999999999999995
. Smalltalk starts to differ from Motoko when one of the arguments is negative.