Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. JavaScript math / rounding in QML

JavaScript math / rounding in QML

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 26.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frankiefrank
    wrote on 25 Oct 2011, 10:48 last edited by
    #1

    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.

    "Roads? Where we're going, we don't need roads."

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 25 Oct 2011, 12:31 last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on 25 Oct 2011, 14:09 last edited by
        #3

        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.

        "Roads? Where we're going, we don't need roads."

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on 25 Oct 2011, 23:43 last edited by
          #4

          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

          1 Reply Last reply
          0

          1/4

          25 Oct 2011, 10:48

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved