32
votes

Could someone explain why I can not use int() to convert an integer number represented in string-scientific notation into a python int?

For example this does not work:

print int('1e1')

But this does:

print int(float('1e1'))

print int(1e1)  # Works

Why does int not recognise the string as an integer? Surely its as simple as checking the sign of the exponent?

3
For the same reason that int() doesn't work on, say, '1.0': it is intended for integers. - TigerhawkT3
What do you think would happen with int('hello world')? It fails for the exact same reason that int('1e1') does - int() parses integer strings, like it says on the tin. - TigerhawkT3
1e1 is an integer! It is the number 10. I just wanted to know why you have to interpret a string of it as a float in order to realise it is an integer. - kezzos
No it isn't. Put 1e1 into an interpreter and watch it return 10.0. If that doesn't say "float" to you, try type(1e1) and watch it return <class 'float'>. Of course, the fact that only float(), and not int(), could parse the string '1e1' is a good indication, as well. - TigerhawkT3
The question is: why, when Python treats ints as floats (numeric hierarchy)… does it not treat a clear int as an int. How do you do an int that's 1e100? Try it. In Python3. Fun. 1e1000? Right out. - Jürgen A. Erhard

3 Answers

18
votes

Behind the scenes a scientific number notation is always represented as a float internally. The reason is the varying number range as an integer only maps to a fixed value range, let's say 2^32 values. The scientific representation is similar to the floating representation with significant and exponent. Further details you can lookup in https://en.wikipedia.org/wiki/Floating_point.

You cannot cast a scientific number representation as string to integer directly.

print int(1e1)  # Works

Works because 1e1 as a number is already a float.

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

Back to your question: We want to get an integer from 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 for int() with base 10: '13.37'

For float or scientific representations you have to use the intermediate step over float.

2
votes

Very Simple Solution

print(int(float(1e1)))

Steps:- 1- First you convert Scientific value to float. 2- Convert that float value to int . 3- Great you are able to get finally int data type.

Enjoy.

0
votes

Because in Python (at least in 2.x since I do not use Python 3.x), int() behaves differently on strings and numeric values. If you input a string, then python will try to parse it to base 10 int

int ("077")
>> 77

But if you input a valid numeric value, then python will interpret it according to its base and type and convert it to base 10 int. then python will first interperet 077 as base 8 and convert it to base 10 then int() will jsut display it.

int (077)  # Leading 0 defines a base 8 number.
>> 63
077 
>> 63

So, int('1e1') will try to parse 1e1 as a base 10 string and will throw ValueError. But 1e1 is a numeric value (mathematical expression):

1e1
>> 10.0

So int will handle it as a numeric value and handle it as though, converting it to float(10.0) and then parse it to int. So Python will first interpret 1e1 since it was a numric value and evaluate 10.0 and int() will convert it to integer.

So calling int() with a string value, you must be sure that string is a valid base 10 integer value.