zsh-workers
 help / color / mirror / code / Atom feed
* Re: print and floating point output
@ 2004-05-11 13:08 Matthias Kopfermann
  2004-05-11 13:18 ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Matthias Kopfermann @ 2004-05-11 13:08 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers


On Tue, May 11, 2004 at 01:01:06PM +0100, Peter Stephenson wrote:

> Well, the alternative is silently censoring decimal places, many of
> which may be valid.  If you want something smart to guess how many
> places are valid, you need to write it.

So you think that ruby, perl and python censor output?
I think that's just useful and uses the principle of least
surprise.

>   typeset -E 8 var
> 
> then
> 
>   (( var = 2.8 * 16.0 ))
>   print $var
> 
> for a given number of decimal places.  Or use printf, which does it's
> own conversion:

I know about printf and typeset already. I just thought that
perl, ruby and python have good points in their decision,
too. :)

And what's the way of using typeset with something like
print $((2.8*16.0)) ?


plus, bc does it that way, too.
and i don't think that bc does censor my output at all.
Well, to a point there always is censoring with floating
point, ack!

> 
> % printf "%f\n" $((2.8*16.0))
> 44.800000

Why is that no censoring then?

> 
> If you are doing serious floating point work, unfortunately you need to
> understand something about rounding errors, which are a tricky and
> ever-present feature.

I learned that here.

        Matthias


^ permalink raw reply	[flat|nested] 7+ messages in thread
* print and floating point output
@ 2004-05-11 11:43 Matthias Kopfermann
  2004-05-11 11:57 ` DervishD
  2004-05-11 12:01 ` Peter Stephenson
  0 siblings, 2 replies; 7+ messages in thread
From: Matthias Kopfermann @ 2004-05-11 11:43 UTC (permalink / raw)
  To: zsh-workers

Hi zshworkers again,

something that really confused me even if i understand that
floating point is a problem for a computer when converting it to binary
is the result of e.g. 
print $((2.8*16.0))
which _sadly_, i think, returns:
44.799999999999997

I wonder if it really was a good decision to print so many numbers
after the point.

When asking Sven W9y he told me that that's a problem of
gcc's printf/fprintf which indeed gives that result when using it with
more than 15 numbers after the point.

on the other hand perl, ruby and python all return
the right result:
perl -le 'print 2.8*16.0' => 44.8
python -c 'print 2.8*16.0' => 44.8
ruby -e 'puts 2.8*16.0' => 44.8

so i guess it would make a lot sense and not cause confusion if zsh
would have a more sensible output with `print' and for users
not using `printf'.


        
                Matthias


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

end of thread, other threads:[~2004-05-11 15:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-11 13:08 print and floating point output Matthias Kopfermann
2004-05-11 13:18 ` Peter Stephenson
2004-05-11 14:13   ` Matthias Kopfermann
2004-05-11 15:07     ` Peter Stephenson
  -- strict thread matches above, loose matches on Subject: below --
2004-05-11 11:43 Matthias Kopfermann
2004-05-11 11:57 ` DervishD
2004-05-11 12:01 ` Peter Stephenson

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).