I would really like to see ($x div $y) be (floor($x/$y)) and ($x mod $y) be
($x - $x div $y). If the divisor is positive the modulus should be
positive, no matter what the sign of the dividend. Avoids lots of special
case code across 0 boundaries.
On 2005-05-23 18:49, TSa (Thomas Sandlaß) [EMAIL PROTECTED]
wrote:
[EMAIL PROTECTED] wrote:
There are actuall two usefull definition for %. The first which Ada calls
'mod' always returns a value 0=XN and yes it has no working value that is
an identity. The other which Ada calls 'rem' defined as follows:
Signed integer division and remainder are defined by the relation:
A = (A/B)*B + (A rem B)
where (A rem B) has the sign of A and an absolute value less than the
absolute value of B. Signed integer division satisfies the identity:
(-A)/B = -(A/B) = A/(-B)
It does have a right side identity of +INF.
This is the truncating div-dominant definition of modulo.
The eulerian definition is mod-dominant and nicely handles
non-integer values. E.g.
3.2 == 1.5 * 2 + 0.2 -+-- 3.2 / 1.5 == 2 + 0.2 / 1.5 == 2 + 1/15
| == 2 + 0.1333...
+-- 3.2 % 1.5 == 0.2
Note that -3.2 == -4 + 0.8 == -4.5 + 1.3 == ...
-3.2 / 1.5 == -3 + 1.3 / 1.5 == -3 + 0.8666... == -2.1333
-3.2 % 1.5 == 1.3
With integers:
8 / 3 == (2 + 2/3) == 2
8 / (-3) == -(2 + 2/3) == -2
(-8) / 3 == -(3 - 1/3) == -3 # this might surprise some people ;)
(-8) / (-3) == (3 - 1/3) == 3
8 % (-3) == 8 % 3 == 2
(-8) % (-3) == (-8) % 3 == 1 # this as well, but it's just -3 * 3 + 1
Real valued division can be considered as % 0, that is infinite precision.
While integer arithmetic is % 1. I.e. int $x == $x - $x % 1.
floor $x == $x - $x % 1# -1.2 - (-1.2) % 1 == -1.2 - 0.8 == -2
ceil $x == 1 + floor $x
round $x == floor( $x + 0.5 )
trunc $x == $x 0 ?? ceil $x :: floor $x
To @Larry: how are mod and div defined in Perl6?