Deutsch English Français Italiano |
<mailman.5.1734205250.2912.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: 2QdxY4RzWzUUiLuE@potatochowder.com Newsgroups: comp.lang.python Subject: Re: Division-Bug in decimal and mpmath Date: Sat, 14 Dec 2024 14:21:06 -0500 Lines: 49 Message-ID: <mailman.5.1734205250.2912.python-list@python.org> References: <vjjcnl$3tdnk$1@dont-email.me> <vjjsfu$5jc$1@dont-email.me> <Z13aolhGcrrxU1vT@spot.fqdn.local> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de 8o81io49+xwZqE36b6a2jAIBhYsiUxljZS50R6Xhdm6g== Cancel-Lock: sha1:qfCtwGwB9NO8LwLtzpYLJPSnL+Q= sha256:HDM8ntds1uq07U9MKAGEJAsvDiFC/HP+6gEvjOAG3Tg= Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=potatochowder.com header.i=@potatochowder.com header.b=PLzbH5V7; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'pypi': 0.05; ';-)': 0.07; 'string': 0.07; 'calculating': 0.09; 'calculator': 0.09; 'comparison': 0.09; 'float': 0.09; 'identical': 0.09; 'received:78': 0.09; 'skip:` 20': 0.09; 'string,': 0.09; 'subject:Bug': 0.09; 'utility': 0.09; 'import': 0.15; 'conversion': 0.16; 'decimal': 0.16; 'division': 0.16; 'emacs,': 0.16; 'float,': 0.16; 'from:addr:2qdxy4rzwzuuilue': 0.16; 'from:addr:potatochowder.com': 0.16; 'precision.': 0.16; 'received:136.243': 0.16; 'received:www458.your-server.de': 0.16; 'received:your-server.de': 0.16; 'right.': 0.16; 'wrote:': 0.16; 'instead': 0.17; 'probably': 0.17; "can't": 0.17; 'to:addr:python- list': 0.20; 'received:de': 0.23; 'anything': 0.25; 'wrong': 0.28; 'think': 0.29; 'dec': 0.31; "doesn't": 0.32; 'guess': 0.32; 'python-list': 0.32; 'received:136': 0.32; 'but': 0.32; 'mark': 0.32; "i'm": 0.33; 'header:In-Reply-To:1': 0.34; 'running': 0.35; 'close': 0.35; 'using': 0.37; 'both': 0.38; 'use': 0.39; "that's": 0.39; 'least': 0.39; 'appears': 0.40; "couldn't": 0.40; 'exact': 0.40; 'martin': 0.40; 'should': 0.40; 'gives': 0.62; 'limited': 0.62; 'internal': 0.63; 'pass': 0.64; 'your': 0.64; 'six': 0.65; 'well': 0.65; 'exactly': 0.68; 'result,': 0.69; 'below': 0.69; 'eight': 0.76; '0.4': 0.84; 'fraction': 0.84; 'represented': 0.84 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=potatochowder.com; s=default2305; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:To:From:Date:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=mEqxTQwxfwa8JL/xhGs+abZ7nWsaRyddcR634HG2f8o=; b=PLzbH5V7aaIGVpyvZRNaO1Csu4 Ekql7cFOPOe4Xt/3f2F5AN2Ehqrk0dUdQYoIHFDsOFNFFBqKHNb1yzrhTlopiJWKDO4CTQ7899vgM vdjlNxvQVLm6MFdTWMPnjt3zOMoYzyZW/KUwp7wdwPnekUWwtTlCruqbaoep4lgt2IXFto3/wfnLw 2dBLrDyc5weRIBKd1gepd8kzkti/tx4g/xt6DCCIGgnRoAmqzDqttosGIoyf0tbilgHxh5x1+LTdr lqCbzBxgjsiSFx7F/8lhQDdfK5TjDPYzZ5tdWAIsFXhJ19AAm+q5V16CkUR639Y438ttjab7gNnpA aff+Tb4w==; Mail-Followup-To: python-list@python.org Content-Disposition: inline In-Reply-To: <vjjsfu$5jc$1@dont-email.me> X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com X-Virus-Scanned: Clear (ClamAV 1.0.7/27487/Sat Dec 14 10:38:46 2024) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language <python-list.python.org> List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> List-Archive: <https://mail.python.org/pipermail/python-list/> List-Post: <mailto:python-list@python.org> List-Help: <mailto:python-list-request@python.org?subject=help> List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> X-Mailman-Original-Message-ID: <Z13aolhGcrrxU1vT@spot.fqdn.local> X-Mailman-Original-References: <vjjcnl$3tdnk$1@dont-email.me> <vjjsfu$5jc$1@dont-email.me> Bytes: 6462 On 2024-12-14 at 12:08:29 +0000, Mark Bourne via Python-list <python-list@python.org> wrote: > Martin Ruppert wrote: > > Hi, > > > > the division 0.4/7 provides a wrong result. It should give a periodic > > decimal fraction with at most six digits, but it doesn't. > > > > Below is the comparison of the result of decimal, mpmath, dc and calc. > > > > 0.0571428571428571460292086417861615440675190516880580357142857 decimal: 0.4/7 > > 0.0571428571428571460292086417861615440675190516880580357142857 mpmath: 0.4/7 > > 0.0571428571428571428571428571428571428571428571428571428571428 dc: 0.4/7 > > 0.0571428571428571428571428571428571428571428571428571428571429 calc: 0.4/7 > > 0.05714285714285715 builtin: 0.4/7 > > > > Both decimal and mpmath give an identical result, which is not a > > periodic decimal fraction with at most six digits. > > > > calc and dc provide as well an identical result, which *is* a periodic > > decimal fraction with six digits, so I think that's right. > > I looks like you might be running into limitations in floating-point > numbers. At least with decimal, calculating 4/70 instead of 0.4/7 appears > to give the correct result. As does: > ``` > from decimal import Decimal as dec > z2 = dec(4) / dec(10) > print(z2 / dec(nen)) > ``` > You can also pass a string, and `dec("0.4")/dec(10)` gives the correct > result as well. > > Your `z` is a float, and therefore limited by the precision of a float. It > doesn't represent exactly 0.4, since that can't be exactly represented by a > float. Anything you do from then on is limited to that precision. > > I can't easily find documentation for dc and calc (links from PyPI are > either broken or don't exist), but I'm guessing they use some heuristics to > determine that the float passed in very close to 0.4 so that was probably > intended, rather than using the exact value represented by that float. I'm going to guess that since dc is a shell utility and calc is either another shell utility or the calculator in emacs, and that they both do their own conversion from a string to an internal representation without going through an IEEE float. Why couldn't we have evolved with eight fingers on each hand? ;-)