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

Re: print and floating point output



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



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