zsh-workers
 help / color / mirror / code / Atom feed
* 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

* Re: print and floating point output
  2004-05-11 11:43 print and floating point output Matthias Kopfermann
@ 2004-05-11 11:57 ` DervishD
  2004-05-11 12:01 ` Peter Stephenson
  1 sibling, 0 replies; 7+ messages in thread
From: DervishD @ 2004-05-11 11:57 UTC (permalink / raw)
  To: Matthias Kopfermann; +Cc: zsh-workers

    Hi Matthias :)

 * Matthias Kopfermann <matthi@infodrom.north.de> dixit:
> 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.

    Not in my system, at least :? Could you point to an example?
 
> 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'.

    You're right. 'printf' from coreutils-5.2.1 works ok and prints
the correct number.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: print and floating point output
  2004-05-11 11:43 print and floating point output Matthias Kopfermann
  2004-05-11 11:57 ` DervishD
@ 2004-05-11 12:01 ` Peter Stephenson
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2004-05-11 12:01 UTC (permalink / raw)
  To: zsh-workers

Matthias Kopfermann wrote:
> 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.

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.

Use

  typeset -F 8 var

or

  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:

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

What you're really asking for is zsh to `guess' that the accuracy is one
place, or a few places less.  A little playing around with chains of
multiplications will soon so that this isn't a particularly good
solution.

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.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: print and floating point output
  2004-05-11 14:13   ` Matthias Kopfermann
@ 2004-05-11 15:07     ` Peter Stephenson
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2004-05-11 15:07 UTC (permalink / raw)
  To: zsh-workers

Matthias Kopfermann wrote:
> On Tue, May 11, 2004 at 02:18:39PM +0100, Peter Stephenson:
> > Matthias Kopfermann wrote:
> > > > 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.
> > 
> > Good lesson!  The principle of least surprise is best satisfied by not
> > trusting rounding errors.
> 
> it only _depends_ on what the user is expecting.
> And _I_ just expected the output of zcalc, python, ruby, perl
> and bc when using print with this example. :)

Well, they happen to round it earlier.  That's not a particularly good
basis for expectation.  But the case isn't the same anyway, read on.

I should already have said a bit more about why it's the way it is...

In perl, etc. the operation you're talking about is printing a number,
and that's all.  This is an output operation on an internally
represented floating point number.  The equivalent of that in zsh would
be what I suggested you use,

  float var
  (( var = expr ))
  print $var

In zsh, the operation you're talking about is the result of a
substitution.  The print is irrelevant; it just passes through the
result unchanged in this case.  Substitutions are frequently used
internally for passing on numbers to other values.  For example,

  var=$(( <floating point expression ))

The way zsh is at the moment, we keep the maximum precision until
output.  At output, the expectation is if you want neat formatting, you
ask for it, using typeset on the output variable or printf.  A
substitution is an internal mechanism for generating data, not an final
step for the user.

Actually, it would be better to do

  (( var = <floating point expression> ))

but if var isn't yet set or is a scalar it does the conversion to
floating point at this stage, so you still potentially lose precision
when it truncates.

> And it's only _default_ we are talking about.

As I explain above, that's exactly why it gives the longest precision.
The default (and least surprising) behaviour of a substitution is not to
remove information.  The default behaviour of outputting is got by using
a properly typed variable; $(( ... )) has no associated typing.

> So I am not convinced now but I of course respect this
> decision.

Well, if anyone else feels strongly, they should make their views known.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

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

On Tue, May 11, 2004 at 02:18:39PM +0100, Peter Stephenson:
> Matthias Kopfermann wrote:
> > > 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.
> 
> Good lesson!  The principle of least surprise is best satisfied by not
> trusting rounding errors.

it only _depends_ on what the user is expecting.
And _I_ just expected the output of zcalc, python, ruby, perl
and bc when using print with this example. :)

And it's only _default_ we are talking about.
So I am not convinced now but I of course respect this
decision.
        
        Matthias


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

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

Matthias Kopfermann wrote:
> > 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.

Good lesson!  The principle of least surprise is best satisfied by not
trusting rounding errors.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* 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

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 11:43 print and floating point output Matthias Kopfermann
2004-05-11 11:57 ` DervishD
2004-05-11 12:01 ` Peter Stephenson
2004-05-11 13:08 Matthias Kopfermann
2004-05-11 13:18 ` Peter Stephenson
2004-05-11 14:13   ` Matthias Kopfermann
2004-05-11 15:07     ` 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).