← Back to team overview

yade-users team mailing list archive

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

 

>
>
> @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.
>

sorry, you are right, [1] shows that decimal representation is indeed
rounded..


> 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
>

see Vaclav's thesis, section 2.7.2 or [1]


>
> 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
>

see [1] (again :-)


> ___
> 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])
>
>
Another (and it seems to me that really proving) alternative is to use
Python float.hex() function [2], that prints what is really in the memory:

...
print O.bodies[0].state.pos[0].hex()

cheers
Jan

>
[1] https://docs.python.org/2/tutorial/floatingpoint.html
[2] https://docs.python.org/2/library/stdtypes.html#float.hex

Follow ups

References