Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Accent on helpful side of your nature. Drain the moat.


comp / comp.lang.python / Re: Division-Bug in decimal and mpmath

SubjectAuthor
* Division-Bug in decimal and mpmathMartin Ruppert
`* Re: Division-Bug in decimal and mpmathMark Bourne
 +* Re: Division-Bug in decimal and mpmath2QdxY4RzWzUUiLuE
 |`- Re: Division-Bug in decimal and mpmathMark Bourne
 `- Re: Division-Bug in decimal and mpmathOscar Benjamin

1
Subject: Division-Bug in decimal and mpmath
From: Martin Ruppert
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sat, 14 Dec 2024 07:39 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ruppert@hs-worms.de (Martin Ruppert)
Newsgroups: comp.lang.python
Subject: Division-Bug in decimal and mpmath
Date: Sat, 14 Dec 2024 07:39:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <vjjcnl$3tdnk$1@dont-email.me>
Reply-To: ruppert@hs-worms.de
Injection-Date: Sat, 14 Dec 2024 08:39:35 +0100 (CET)
Injection-Info: dont-email.me; posting-host="1e9732918c36f47788273ed53a25e352";
logging-data="4110068"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194S6L8xhsQ7s6uglAfc7GL"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/4.19.0-6-amd64 (x86_64))
Cancel-Lock: sha1:MjkRp6ohI726n5VTfJjcL1Ocz9o=
View all headers

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.

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}")

--
ruppert@hs-worms.de

Subject: Re: Division-Bug in decimal and mpmath
From: Mark Bourne
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sat, 14 Dec 2024 12:08 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nntp.mbourne@spamgourmet.com (Mark Bourne)
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>
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>
View all headers

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}")
>

Subject: Re: Division-Bug in decimal and mpmath
From: 2QdxY4RzWzUUiLuE@potatochowder.com
Newsgroups: comp.lang.python
Date: Sat, 14 Dec 2024 19:21 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!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>
View all headers

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? ;-)

Subject: Re: Division-Bug in decimal and mpmath
From: Mark Bourne
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sun, 15 Dec 2024 11:59 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nntp.mbourne@spamgourmet.com (Mark Bourne)
Newsgroups: comp.lang.python
Subject: Re: Division-Bug in decimal and mpmath
Date: Sun, 15 Dec 2024 11:59:40 +0000
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <vjmgbe$if0a$1@dont-email.me>
References: <vjjcnl$3tdnk$1@dont-email.me> <vjjsfu$5jc$1@dont-email.me>
<Z13aolhGcrrxU1vT@spot.fqdn.local>
<mailman.5.1734205250.2912.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 15 Dec 2024 12:59:42 +0100 (CET)
Injection-Info: dont-email.me; posting-host="72cc57422a5a2f404efbb51a344a2c1a";
logging-data="605194"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FrfCqFtdaa6OY6QTq2gl9"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
SeaMonkey/2.53.19
Cancel-Lock: sha1:qwP0R7bHnhzuBfpPg3No7e2IUoI=
In-Reply-To: <mailman.5.1734205250.2912.python-list@python.org>
View all headers

2QdxY4RzWzUUiLuE@potatochowder.com wrote:
> 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.

Oh yes. Thinking the question was about 4 different Python packages,
I'd just looked up the packages with those names on PyPI and hadn't
noticed they were separate commands being called via popen! In that
case, the string formatting of a float in the commands defaults to 6
decimal places, and `z` is rounded back to 0.4 rather than the exact
value represented by the float. From there, as you say, `dc` and `calc`
probably handle that string similarly to `decimal.Decimal` being passed
the string "0.4".

> Why couldn't we have evolved with eight fingers on each hand? ;-)

Yeah, it would have made conversions to and from binary somewhat more
intuitive...

--
Mark.

Subject: Re: Division-Bug in decimal and mpmath
From: Oscar Benjamin
Newsgroups: comp.lang.python
Date: Sun, 15 Dec 2024 21:29 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: oscar.j.benjamin@gmail.com (Oscar Benjamin)
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>
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>
View all headers

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

1

rocksolid light 0.9.8
clearnet tor