[Tutor] Need a better name for this function
Hugo Arts
hugo.yoshi at gmail.com
Wed Dec 16 08:30:33 CET 2009
On Wed, Dec 16, 2009 at 5:12 AM, Richard D. Moores <rdmoores at gmail.com> wrote:
>
> Before I can go below I need to know if you are saying that the
> relevant doc is wrong. I took the original name for my function almost
> directly from it. Near the bottom of
> <http://docs.python.org/3.1/tutorial/floatingpoint.html#representation-error>
> we find "meaning that the exact number stored in the computer is equal
> to the decimal value
> 0.1000000000000000055511151231257827021181583404541015625." And
> coincidence or no, that's precisely what float2Decimal() returns for
> 0.1 .
>
The docs are right. The function will give you the exact value of any
floating point number stored in memory. I think what Dave is trying to
say is that if you want to store the exact value 0.1 on your computer,
this function won't help you do that. It also won't help you avoid any
kinds of rounding errors, which is worth mentioning. If you want 0.1
represented exactly you'll need to use the Decimal module all the way
and avoid floating point entirely.
Of course, if all you want is to get a better understanding of what's
actually stored on your computer when you try to store 0.1 in a float,
this can be a useful tool. I also recommend the article "What Every
Computer Scientist Should Know About Floating-point Arithmetic." It's
a very detailed explanation, though somewhat technical:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Hugo
More information about the Tutor
mailing list