Moment Type Casting Crash

Former Member
Former Member
Hi,

I am working on an app that reacts to tap events, averaging the time between the taps. I am having trouble with storing the difference between two Moment values.

The first tap registers correctly, and
setupLastTime = Time.now().value();
is executed correctly with the proper value.

on the second tap,
setupThisTime = Time.now().value();
executes, but the app crashes on
beatTime += (setupThisTime - setupLastTime).toDouble();

with the error
UnexpectedTypeException: Expected Number/Float/String/Long/Double, given Object/Double
found in the logs.

I guess I'm not quite sure how types work in Monkey C.

Any help would be much appreciated, as this is my first Connect IQ app! ^^
  • beatTime += (setupThisTime - setupLastTime).toDouble();

    Ok, so your taking two "unix times", subtracting them, and giving you a number in seconds.

    You're then taking a number, converting it to a double and adding it to beatTime. What is beatTime? How is beatTime defined?
  • Former Member
    Former Member over 8 years ago
    beatTime += (setupThisTime - setupLastTime).toDouble();

    Ok, so your taking two "unix times", subtracting them, and giving you a number in seconds.

    You're then taking a number, converting it to a double and adding it to beatTime. What is beatTime? How is beatTime defined?


    beatTime is declared with all of my other variables in the class before the initialize() function.
    var beatTime;

    it is then initialized as a Double (I believe?) in the onLayout() function
    beatTime = 0.0;
  • beatTime is null...

    Try

    var beatTime=0.0d;

    if you really want a double. But why do you need it to be a double? It will only be a number which will easily fit in 32 bits.

    var beatTime=0;

    should also work...

    I'm not sure what you mean about it being initialized in onLayout()...
  • Former Member
    Former Member over 8 years ago
    beatTime is null...

    Try

    var beatTime=0.0d;

    if you really want a double. But why do you need it to be a double? It will only be a number which will easily fit in 32 bits.

    var beatTime=0;

    should also work...

    I'm not sure what you mean about it being initialized in onLayout()...


    even if I store the times as Moments and use the built in subtract function,
    beatTime += (setupThisTime.subtract(setupLastTime).value().toDouble());
    I still get this error
    UnexpectedTypeException: Expected Number/Float/Long/Double, given Object
    and cannot seem to cast to the proper type in any way.

    I have been having trouble with this for a few days now.

    It needs to be a Double as it is the sum of differences between the unix times (sadly i believe i can only be as precise as seconds in Monkey C), which, at the end of a timer, is divided by the number of differences to compute the average difference, which may or may not be an integer.

    If I had to guess I would say that the size of the numbers could have something to do with it?
  • It's likely because where you have

    var beatTime;

    and beatTime is null and you need

    var beatTime=0.0;

    You can verify this by adding:

    Sys.println("beatTime="+beatTime);

    before you try to change it, so see what it is and what this change does.
  • Former Member
    Former Member over 8 years ago
    It's likely because where you have

    var beatTime;

    and beatTime is null and you need

    var beatTime=0.0;

    You can verify this by adding:

    Sys.println("beatTime="+beatTime);

    before you try to change it, so see what it is and what this change does.


    Turns out I was assigning beatTime in two different places, and one was still a Duration.

    Seems like this bug is fixed.

    Thank you for your prompt responses and for your suggestion, it led me down the right path for sure.
  • No Problem...

    But keep the Sys.println() concept in mind, as that can tell you a bunch about what's going on in the code.

    And I'm still not sure why you need a double for this, BTW... Why not just use a number?
  • Former Member
    Former Member over 8 years ago
    If you want more precise timing, you can use the free running millisecond timer from the system to compute the time difference.

    It is accessed with Toybox.System.getTimer()