[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modulo infinity
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 3 Apr 2014 22:44:40 -0300
> a = 100
> b = a / 3
> print(a/b, a%b, math.fmod(a,b))
>
> This prints "3 0 33.333333333333". I have no idea why, but it breaks the
> guarantee that a=floor(a/b)*b+a%b
Because 100/3 has no exact representation, a/b can result in something
a little smaller than 3 (in which case its floor is 2) or something a
little larger than 3 (in which case its floor is 3). With floor(a/b)=2,
fmod gives 33.33333; with floor=3, a%b gives 0. (I would say that a%b
sounds more correct than fmod(a,b) in this case.)
> By that reason, though, it's a bug for N%math.huge to return anything other
> than N since 0=floor(N/math.huge)
It is correct that floor(N/math.huge)=0, but 0*math.huge is not zero in IEEE
arithmetic; it is NaN. So, floor(N/math.huge)*math.huge gives NaN.
-- Roberto