[fpc-devel] -1 mod 3
m2 at ellipsa.net
Wed Nov 5 23:18:03 CET 2008
Schatzl Thomas a écrit :
>> Datum: Wed, 05 Nov 2008 18:04:01 +0100
>> Von: "Vinzent Höfler" <JeLlyFish.software at gmx.net>
>>> Unless they make use of Ada.
>> Which just has both, that's all. ;)
>> As an alternative one could read some other language reference manuals
>> and notice that - surprisingly - at least two of the more common "real
>> world languages" don't even have a "modulo" operation:
>> ISO/IEC9899:1999: (C99)
>> So, IMO somebody at Borland got it wrong, just because someone at Intel
>> did it that way - and this has definitely and absolutely nothing to do
>> with a "computer science implementation".
> Just because it's called "remainder" in other languages (and if you had actually read the specs ;), the result in these languages is still the same as for the FPC "mod" operator.
> E.g. both read, having the exact same wording in both specs, "...produces a result value such that (a/b)*b+(a%b) is equal to a."
> I.e. for a=-1 and b=3 the result is -1 following from a%b = a - (a/b)*b for both C99 and Java.
Which could solve the problem assuming A/B has only one solution. But
this is untrue. With real operands, i.e., not integer ones, there is one
solution (when B <> 0, of course). But with integer operands, if A is
not a multiple of B, there are two possible quotients. And none is
particular, both agree with the math definition :
A = B*q + r with |r| < |B|
For instance, for 13/7, the 2 possible quotients are 1 and 2 because
13 = 7 * 1 + 6
13 = 7 * 2 - 1
Now, that FPC returns -1 for -1 mod 3 is not very important. If
one needs a Ada-like behaviour, the code is easy to write.
More information about the fpc-devel