zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk
Subject: Re: print and floating point output
Date: Tue, 11 May 2004 16:07:49 +0100	[thread overview]
Message-ID: <9991.1084288069@csr.com> (raw)
In-Reply-To: "Matthias Kopfermann"'s message of "Tue, 11 May 2004 16:13:59 +0200." <20040511141359.GA32388@finlandia.infodrom.north.de>

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
**********************************************************************


  reply	other threads:[~2004-05-11 15:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
  -- 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9991.1084288069@csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).