zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: zsh-workers@zsh.org
Subject: Re: floating-point precision and zsh vs ksh93 and coreutils
Date: Sun, 27 Mar 2022 09:26:52 +0100	[thread overview]
Message-ID: <20220327082652.fg6uef5munymssm6@chazelas.org> (raw)
In-Reply-To: <20220323163646.GB1190496@cventin.lip.ens-lyon.fr>

2022-03-23 17:36:46 +0100, Vincent Lefevre:
[...]
> Shouldn't zsh switch to long double?
[...]

I'm not convinced it's a good idea. I had made a long write-up
related to that some time ago at
https://unix.stackexchange.com/questions/422122/why-does-0-1-expand-to-0-10000000000000001-in-zsh
that we did discussed it here about the artefacts whereby echo
$((0.1)) outputs 0.10000000000000001 for instance.

Several problems:
- double is still the most commonly used float representation
  used by default in other languages (perl, python, most if not
  all awks...)
- long double precision varies with the system/compiler/CPU
  (64bit, 80bit, 128bit), whilst double is more consistently
  64bit on Unix systems at least.
- so to preserve the precision upon round-trip to/from decimal,
  we'd need 17, 21 or 36 digit precision making for even uglier
  artefacts (and very long numbers), and if we give up on
  preserving precision, we get the same problems as affect
  yash/ksh93 described at that link above.

  $ ksh -c 'if (( $((1. / 3)) == 1./3 )); then echo yes; else echo no; fi'
  no
  $ ksh -c 'echo $(( $((1. / 3)) - 1./3 ))'
  -3.52365706057788941e-19

  See the discussion around workers/45106 (to which you
  contributed) about how to reduce the display artefacts, but
  that's potentially costly considering that shells do have to
  always go back and forth between binary and decimal
  representation of numbers, as decimal is the "interchange"
  format to pass to the user or to commands and that's the
  decimal text representation that is stored in variable (IIRC
  ksh93 stores both the decimal/text and binary representation
  though for variables declared with typeset -F/E to reduce the
  need to translate all the time).

To me, if double is good enough for perl or python, it should we
all the more for a shell.

-- 
Stephane


  reply	other threads:[~2022-03-27  8:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23 16:36 Vincent Lefevre
2022-03-27  8:26 ` Stephane Chazelas [this message]
2022-03-27 22:26   ` Bart Schaefer
2022-04-07 15:06     ` Vincent Lefevre
2022-04-07 18:00       ` Bart Schaefer
2022-04-07 15:03   ` Vincent Lefevre

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=20220327082652.fg6uef5munymssm6@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=zsh-workers@zsh.org \
    /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).