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

Re: zsh converts a floating-point number to string with too much precision

On 2019-12-20 16:58:24 +0000, Stephane Chazelas wrote:
> https://unix.stackexchange.com/questions/422122/why-does-0-1-expand-to-0-10000000000000001-in-zsh
> Reproduced below for convenience:
> ════════════════════════════════════════════════════════════════
> zsh chooses a decimal representation for the double binary
> numbers that it uses for evaluating floating point arithmetics
> that preserves their information fully, that is safe for reinput
> into its arithmetic expressions.

But there are several possible decimal representations with this

> And that is done at the expense
> of cosmetic. For that, it needs 17 significant digits, and make
> sure the expansion always includes a . or e so it's treated as
> float on reinput.

This is only one way to fulfill the requirement. Here, it was assumed
that the output precision is chosen independently from the argument
(it seems that zsh chooses printf "%.17g"). But it does not need to
be like that.

BTW, what zsh does is not documented.

Vincent Lefèvre <vincent@xxxxxxxxxx> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

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