Float** internal representation (Numeric module)

  • Thread starter Alexander Sourjikov
  • Start date
A

Alexander Sourjikov

Hi pals,
i am using python for numerical simulations and noticed that
when i assign a Float variable say 0.1 python sees it like
0.10000000149011612.
I have also checked if python really takes these digits into account and
it turned out to be truth, because variable * 2 gives
0.20000000298023224
Does anyone have an idea where these weird digits come from and
how to avoid this strange effect ?

Would be very grateful for any help
 
A

Andrew Dalke

Alexander Sourjikov:
i am using python for numerical simulations and noticed that
when i assign a Float variable say 0.1 python sees it like
0.10000000149011612.

http://www.python.org/doc/faq/general.html#why-are-floating-point-calculatio
ns-so-inaccurate

It's a fact of life using computers. If you're doing simulations,
you should learn more about numerical analysis, and make
sure you're using algorithms which are robust in the face of
expected error. (Eg, molecular dynamics is chaotic but the
integrators are stable (I think the proper term is "symplectic")
and the results, while not exactly correct, still provide insight
into the problems.


Andrew
(e-mail address removed)
 
A

Andrei

Alexander said:
Hi pals,
i am using python for numerical simulations and noticed that
when i assign a Float variable say 0.1 python sees it like
0.10000000149011612.
I have also checked if python really takes these digits into account and
it turned out to be truth, because variable * 2 gives
0.20000000298023224
Does anyone have an idea where these weird digits come from and
how to avoid this strange effect ?

Common problem: floats aren't 100% accurate. See question 4.98 of the
FAQ or http://www.python.org/doc/current/tut/node14.html.

--
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V
ernq gur yvfg, fb gurer'f ab arrq gb PP.
 
A

Alexander Sourjikov

Thanks for the usefull link, but in that example
0.1 is represented as 0.10000000000000001.
So the precision is 16 digits, and it is good enough for almost anything.
however, in my case 0.1 equals 0.10000000149011612
and the precision is only 8 digits.
the range of loop in my program reaches 10**8 and for iterative algorithm
where next step is based on the data from the previous one the error is
accomulated and for this number of iterations can become significant.
How can i raise the level of precision to that mentioned in the example ?

Thanks in advance.
 
J

Jeff Epler

a Python float() is a C "double", while I'm sure Numeric uses "float"
and "double" to match the C compiler. The smallest C float different
from 1.0 is 1.0+(.5**23), and the smallest C double different from 1.0
is 1.0+(.5**52). The smallest python float() different from 1.0 is also
1.0+(.5**52).

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top