← Back to team overview

yade-users team mailing list archive

Re: [Question #260626]: Precision of variables saved using O.save

 

Question #260626 on Yade changed:
https://answers.launchpad.net/yade/+question/260626

Bruno Chareyre proposed the following answer:
@Jan
The fact that two floats are converted to the same string with "print" does not imply that they are equal. Before printing a number, python is rounding and truncating.

Real number representation is still a mystery to me, I know just enough to know that it is never as simple as one expects.
For instance, this is not a bug:
>>> x=0.1; y=0.2
>>> x+y
0.30000000000000004
>>> print x+y
0.3

This being said, your conclusion may be right (I'm still not 100% sure) even though the proof was flawn, since a variant of your script with "all" digits displayed gives this:
0.2017354310946590789654919717577286064624786376953125
0.2017354310946590789654919717577286064624786376953125
0.2017354310946590789654919717577286064624786376953125
0.2017354310946590789654919717577286064624786376953125
___
import random; r=random.random
from decimal import Decimal #see https://docs.python.org/2/tutorial/floatingpoint.html
O.bodies.append(sphere((r(),r(),r()),1))
base = '/tmp/a.'
for ext in 'yade','xml':
	print Decimal(O.bodies[0].state.pos[0])
	O.save(base+ext)
	O.reset()
	O.load(base+ext)
	print Decimal(O.bodies[0].state.pos[0])

-- 
You received this question notification because you are a member of
yade-users, which is an answer contact for Yade.


Follow ups

References