Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Floating point modulus - the "why not"



On 2015-01-13 10:10:16 +0000, Peter Stephenson wrote:
> On Mon, 12 Jan 2015 21:38:52 -0800
> Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > I think we did the right thing, but just as a counter-point:
> > 
> > torch% print $(( 91 % 13 ))                 
> > 0
> > torch% print $(( 9.1 % 1.3 ))    
> > 1.2999999999999994
> 
> That's the same problem with floating point division as everywhere else;
> if you need to round it, you need to decide what you're rounding to and
> add half of that before you truncate downwards.  I don't think that's a
> fundamentally new effect in this one case.

The problem actually comes from the decimal to binary conversions,
not with the division (or modulo) itself: 9.1 and 1.3 are not exactly
representable. While 91 is a multiple of 13, 9.1 converted to binary
is not a multiple of 1.3 converted to binary.

-- 
Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)



Messages sorted by: Reverse Date, Date, Thread, Author