Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] incorrect calculation in Qt Script
Forum Updated to NodeBB v4.3 + New Features

[Solved] incorrect calculation in Qt Script

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 5.5k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    Ruzik
    wrote on 2 Oct 2011, 18:20 last edited by
    #1

    While when i calculete Math.sin(Math.PI) i get this answer: 1.2246467991473532e-16 instead of 0
    Same with Math.cos(Math.PI/2) = 6.123233995736766e-17
    Why is this happening?
    I advance thank you for your help!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 2 Oct 2011, 18:46 last edited by
      #2

      THis is not a Qt Script problem, but a computer problem. There is a limit to the precision of numbers you can express in floating point numbers. That is what you are seeing. You should compare them using an epsilon.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on 3 Oct 2011, 08:59 last edited by
        #3

        Limited numerical precision of the floating point representation of numbers is the problem.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Ruzik
          wrote on 3 Oct 2011, 10:15 last edited by
          #4

          As i understude, you dont understunde me(maybe i am dont right, my english is bad) i say not about
          e-16 in 1.2246467991473532e-16, i say about sin(pi) should be equal 0 as cos(pi/2)

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on 3 Oct 2011, 10:24 last edited by
            #5

            As far as I understand the problem is that Math.PI does not express the pure PI, since Math.PI is limited by the precision of the computer floating numbers. I guess you will have the same result (or a similar one) using another math library.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 3 Oct 2011, 10:30 last edited by
              #6

              [quote author="Ruzik" date="1317636902"]As i understude, you dont understunde me(maybe i am dont right, my english is bad) i say not about
              e-16 in 1.2246467991473532e-16, i say about sin(pi) should be equal 0 as cos(pi/2)[/quote]

              I'm afraid they are right, mate. With floating point, you cannot really get exactly 0, save for a few cases.

              (Z(:^

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 3 Oct 2011, 10:33 last edited by
                #7

                [quote author="Ruzik" date="1317636902"]As i understude, you dont understunde me(maybe i am dont right, my english is bad) i say not about
                e-16 in 1.2246467991473532e-16, i say about sin(pi) should be equal 0 as cos(pi/2)[/quote]

                Well, 6.123233995736766e-17 ≈ 0, as far as floating point numbers are concerned.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Ruzik
                  wrote on 3 Oct 2011, 10:43 last edited by
                  #8

                  Clear, thank you for your help!

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    ZapB
                    wrote on 3 Oct 2011, 10:44 last edited by
                    #9

                    Also, the trigonometric functions are transcendental functions and so cannot be calculated exactly. All implementations use approximations that are good to some degree of accuracy for a range of inputs.

                    As Andre suggested, test it with an epsilon (small value) to see if it is in agreement with your criteria. i.e.

                    @
                    const double eps = 1.0e-10; // as an arbitrary example
                    double value = sin( pi ); // pi defined somewhere else
                    const double test = 0.0;
                    if ( qAbs( value - test ) < eps )
                    qDebug() << "Values agree within some small value epsilon";
                    @

                    Obviously you can use different values for eps and test as needed in your particular cases. This is just a fact of life when working with floating point representations on machines with limited memory.

                    Nokia Certified Qt Specialist
                    Interested in hearing about Qt related work

                    1 Reply Last reply
                    0

                    1/9

                    2 Oct 2011, 18:20

                    • Login

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