Unexpected Type Error

Banging my head on the wall w/ this one over the past hour. Yeah.. been trying this for the past 1+ hr to no avail)
It really is a simple thing, but for the life of me, I can't get it to work.

if (Act.getActivityInfo().elapsedDistance != null) {
var aaa = (Act.getActivityInfo().elapsedDistance);
var bbb = (Act.getActivityInfo().elapsedDistance/DistUnit);
var ddd = (Act.getActivityInfo().elapsedDistance/1000.0) % 1;
Sys.println(aaa + ":" + bbb + ":" + ccc + ":" + ddd);


It's very simple - I just want to implement autoLap and want to do a MOD to get a remainder of 0 to execute a new lap.

But I keep getting the unexpected type error when I do the mod (the ddd row)
no matter what I tried, I keep getting errors.
divide by 1000 or 1000.0 or putting a format("%02d") or toNumber() or toString() or toFloat() all gives me the error.

I know it's really simple - but for the life of me, I don't know what's happening.
  • Former Member
    Former Member over 10 years ago
    You are passing a float to the modulus operation, which is the source of the unexpected type error.

    You will need to call .toNumber() on the result of your division before the modulus.

    I think that looks like this:
    var ddd = (Act.getActivityInfo().elapsedDistance/1000.0).toNumber() % 1;

    If that doesn't work, I would try it more explicitly like this:
    var ddd = (Act.getActivityInfo().elapsedDistance/1000.0);
    ddd = ddd.toNumber() % 1;
  • var ccc = (Act.getActivityInfo().elapsedDistance/DistUnit).format("%4.2f");
    var ddd = (Act.getActivityInfo().elapsedDistance/1000.0).toNumber() % 1;


    ccc:ddd ==> 15.93:0

    var ddd = (Act.getActivityInfo().elapsedDistance/1000.0);
    var eee = ddd.toNumber() % 1;


    ddd:eee ==> 27.508007:0

    both of these does while provides an error free number, it is NOT the correct number

    I should be getting the 0.5708007 instead of a 0 number

    var eee = ddd.toFloat() % 1.0;
    var eee = ddd.toNumber() % 1;
    var eee = ddd.toNumber() % 1.0


    the above does not work either
  • Former Member
    Former Member over 10 years ago
    I missed what you were attempting to do.
    Floating point modulus is not supported in Monkey C, so you cannot use modulus for the operation you are trying to do.
    Integer modulus by 1 will always equal 0

    You will need to do it this way:
    var float = 15.93;
    var decimalPart = float - float.toNumber() //decimalPart = 0.93
  • There is a problem that I reported before, that when you first start recording a session, "elapsedDistance" is a null. Could you also be seeing something like this?

    See this thread:

    https://forums.garmin.com/showthread.php?256229-elapsedDistance-in-a-vivoactive-app&highlight=elapsedDistance
  • Hi Jim -
    I'm checking for nulls. so no it's not this.
  • Former Member
    Former Member over 10 years ago
    I missed what you were attempting to do.
    Floating point modulus is not supported in Monkey C, so you cannot use modulus for the operation you are trying to do.
    Integer modulus by 1 will always equal 0

    You will need to do it this way:
    var float = 15.93;
    var decimalPart = float - float.toNumber() //decimalPart = 0.93



    About the float.toNumber conversion: doesn't this run into a roll-over when maxint is reached? For elapsedDistance, this would be around 16 km or 10 miles? Am I wrong?
  • Former Member
    Former Member over 10 years ago
    The source value of elapsedDistance is already a signed 32-bit value in meters. Unless you convert this to something in the range of micrometers, I don't think you are going to be recording a long enough activity to roll it over.