Dividing Ints won't give a Float?

Monkey Forums/Monkey Programming/Dividing Ints won't give a Float?

ondesic(Posted 2013) [#1]
I am porting a game. I have been pulling my hair for a while trying to figure out why my code doesn't work in Monkey. Finally, narrowed it down to a wacky problem

It looks like this:



"test" ends up equalling 0 instead of 0.5
Right now I have to make sure both 1 and 2 are floats.
Where this really gets troublesome is when I am not using literals, but a function like:

test = DeviceHeight()/45

My game has hundreds of divisions. I would hate to go through and have to change every number to a float.
Shouldn't monkey automatically do this?


Jesse(Posted 2013) [#2]
That is correct and it serves a purpose as I have been able to take advantage of it and I am sure many here have also.

the thing is that an integer divided by an integer will give you an integer but a float divided by an integer or an integer divided by a float will give you a float. So at least one of the operands have to be a float for the result to be a float. I am not sure if it's explained in the monkey documentation but I believe it is.


Xaron(Posted 2013) [#3]
You have to make:

test = 1.0/2.0


which is convinient with the behaviour of other languages. You just do an integer division in your example.


GfK(Posted 2013) [#4]
They don't both have to be floats - just one of them. But it's normal behaviour. Mathematical operations on integers, will give you an integer back.


Jesse(Posted 2013) [#5]
it works by casting also:

[monkeycode]
local a:float
local b:int = 3
local c:int = 4
a = b/Float(c)

[/monkeycode]