JavaScript math / rounding in QML



  • I know this may be somewhat unrelated but I'm getting a bit confused with what's happening with my numbers when I try to round them.

    Code:
    @

    property real stepSize: 0.1

    // in a onPressed handler:

    var val = 1656.5001177077993

    console.log("before round: " + val);
    var rounded = Math.round(val);
    console.log("after round: " + rounded);
    var roundedTimesStepSize = rounded * stepSize;
    console.log(roundedTimesStepSize);
    @
    Output:
    before round: 1656.5001177077993
    after round: 1657
    165.70000000000002

    Why am I not seeing 165.7 as a result?
    My problem is that I'm comparing this to a "previous value" and if it was "165.7" my code behaves as if the values changed, although I would have expected it to be the same.



  • floating point numers are always a bit "fuzzy".
    Thats why there is "qFuzzyCompare":http://doc.qt.nokia.com/4.7/qvector4d.html#qFuzzyCompare
    I don't know what to use in JS/qnml for that, but you should not compare it to fixed number.



  • Thanks for the tip. Is that really the best practice? I feel a bit weird sending this out to C++. It's incredibly hard to google for QML specific Math object code usage by the way.



  • Hi,

    Note that the Math object in QML is the standard EcmaScript Math object -- the results of a web search for e.g. "JavaScript Math object" or "Math.round()" should be relevant for QML as well.

    You can do your own fuzzy comparison in QML using something like the following (this is what the QML test framework does, for example):
    @
    if (Math.abs(actual - expected) <= 0.00001) {
    //they are equal
    } else {
    //they are not equal
    }
    @

    Another possibility might be one of the techniques described at http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript .

    Regards,
    Michael


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.