I know python is capable of handling arbitrary big integers, so why doesn't it give me one when using scientific notation? Why is 1e23 any different from 10**23?
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in range(30):
... x1 = eval('long(1e{})'.format(x))
... x2 = eval('long(1{})'.format('0' * x))
... print x1, 'OK' if x1 == x2 else 'YUCK!'
...
1 OK
10 OK
100 OK
1000 OK
10000 OK
100000 OK
1000000 OK
10000000 OK
100000000 OK
1000000000 OK
10000000000 OK
100000000000 OK
1000000000000 OK
10000000000000 OK
100000000000000 OK
1000000000000000 OK
10000000000000000 OK
100000000000000000 OK
1000000000000000000 OK
10000000000000000000 OK
100000000000000000000 OK
1000000000000000000000 OK
10000000000000000000000 OK
99999999999999991611392 YUCK!
999999999999999983222784 YUCK!
10000000000000000905969664 YUCK!
100000000000000004764729344 YUCK!
1000000000000000013287555072 YUCK!
9999999999999999583119736832 YUCK!
99999999999999991433150857216 YUCK!
1e23 is interpreted as a floating point number whereas 10**23 is an integer raised to an integer power (i.e. an integer)
Python floating point type use numbers native to the system architecture for computations - usually 64 bit floating points as defined here: http://www.ee.unb.ca/tervo/ee6373/IEEE64.htm I am not certian if it can build using other floating point data types by default - but I know that if the architecture where Python is running has peculiarities in F>p> number handling, those will show up in Python - therefore you could have differences in Python program running in x86 hardware to those running in PPC, ARM or other CPUs.
However, if you want to use arbitrary precision decimal numbers in Python it is possible with the module Decimal. As of the recently released Python 3.3, the speed for Decimal numbers is comparable to that of native floating point numbers (inside a Python program - native code able to use F.P. speedups such as SIMD instrucions would be much faster)
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