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


Groups > comp.lang.python > #197061

Re: Division-Bug in decimal and mpmath

Path csiph.com!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Mark Bourne <nntp.mbourne@spamgourmet.com>
Newsgroups comp.lang.python
Subject Re: Division-Bug in decimal and mpmath
Date Sat, 14 Dec 2024 12:08:29 +0000
Organization A noiseless patient Spider
Lines 76
Message-ID <vjjsfu$5jc$1@dont-email.me> (permalink)
References <vjjcnl$3tdnk$1@dont-email.me>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sat, 14 Dec 2024 13:08:31 +0100 (CET)
Injection-Info dont-email.me; posting-host="e3336571fe81892447c58e062a920ee5"; logging-data="5740"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KwhnPmDCf/bre1Dh9RnE6"
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 SeaMonkey/2.53.19
Cancel-Lock sha1:9TrFQInMsNV74Bi6Ml4DOc9OYdc=
In-Reply-To <vjjcnl$3tdnk$1@dont-email.me>
Xref csiph.com comp.lang.python:197061

Show key headers only | View raw


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.

> Below ist the python-script, with which the computation was done.
> 
> Best regards
> Martin Ruppert
> 
> #!/usr/bin/env python3
> from decimal import Decimal as dec
> from mpmath import *
> from os import popen
> import decimal
> 
> z=.4
> nen=7
> 
> decimal.getcontext().prec=60
> print(dec(z)/dec(nen),end=' ');print(f"decimal: {z}/{nen}")
> 
> mp.dps=60
> a=fdiv(z,nen);print(a,end=' ');print(f"mpmath: {z}/{nen}")
> 
> f=popen(f"dc -e'61k{z} {nen}/f'")
> for i in f:i=i.rstrip()
> f.close()
> print(f"0{i}",end=' ');print(f"dc: {z}/{nen}")
> 
> f=popen(f"calc 'config(\"display\",61);{z}/{nen}'")
> j=0
> for i in f:
>      if j>0:i=i.rstrip();print(i,end=' ');print(f"calc: {z}/{nen}")
>      j+=1
> f.close()
> 
> print(f"{z/nen}",end=' ');print(f"builtin: {z}/{nen}")
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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