From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 10079 invoked from network); 27 Mar 2022 08:27:32 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 27 Mar 2022 08:27:32 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1648369652; b=aXfvvW5kR1DWBNFuHF/CmYPLCWgQKWWQlcnpwNfN6RH11rTUNd7CHom4gxae9iPPKSHKAznCZI +XoTbmBuGEHEim1p+LwWYtZyNqOWLSt7inbT4G+hYMX6Ho83cU/TaK5ydHKwEEIENAKShZhpdk qbiuVVFpgLDmfP8jTMPCDP76HCRcGnHdmRRL4jGlulCJKMHocthLSL0O/2vrqHXb7mTyoCQmpZ U9wjmzn4QDh65aipVf+RaS9e99wqVlaYY9dSDTJBtcSCPFILIqeuVfqdyPxUKFjXvGq3MuZayL a6SZlyS9AnhNsVCMNMnsQsaN+SWlz8QjdRDQ7Bh4H9JoXQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay8-d.mail.gandi.net) smtp.remote-ip=217.70.183.201; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1648369652; bh=amCs7E6XEfz69HILsFFMJn2cqYKvG5yYavZ9N7xKX5k=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:To:From:Date:DKIM-Signature; b=QiyYX0uTCGcwgYyd7ONoADRF3d4pg9lw6jqAu79UnkbEsiEf1tnm/eWMidinlFUD0P5ThlGF30 Mfc2STeoCMNRwd1e+FRdfnETXGB6DZmnH7zpxx7AM5yr9LOYy9xOUtkFXrMRamvRCp08w9EDnd 7Z4ZTDEl8jNCMWMobXZuWSy1zu8Sq4qbjbQMXdB+79s+UWGd64KXsjRREIU6N8aN/yyklLAPlv qABdkrdnxgDAoLnJKqH0q8G8mSDJxc55d4SGpjGpYYvHrmwAgRhdgrA/coJ/DqHn4EZDF7liJ6 lXY+eTi1UraHHy+EXPoJG4fjVAvmSDTWzMU/+OUXaghC7A==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:To:From:Date:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=ndMxkwf7GTzEUfdQpVWbBx5ZSuUpoxeKpem91MXI9uw=; b=UM/k1500d1p1enBRxUHlwLtX30 tQo/UYCuSYMG+a89huFyLnjvns1kas+p4YtxMaj3CBn6hwXvTZohw4CY7pR/RfLr3WAK73zAu1AsJ Ytlkf+e3lQwvfyqomjotRxjuth4VclrGVB0AB8SsluNmEPEfOKZMfAc465cYbBTquDrX5NszGoR8b 9A1GR1WjmvCuaNKtBMgls+hOnBIZD7uU6WsQyVnYxy0II3rgaxxaFl5W5wvluMy2UJZQjbNwgGtXL A2QfP9ilzkgvxUPcVry8tdoEAaYVCVk4YGSY/wSPilP6Oq7n7wC/cpmYBQ0N9ufMvIVPl5viYXwZN /RmaA4jg==; Received: from authenticated user by zero.zsh.org with local id 1nYOFH-000E5u-DV; Sun, 27 Mar 2022 08:27:31 +0000 Authentication-Results: zsh.org; iprev=pass (relay8-d.mail.gandi.net) smtp.remote-ip=217.70.183.201; dmarc=none header.from=chazelas.org; arc=none Received: from relay8-d.mail.gandi.net ([217.70.183.201]:33337) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1nYOEg-000DnB-Cx; Sun, 27 Mar 2022 08:26:55 +0000 Received: (Authenticated sender: stephane@chazelas.org) by mail.gandi.net (Postfix) with ESMTPSA id 7A5511BF206 for ; Sun, 27 Mar 2022 08:26:53 +0000 (UTC) Date: Sun, 27 Mar 2022 09:26:52 +0100 From: Stephane Chazelas To: zsh-workers@zsh.org Subject: Re: floating-point precision and zsh vs ksh93 and coreutils Message-ID: <20220327082652.fg6uef5munymssm6@chazelas.org> Mail-Followup-To: zsh-workers@zsh.org References: <20220323163646.GB1190496@cventin.lip.ens-lyon.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220323163646.GB1190496@cventin.lip.ens-lyon.fr> X-Seq: 49896 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: 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