Weird Compiler Issue

I'm getting this:

Error: Unhandled Exception
Exception: UnexpectedTypeException: Expected Number/Long, given Number/Float

When I run This:

var speedValue = 0;
var eteMinsValue = 0;
var eteHoursValue = 0;


if(info has :distanceToNextPoint ){
   if(info.distanceToNextPoint != null){
      drValue = info.distanceToNextPoint / 1000;// to Km
      if(speedValue != 0){
         eteHoursValue = drValue / speedValue;
         eteMinsValue = (eteHoursValue % 1)*60;
      }
   }
}

The issue goes away if I remove the modulus operator and change it with whatever other operator

  • I'm guessing that speedValue is a float here (maybe it comes from ActivityInfo.currentSpeed?). That makes eteHoursValue a float as well.

    In Monkey C, modulus (%) is only defined for integers, which is why your app crashes when you try to use it with a float. In this case, you could replace "eteHoursValue % 1" with "eteHoursValue.toNumber()". Or you could replace "eteHoursValue = drValue / speedValue;" with "eteHoursValue = (drValue / speedValue).toNumber();"

    Note that these examples will truncate, not round. If you need to round, use Math.round() in addition to toNumber(). e.g.

    eteHoursValue = Math.Round(drValue / speedValue).toNumber();

    If you need a version of mod that works with floats and doubles, you could implement your own.

    I use something like this, in a data field where I allow the user to enter arbitrary math formulas (like Excel), and mod is one of the operators available to the user:

    function mod(x, y) {

      return x - y * Math.floor(x/y).toNumber();

    }

  • Thanks
    I multiplied by 100, converted to number and then did a %100 instead of a %1. I didnt know modulus didnt accept floats.

    It works now Slight smile