Mod always returns an integer

Monkey Forums/Monkey Programming/Mod always returns an integer

Warpy(Posted 2011) [#1]
The Mod operator chops off any decimal part in its operands.

Local x#=4.1, y#=3.1
x = x Mod y


gets rendered in javascript as

var bbt_x=4.2;
var bbt_y=3.1;
bbt_x=(((bbt_x)|0) % ((bbt_y)|0));


This isn't a bug, because it's been deliberately made to happen, but could it be done better? Which of the targets will only do modulo on integers?


Samah(Posted 2011) [#2]
http://www.monkeycoder.co.nz/Community/posts.php?topic=1478
http://www.monkeycoder.co.nz/Community/posts.php?topic=1309
http://www.monkeycoder.co.nz/Community/posts.php?topic=416


Floyd(Posted 2011) [#3]
We can roll our own floating point Mod with
Function fmod:Float( a:Float, b:Float )   ' a Mod b  for floats
	Return a - b * Int( a/b )
End

Take this with a grain of salt. It should be okay with Floats. But I'm a little vague on what happens if an Int is used for one or both arguments. The cross platform behavior could be a problem, especially if the Int is too big for a Float.

The built-in Mod is supposed to work with floats in the not too distant future.


Warpy(Posted 2011) [#4]
Yeesh. All because of 'mod='?