zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: zsh converts a floating-point number to string with too much precision
Date: Fri, 20 Dec 2019 04:38:16 +0100	[thread overview]
Message-ID: <CAHYJk3Sz6WWDwe92_sQM9By-Eus-=CqdvAfYcYhPiPAgo2batQ@mail.gmail.com> (raw)
In-Reply-To: <20191220013711.GA708801@zira.vinc17.org>

On 12/20/19, Vincent Lefevre <vincent@vinc17.net> wrote:
> With zsh 5.7.1, I get:
>
> zira% echo $((1.1))
> 1.1000000000000001
>
> because zsh seems to first select the precision independently
> from the value, i.e. 17 to be able to convert the string back
> to floating point, preserving the original value, then it
> outputs the closest number in this precision.
>
> Instead, zsh should select the minimum precision so that the
> inverse conversion can give the original value, i.e. it should
> output 1.1 here.

You can use typeset -F1 one=1.1 to specify the output precision of a
parameter (note that this doesn't affect the float value stored, you
can change to -F20 later to display more decimals without
reassignment). So in your case you could count the number of digits in
the string after the . and then pass that to -F if you wanted to.

-- 
Mikael Magnusson

  reply	other threads:[~2019-12-20  3:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-20  1:37 Vincent Lefevre
2019-12-20  3:38 ` Mikael Magnusson [this message]
2019-12-20 16:58 ` Stephane Chazelas
2019-12-20 17:12   ` Roman Perepelitsa
2019-12-21  0:50     ` Vincent Lefevre
2019-12-21  8:47       ` Stephane Chazelas
2019-12-21  9:43         ` Roman Perepelitsa
2019-12-21 17:56           ` Stephane Chazelas
2019-12-21 18:11             ` Stephane Chazelas
2019-12-21 18:20               ` Roman Perepelitsa
2019-12-21 21:28         ` Vincent Lefevre
2019-12-21  1:00   ` 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='CAHYJk3Sz6WWDwe92_sQM9By-Eus-=CqdvAfYcYhPiPAgo2batQ@mail.gmail.com' \
    --to=mikachu@gmail.com \
    --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).