From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id d763633a for ; Fri, 20 Dec 2019 17:13:10 +0000 (UTC) Received: (qmail 10277 invoked by alias); 20 Dec 2019 17:13:04 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 45097 Received: (qmail 24139 invoked by uid 1010); 20 Dec 2019 17:13:03 -0000 X-Qmail-Scanner-Diagnostics: from mail-il1-f182.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.1/25663. spamassassin: 3.4.2. Clear:RC:0(209.85.166.182):SA:0(-2.0/5.0):. Processed in 1.569382 secs); 20 Dec 2019 17:13:03 -0000 X-Envelope-From: roman.perepelitsa@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.166.182 as permitted sender) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=P4dJXW1cKpvrc0KzRl7XrfXPehJusgffw5xX8nK999k=; b=oc7gxwjWdbNs9pxf3zq6FmeC4dUu8uP9WwyxjedcOUfYWEVTV9XDipS7bCc5BZ+7MQ 8hySyA+sfzrgb0++8pQJ0oFGPHXv/kv0sF4dlRbgV+1cM68tKcPncy+JDTGNl9y7P/Ny W/M2H3Ye48L7LNCMHBZ0ooDE9pwiplTIma1tH2K2tzQliIUxUBCQZ0X0l6or0FWti0eJ XkyEMDYWpAUQNVGMVata8AcD+QEkaEEipIM2wb1RKOhLvaI0Sa/cUGv0iXKiyccUY6sW 7aVSTJROX5luXDvPi83/aeCzkNCh/AMNNPYfqK++EzVZvgDt8LBbpMGJ1loNDFPIIiNj Qz4A== X-Gm-Message-State: APjAAAWmXZ+LA32vCuXa+Hf8Pt1gC3/Ld7IWu115Sjhh94GHI2E6PovR wCCX9yz1NnhHc5QhfvQ4On4sv/4hSJk03mCSKqTKL1Mp X-Google-Smtp-Source: APXvYqzzetVYsDpy+KWsOyAbnBpPrCLgc9RrF4ZENUbB0ikAHG7c99sRI2cY+HPIFq1cKFKu6fi8Yl9Xa+SXJK6iHlU= X-Received: by 2002:a92:d642:: with SMTP id x2mr13234064ilp.169.1576861949186; Fri, 20 Dec 2019 09:12:29 -0800 (PST) MIME-Version: 1.0 References: <20191220013711.GA708801@zira.vinc17.org> <20191220165824.ufvjtx37xt7dp2dt@chaz.gmail.com> In-Reply-To: <20191220165824.ufvjtx37xt7dp2dt@chaz.gmail.com> From: Roman Perepelitsa Date: Fri, 20 Dec 2019 18:12:18 +0100 Message-ID: Subject: Re: zsh converts a floating-point number to string with too much precision To: Zsh hackers list Content-Type: text/plain; charset="UTF-8" On Fri, Dec 20, 2019 at 5:59 PM Stephane Chazelas wrote: > > 2019-12-20 02:37:11 +0100, Vincent Lefevre: > > 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. > > And what should it give for > > $((1.1000000000000001)) ? > > (hint, 1.1000000000000001 and 1.1 have the same "double" > representation). 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. 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. Roman.