0

A simple question :

1) In Python using numpy, how to get without parisitic decimals the follwing result (true value 0.6968) ? :

>>> 0.67*(1.04)
0.6968000000000001

If I convert to string, I can find the right value :

>>> str(0.67*(1.04))
'0.6968'

2) By the way, how to control the rounding to systematically get the real value ? I saw that epsilon machine with Python (and/or Numpy) was around 2e-16.

So, I conclude that taking 16 decimals is too small and implies random values after 16th decimal or more precisely below 2e-16.

I don't want to troll but Matlab (I like also Python) doesn't produce this kind of error.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    i didn't get ur second question. explain it more pls. – Yasi Klingler Feb 02 '20 at 21:28
  • 1
    Does this answer your question? [Formatting floats in Python without trailing zeros](https://stackoverflow.com/questions/2440692/formatting-floats-in-python-without-trailing-zeros) – Yasi Klingler Feb 02 '20 at 21:31
  • 1
    What has this to do with "parasitic decimals"? – Hymns For Disco Feb 02 '20 at 21:45
  • @HymnsForDisco . I am talking about the `1` into Python result, i.e `0.6968000000000001` –  Feb 02 '20 at 21:50
  • @kaya This is weird, whay does Matlab enable it and not python ? What's the difference between both from an implementation point of view ? I know we can't compare since sources of Matlab are private, so it's going to be pretty difficult to compare... Intel Pythom seems to have the same bug. –  Feb 02 '20 at 22:36
  • 1
    You're looking at a string representation in decimal, not the actual number which is stored as a double-precision IEEE 754 floating point number. Please see the link I posted. – kaya3 Feb 02 '20 at 22:40
  • @kaya3 . Ok so no way to get directly in Python terminal or in code : `0.67*1.04 = 0.6968` ? However, I like open sources languages but sometimes, proprietary does better... it's frustrating ... –  Feb 02 '20 at 22:51
  • 1
    This is explained in the link which I posted. – kaya3 Feb 02 '20 at 22:53
  • 1
    @youpilat13 I am just confused why you specifically mention "parisitic decimals", from reading https://wikipedia.org/wiki/Parasitic_number it seems they have no relation to your question – Hymns For Disco Feb 02 '20 at 23:43
  • @HymnsForDisco . I wanted just to mean decimals that shouldn't exist in the result given par Python. I take for example the same operation but with Matlab, with which the result is printed right. –  Feb 04 '20 at 06:58
  • 1
    @youpilat13 To hopefully clear some of your confusion: In computing, generally there is no such thing as a pure "number" as we have in mathematics, but rather representations of numbers within limited datatypes. No datatype is capable of representing *all* numbers. In a sense, you could say that the result Python gives is incorrect, because your code is incorrect. Neither `0.67`, `1.04` or `0.6968` can be represented exactly by a float, its as if they're impossible numbers. You could try using the built-in decimal module, then your problem is solved until you need a `1/3`, `1/6`, `1/7`... – Hymns For Disco Feb 04 '20 at 07:39
  • 1
    Matlab also uses floating point types internally and has the same fundamental problems. The only difference is that it may make different formatting choices when printing numeric values so that you don't see the blemishes, but they are still there – Hymns For Disco Feb 04 '20 at 08:35

1 Answers1

0

use %g for that.

x = 0.67*(1.04)
x = '%g'%(x)
print("x:",x)

your question is duplicate of this post.

Yasi Klingler
  • 606
  • 6
  • 13
  • Thanks for your quick answer. Is there an automatic (or implicit) way to apply this converting for all floats in my code, I mean without explicitly having to do a `x = '%g'%(x)` for each of all variables ?? It would be nice. –  Feb 02 '20 at 21:42
  • 1
    @youpilat13 It would be much more appropriate to just use [decimal](https://docs.python.org/3/library/decimal.html) for all your numeric values – Hymns For Disco Feb 04 '20 at 08:04