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 18:12:18 +0100, Roman Perepelitsa wrote:
> I think what Vincent meant is that zsh should produce the shortest
> string that, when parsed, results in a value equal to the original.
> 
> For your example, "1.1" is the shortest string that parses into
> floating point value equal to the original, hence this (according to
> Vincent) is what zsh should produce.

Yes, this is exactly what I meant, and what Java's System.out.println
seems to do. This is also specified like that in XPath.

I think that's the best compromise in practice.

> Many languages and libraries do this sort of thing. The roundtrip
> guarantee is sometimes limited to the same machine. That is, some
> implementation don't guarantee that you can serialize a floating point
> value on one machine, parse it on another and get the same value.

The roundtrip guarantee is associated with the floating-point format.
If you don't know what format will be used when parsing the string,
then you need to store the exact value (this is always possible for
binary numbers written in decimal, but can take many digits, up to
something around the absolute value of the minimum exponent).

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