zsh-workers
 help / color / mirror / code / Atom feed
* Floating point modulus - the "why not"
@ 2015-01-13  5:38 Bart Schaefer
  2015-01-13 10:10 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2015-01-13  5:38 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Floating point modulus - the "why not"
  2015-01-13  5:38 Floating point modulus - the "why not" Bart Schaefer
@ 2015-01-13 10:10 ` Peter Stephenson
  2015-01-13 16:18   ` Vincent Lefevre
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2015-01-13 10:10 UTC (permalink / raw)
  To: zsh-workers

On Mon, 12 Jan 2015 21:38:52 -0800
Bart Schaefer <schaefer@brasslantern.com> 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.

pws


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Floating point modulus - the "why not"
  2015-01-13 10:10 ` Peter Stephenson
@ 2015-01-13 16:18   ` Vincent Lefevre
  2015-01-13 16:57     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Vincent Lefevre @ 2015-01-13 16:18 UTC (permalink / raw)
  To: zsh-workers

On 2015-01-13 10:10:16 +0000, Peter Stephenson wrote:
> On Mon, 12 Jan 2015 21:38:52 -0800
> Bart Schaefer <schaefer@brasslantern.com> 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@vinc17.net> - 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)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Floating point modulus - the "why not"
  2015-01-13 16:18   ` Vincent Lefevre
@ 2015-01-13 16:57     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2015-01-13 16:57 UTC (permalink / raw)
  To: zsh-workers

On Jan 13,  5:18pm, Vincent Lefevre wrote:
} Subject: 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@brasslantern.com> wrote:
} > > torch% print $(( 9.1 % 1.3 ))    
} > > 1.2999999999999994
} 
} The problem actually comes from the decimal to binary conversions,

Yes, I know; the point is that you can't guarantee a consistent result
from floating-point division, and this is particularly significant for
modulus because the error may be (almost) as large as the divisor; so
it's easy to see why that operation may have been left unimplemented.

(Compare division

torch% print $(( 9.1 / 1.3 ))     
6.9999999999999991

where the error is a tiny fraction of the result.)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-01-13 16:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-13  5:38 Floating point modulus - the "why not" Bart Schaefer
2015-01-13 10:10 ` Peter Stephenson
2015-01-13 16:18   ` Vincent Lefevre
2015-01-13 16:57     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).