Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

what is the type of int*float?

OpenStudy (anonymous):

the answer will come out as a float even for like 4.5*2 the answer will come out 9.0 instead of just 9.

OpenStudy (anonymous):

this is called the type conversion and the precedence of float is greater than int thus answer must be in float

OpenStudy (carlsmith):

ints generally become floats whenever it'd be obvious for them to do so, floats never become ints unless you explicitly tell them to do so.

OpenStudy (anonymous):

It is float because most of the compilers does automatic type conversion during the semantic analysis phase. So, in general all the types of the variable involved in an expression are converted into one single type (The highest type .. in your case, float) before the equation is evaluated...

OpenStudy (anonymous):

@carlsmith Your not completely wrong but that wouldn't be the best explanation because look at lets say 10/4 you would expect it to return 2.5 but it returns 2 so therefore it doesn't become a float when it needs to only when given a float. (kenators explanation is correct)

OpenStudy (carlsmith):

@Onyx - If you write the expression 10 / 4, you get 2 because this, at least in Python2, does floor division. It has nothing to do with intuitive type conversion. If you take the two ints, 10 and 4, and do sane division 10 // 4, you get 2.5, a float, as expected. In Python3, 10 / 4 returns a float and 10 // 4 returns an int. This is an issue with the counter-intuitive syntax of division operators before Python3, it's nothing to do with ints and floats.

OpenStudy (anonymous):

Most people that take this course use 2.x because thats what the teacher uses. And I said you weren't completely wrong that just wasn't the best way to explain for someone that doesn't understand type conversion unless you already knew he was using 3.xsyntax.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!