Changed the float.__repr__() and float.__str__() methods in Python 2.7; The Python 3.1 float-to-string conversion method was fallback, and the values ββare now rounded.
The C source code for float.__str__() formats the floating point value using the g formatter code into the sprintf() function with an accuracy of 12 positions.
To get the same result in Python 2.6, you have to format the string yourself:
'%.12g' % fp_value
or use the format() function:
format(fp_value, '.12g')
Note that in Python 2.7, only the view has changed, not the actual values. Floating-point values ββare still binary approximations of real numbers, and binary fractions do not always coincide with the exact number.
If you need higher accuracy than the float approximations you are proposing, you need to switch to using the decimal.Decimal() provides accuracy at the expense of speed (floating-point arithmetic is processed by hardware on modern computers).
source share