In Python when I raise 10 to the minus sixth power:
>>> 10**-6
1e-06
It will display 1e-06.
Is there a noticable difference between writing 10**-6 and 0.000001 as displayed in the interpreter? Or is it just representation / formatting difference.
To test if two float values are exactly equal, just use ==:
>>> 0.000001 == 10**-6
True
You may be confusing representation with the value. Python formats a float, when echoed in the interpreter, with the repr() function, and it represents the value by formatting with the g notation; this notation switches to using the scientific representation (e formatting) when the exponent gets large enough. repr() is effectively the same as format(value, '.16g').
You can format the numbers manually:
>>> format(10**-6, '.53f')
'0.00000099999999999999995474811182588625868561393872369'
>>> format(0.000001, '.53f')
'0.00000099999999999999995474811182588625868561393872369'
where .53f formats the value with up to 53 decimal numbers, a semi-arbitrary number based on the limits of what a floating point value can encode.
And indeed, both values are exactly the same. This was not a given; a float calculation can easily introduce small errors as float numbers are but approximations with binary fractions, after all.
FWIW in order to convince yourself, you might use the marshal module to see a binary representation of the object:
>>> import marshal
>>> marshal.dumps(10**-6)
'g\x8d\xed\xb5\xa0\xf7\xc6\xb0>'
>>> marshal.dumps(0.000001)
'g\x8d\xed\xb5\xa0\xf7\xc6\xb0>'
As you can see, both values have the same binary representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With