Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197069

Re: Division-Bug in decimal and mpmath

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: Division-Bug in decimal and mpmath
Date Sun, 15 Dec 2024 21:29:18 +0000
Lines 46
Message-ID <mailman.7.1734298172.2912.python-list@python.org> (permalink)
References <vjjcnl$3tdnk$1@dont-email.me> <vjjsfu$5jc$1@dont-email.me> <CAHVvXxTyu0BWGjnYHR6eSdvTj+1EQACT67SW2hb-q0Q_MvU-mg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
X-Trace news.uni-berlin.de oXRr7ltFEl+CFRa5gRFigAO3FjY4kMtR6tliWtx/By9A==
Cancel-Lock sha1:BFV8iASQef4p3iEwInQfOBWxJco= sha256:LlwSW2vDksNanqC8Q0kyk2Lv5qLQEA0JWgy5eAvhshc=
Return-Path <oscar.j.benjamin@gmail.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=gmail.com header.i=@gmail.com header.b=RSf1loFL; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'looks': 0.02; 'calculating': 0.09; 'comparison': 0.09; 'expression': 0.09; 'skip:` 20': 0.09; 'string,': 0.09; 'subject:Bug': 0.09; 'import': 0.15; 'calculations': 0.16; 'decimal': 0.16; 'division': 0.16; 'evaluating': 0.16; 'received:mail-ot1-x331.google.com': 0.16; 'wrote:': 0.16; 'instead': 0.17; 'uses': 0.19; 'to:addr:python- list': 0.20; 'sat,': 0.22; '>>>': 0.28; 'wrong': 0.28; 'dec': 0.31; 'message-id:@mail.gmail.com': 0.31; 'python-list': 0.32; 'but': 0.32; 'mark': 0.32; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34; 'from:addr:gmail.com': 0.34; 'running': 0.35; 'final': 0.35; 'errors': 0.36; 'means': 0.38; 'use': 0.39; 'least': 0.39; 'appears': 0.40; 'exact': 0.40; 'martin': 0.40; 'should': 0.40; 'gives': 0.62; 'pass': 0.64; 'six': 0.65; 'below': 0.69; '....': 0.76; 'completeness': 0.84; 'fraction': 0.84; 'oscar': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734298169; x=1734902969; darn=python.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=MWMKV+gwoFWTBJX6RB0Z0kw9AGG8OQrKYYCRK7bJo9w=; b=RSf1loFLWA1no2+5kovQR7UonO6WICGcnyq6x0oJve1FRLhr4gESzwTFzM/Cs10qtG G/Ysjr9kOdYdMvvzyFfmZ+0Ep7XSuHYHYwy5EjnhCCYnjjxx8we+uoDS1vPR2lJdIYE+ d3GNgAwLkPHeSP7F9H1Ctvhu0bCWuh2GF/QmW/oY4S9GyLWgIPSrVSPvb4050GSSFubG ehUnnPz+CHyc17sq7Pl7fRdz4g+Vn8LoD4gH1t5nUAcU2KjEa49lE7+9rDxUayimtpqc 4dwzvGxa2IvSBhpe7TRGTEyWHZTFN1VwYT2E5IM2jPdpCX2wLGP9pGiRl6W6N2hsoVR9 iO0A==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734298169; x=1734902969; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MWMKV+gwoFWTBJX6RB0Z0kw9AGG8OQrKYYCRK7bJo9w=; b=RR0KZQ2aFbQOZNUahmgC8Qn9+vJXvuZqKCoP3tmVSRh2YncGtZ8/VRt0dijosl2UeE nRHnBWyAX/WifwlHjJTAelDWtCVjTka6Jmds6/xodvQKRcvo/qhIVKGFxkWY+a6iKpVt 8Wa7TMkmtRRQf2Y9mm1ZFP/0SJSlkjD3JaZtkMdEUaG3DWnEG5/Y/Byw4RwKMs8CU86h m2oJHR6gSs0xIOqsLaGe6sQWnxecdhX9aUtngbDdfWjxly8q7ryNFHt26Czv8idT+clF +qQnsdkCe3AHAGyOCKaf0mivMxzkAUTiQCZsdCcp8Hzc3ujA3cpofEDzglw2yAiKVdP4 fltQ==
X-Gm-Message-State AOJu0YyR02DrALuZUQ5IETRS57GBXNj7BQE8CTdsrBdiIceK/Sg2jgos /AZpAXO7X7Mg5b0otkcdEf3h2HJ7i2UzSKhE90v/zqXp2izaYF+YGeGjF1Z6Y8APCOqWD172OFH Ccelt0e7zjhMvy5h6iIdyGFTGpxwI1e8p
X-Gm-Gg ASbGncuk92aR9SctexMx/JvwmzrDPwC3s2nugpKDc2roHnVGW+sPrOxgKTrnTMTpyQ9 MOSlNtOjBbv3UOAGE2KhnGsfrvjKkc2sbKeod2IQuRpBIAsHHkdPym+njWaDPKdNVE6Tw7O1M
X-Google-Smtp-Source AGHT+IEGt0NG2TSmoqJT+shRfLJSMIsvTbJOxYJjEEWGOCt6ZqQxN4MIV1iMF1/U7AwD0cwi3NgHayHghBkjuMa5a30=
X-Received by 2002:a05:6870:1f15:b0:29e:80d8:31a9 with SMTP id 586e51a60fabf-2a3ac491170mr5590928fac.2.1734298169053; Sun, 15 Dec 2024 13:29:29 -0800 (PST)
In-Reply-To <vjjsfu$5jc$1@dont-email.me>
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 <CAHVvXxTyu0BWGjnYHR6eSdvTj+1EQACT67SW2hb-q0Q_MvU-mg@mail.gmail.com>
X-Mailman-Original-References <vjjcnl$3tdnk$1@dont-email.me> <vjjsfu$5jc$1@dont-email.me>
Xref csiph.com comp.lang.python:197069

Show key headers only | View raw


On Sat, 14 Dec 2024 at 19:02, 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.
...
>
> 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.

For completeness this is how to do it with mpmath:

 >>> from mpmath import mp
 >>> mp.dps = 60
 >>> mp.mpf('0.4') / 7
 mpf('0.0571428571428571428571428571428571428571428571428571428571428549')

You can also use SymPy:

 >>> from sympy import Rational
 >>> a = Rational('0.4') / 7
 >>> a
 2/35
 >>> a.evalf(60)
 0.0571428571428571428571428571428571428571428571428571428571429

SymPy uses mpmath for evalf but it allows doing exact calculations
first and then evaluating the final exact expression to however many
digits are desired at the end which means that you don't need to
accumulate rounding errors before calling evalf.

--
Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

Division-Bug in decimal and mpmath Martin Ruppert <ruppert@hs-worms.de> - 2024-12-14 07:39 +0000
  Re: Division-Bug in decimal and mpmath Mark Bourne <nntp.mbourne@spamgourmet.com> - 2024-12-14 12:08 +0000
    Re: Division-Bug in decimal and mpmath 2QdxY4RzWzUUiLuE@potatochowder.com - 2024-12-14 14:21 -0500
      Re: Division-Bug in decimal and mpmath Mark Bourne <nntp.mbourne@spamgourmet.com> - 2024-12-15 11:59 +0000
    Re: Division-Bug in decimal and mpmath Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2024-12-15 21:29 +0000

csiph-web