python - Converting number in scientific notation to int -


could explain why can not use int() convert integer number represented in string-scientific notation python int?

for example not work:

print int('1e1') 

but does:

print int(float('1e1'))  print int(1e1)  # works 

why int not recognise string integer? surely simple checking sign of exponent?

behind scenes scientific number notation represented float internally. reason varying number range integer maps fixed value range, let's 2^32 values. scientific representation similar floating representation significant , exponent. further details can lookup in https://en.wikipedia.org/wiki/floating_point.

you cannot cast scientific number representation string integer directly.

print int(1e1)  # works 

works because 1e1 number float.

>>> type(1e1) <type 'float'> 

back question: want integer float or scientific string. details: https://docs.python.org/2/reference/lexical_analysis.html#integers

>>> int("13.37") traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: invalid literal int() base 10: '13.37' 

for float or scientific representations have use intermediate step on float.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -