1

I need to display a number, let's say 0.9 exact at least until 17 decimal places. Even if I use Decimal or mpf, it gives me the result as if I would not be using them at all.

from mpmath import mpf
from decimal import Decimal
from decimal import getcontext
import mpmath

getcontext().prec = 25
mpmath.mp.prec=100

a=mpf("0.9")
b=mpf(0.9)
c=Decimal("0.9")
d=Decimal(0.9)
e=0.9


print("%.17f" % a)
print("%.17f" % b)
print("%.17f" % c)
print("%.17f" % d)
print("%.17f" % e)

The output is

>>0.90000000000000002
>>0.90000000000000002
>>0.90000000000000002
>>0.90000000000000002
>>0.90000000000000002

I have searched everywhere, but I can not seem to find aa solution. Is it even possible to get that precision? What I understood from mpf, they claim a much higher precision than I am getting. What am I missing?

Edited:

I am sorry for a potential misunderstanding, but what I really had in mind, is that I need to perform actual calculations with this number, not only to display it properly.

  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – It_is_Chris Feb 15 '22 at 14:57
  • 1
    @It_is_Chris: No, that does not answer the question. This question is about why operations intended to be decimal do not work on decimal values, i.e., why there is a binary-induced rounding error where none is expected. Please do not promiscuously mark floating-point questions as duplicates of that question. Just because a question involves floating-point does not mean it is the same as that question, and closing questions indiscriminately interferes with providing good answers and useful knowledge. – Eric Postpischil Feb 16 '22 at 12:43
  • @EricPostpischil I did not close the question; simply voted to. – It_is_Chris Feb 16 '22 at 13:43
  • @It_is_Chris: Please do not promiscuously mark floating-point questions as duplicates of that question. Just because a question involves floating-point does not mean it is the same as that question, and voting to close questions indiscriminately interferes with providing good answers and useful knowledge. – Eric Postpischil Feb 16 '22 at 13:54
  • @EricPostpischil Ha – It_is_Chris Feb 16 '22 at 14:13
  • Also, I have edited the question, I do not really need to display the value, but I need to make precise calculations with it. – Andris Erglis Feb 16 '22 at 15:37

1 Answers1

2

I suspect that the % operator converts its argument to a float when using a %f format. Using f-strings instead (or the .format method on strings) might be a better idea, since they give the object a better chance of controlling the way it gets formatted.

I just tried this:

Python 3.9.0 (v3.9.0:9cf6752276, Oct  5 2020, 11:29:23) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
>>> d=decimal.Decimal('0.1')
>>> '%.20f' % (d,)
'0.10000000000000000555'
>>> f'{d:.20f}'
'0.10000000000000000000'
Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15
  • It is very weird - when I try this in terminal, I get the same answer as you, but when I deploy the script, nothing changes. It still has that weird value with 5's in the end. – Andris Erglis Feb 16 '22 at 15:37