On Apr 16, 2015 1:32 AM, "Jim Mooney" <cybervigila...@gmail.com> wrote: > > Why does Fraction interpret a number and string so differently? They come > out the same, but it seems rather odd > > >>> from fractions import Fraction > >>> Fraction(1.64) > Fraction(7385903388887613, 4503599627370496) > >>> Fraction("1.64") > Fraction(41, 25) > >>> 41/25 > 1.64 > >>> 7385903388887613 / 4503599627370496 > 1.64
In many systems, if everything is the same shape, then certain operations might be implemented more quickly by making uniform assumptions. If all my clothes were the same, for example, maybe I'd be able to sorry my laundry more quickly. And if all my Tupperware were the same size, then maybe my cabinets wouldn't be the nest of ill fitting plastic that it is now. And if every number was represented with a fixed quantity of bits in a computer, then maybe computer arithmetic could go really fast. It's this last supposition that should be treated most seriously. Most computers use "floating point", a representation of numbers that use a fixed set of bits. This uniformity allows floating point math to be implemented quickly. But it also means that it's inaccurate. You're seeing evidence of the inaccuracy. Read: https://docs.python.org/2/tutorial/floatingpoint.html and see if that helps clarify. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor