Minutes per Kilometer

how do i convert this to minutes per kilometer:

 

function toSpeed(s) {

    if (s != null && s > 0.1) {

        if (Sys.getDeviceSettings().distanceUnits == Sys.UNIT_METRIC) {

            var kmPerHour = s * 60.0 * 60.0 / 1000;

            if (kmPerHour > 100) {

                return kmPerHour.format("%d");

            } else {

                return kmPerHour.format("%.1f");

            }

        } else {

            var milesPerHour = s * 60.0 * 60.0 / 1609;

            if (milesPerHour > 100) {

                return milesPerHour.format("%d");

            } else {

                return milesPerHour.format("%.1f");

            }
        }

    }

    return "0.0";
}

i´t trying to use this but ir returrns a float error

(kmPerHour/ 60).format("%01d") + ":" + (kmPerHour % 60).format("%02d");

  • Two things. Assuming that you're trying to insert that code into the definition of toSpeed...

    1. kmPerHour is a Float, and % on a Float does not work in MonkeyC
    2. kmPerHour is a unit of speed, but you're trying to display it as time. You have kmPerHour, but what you really want is minutes per kilometer.

    Assuming that s is meters per second, you probably want something like this:

    var secondsPerMeter = 1.0 / metersPerSecond;
    var secondsPerKilometer = 1000 * secondsPerMeter;

    this can be written more simply in a single step as:

    var secondsPerKilometer = 1000.0 / metersPerSecond;

    Once you have a unit of time, you can break it down into minutes/seconds.

    // round to nearest whole second
    secondsPerKilometer = (secondsPerKilometer + 0.5).toNumber();
    
    // now that we have a Number, we can use %
    var minutes = (secondsPerKilometer / 60);
    var seconds = (secondsPerKilometer % 60);
    
    var formattedTime = Lang.format("$1$:$2$", [ minutes, seconds.format("%02u") ]);
    
    

  • thanks a lot tarvis i missed you!!! i´ll try tomorrow and it just another question: i have been reading that converting operations to float reduces memory but also precision, is this true?

  • Floating point numbers (both Lang.Float and Lang.Double) have advantages and disadvantages when compared to fixed point numbers (Lang.Number and Lang.Long). It is probably not a bad idea to go find a book on the topic, but I think I can give a brief summary:

    1. Floating point arithmetic is typically costlier in terms of CPU cycles than fixed point. For purposes of this conversation, mixed arithmetic (where one operand is a floating point and the other is fixed point) can be considered a floating point operation.
    2. Floating point number systems have more precision than fixed point when the values are relatively small. A 32-bit fixed point number cannot represent values between 0 and 1 at all, but a 32-bit float can represent something like 1,056,964,610 unique values in the range [0, 1].
    3. Floating point number systems have less precision than fixed point when the values are relatively large. A 32-bit float using IEEE754 format is able to represent whole numbers from -16,777,216 to 16,777,216 exactly. For values outside that range, you might want to use something that has more precision (like a double) or used fixed point values.
    4. There is almost always error with floating point, and sometimes it is important to know about the representation and how to determine the error.
    5. A 32-bit floating point value is no costlier to store than a 32-bit fixed point value; both take exactly 32 bits. Same for a 64-bit float vs 64-bit fixed. In MonkeyC a 32-bit value (Float or Number) do not require a heap allocation, but a 64-bit value (Double or Long) do.
  • thanks a lot this solves my question!!

    (where one operand is a floating point and the other is fixed point) can be considered a floating point operation, now the field Is working!!!