Division | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Division

Hi there. I get the point and I quit appreciate the concept. But having the division operator choice returning an integer is odd, isn't it? In most cases an integer divided by another integer will result in a float or double. Will there be an error or some sort of round value? Thanks.

31st Jan 2019, 4:20 PM
ChrA
ChrA - avatar
1 Answer
0
If you divide an integer by another, it will give an integer. But if you divide a float by an integer, it will give a float. Modern languages saved the old syntax, for example from Pascal, when an integer could be divided only by "div" - division and "mod" - module and the / division could be used only by "real" type, or float. So to be simpler, in modern langauges the / division was made global, and only changes in the calculus and logical use. If you want a float from a division of 2 int just make this: int x = (float)12 for a quick conversion. But in most cases you'll want an int.
31st Jan 2019, 6:43 PM
Charlie S
Charlie S - avatar