This project has moved. For the latest updates, please go here.

Why are the Fractions way off?

Mar 15, 2013 at 3:35 PM
Hi all,
New to Python and to this great tool.
Was wondering why when I have this code in the Visual Studio Python Env what I am seeing is this for output. running outside of VS it looks what I was expecting

import fractions
x = fractions.Fraction(1/3)



output something like:

Seems almost like it's not being initialized? Anyone else seeing this?
Mar 15, 2013 at 9:44 PM
It's being initialised, but it is getting the result of "1/3", which cannot be represented exactly as binary. Using fractions.Fraction(1, 3) will give the result you expect, because it doesn't perform the division.

I'd look up "floating point rounding error" for more information - this is a very old problem.