develooper Front page | perl.perl6.language | Postings from May 2005

Re: mod/div

Thread Previous | Thread Next
From:
=?ISO-8859-1?Q?=22TSa_=28Thomas_Sandla=DF=29=22?=
Date:
May 30, 2005 02:15
Subject:
Re: mod/div
Message ID:
429AD9C4.40503@orthogon.com
Mark Reed wrote:
> I would really like to see ($x div $y) be (floor($x/$y))

That is: floor( 8 / (-3) ) == floor( -2.6666.... ) == -3
Or do you want -2?

> and ($x mod $y) be ($x - $x div $y).

Hmm, since 8 - (-3) == 11 this definition hardly works.
But even with $q = floor( $x / $y ) and $r = $x - $q * $y
we get 8 - (-3) * (-3) == -1 where I guess you expect -2.
Looks like you want some case distinction there on the
sign of $x.


> If the divisor is positive the modulus should be
> positive, no matter what the sign of the dividend.

The problem is that with two numbers $x and $y you get
four combinations of signs which must be considered when
calculating $q and $r such that $x == $q * $y + $r holds.


> Avoids lots of special case code across 0 boundaries.

If there is a definition that needs no special casing
then it is the euclidean definition that 0 <= $r < abs $y.
-- 
TSa (Thomas SandlaƟ)


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About