Announcement

Collapse
No announcement yet.

About cosine function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • About cosine function

    Hi,
    could someone kindly explain to me why the simple code below give me a negative value for cosine of 90 degrees?

    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
    
      LOCAL angle AS extended
      
      angle = 0.0174532925199433## * 90
      
      PRINT USING$("#.##################",COS(angle))
      PRINT SGN(COS(angle))
      
      WAITKEY$
    
    END FUNCTION
    thanks!

    Sergio

  • #2
    Output my system (PB/CC 5.0.0) :
    Code:
    -.000000000000000381
    -1
    Since cos(90 degrees ) is zero, seems to me you are simply running into the rounding inherent in the use of floating point numbers and an undisciplined use of PRINT

    Of course you could try it this way...
    Code:
    FUNCTION PBMAIN () AS LONG
    
      LOCAL angle AS EXTENDED
      LOCAL pi AS EXTENDED
    
      angle = 0.0174532925199433## * 90
    
      PRINT USING$("#.##################",COS(angle))
      PRINT SGN(COS(angle))
      
      pi    =  ATN(1) * 4##
      PRINT    PI
      
      angle  = Pi/2.00##     '  90 degrees
      
      PRINT USING$("angle #.######  cosine  #.#######", angle, COS(angle))
     
    
      WAITKEY$
    
    END FUNCTION
    .. and get pretty much what you'd expect.

    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Hi Michael,
      tank you for the reply.
      I'm aware of the rounding problems related to the storage capabilities of the different numeric data types.
      IMHO the problem is related to the negative value, because checking the sign of trigonometric functions is useful in order to determine the trigonometric quadrant.
      In geometric algorithms where the cosine function is used, this problem can create wrong results.

      bye

      Sergio

      Comment


      • #4
        Sergio,
        Try a more accurate value for pi/180
        Code:
          angle = 0.01745329251994329577## * 90
        Paul.

        Comment


        • #5
          Sergio,

          Do not make checks to the exact values because the rounding errors may hinder such verifications, but check the proximity of data to desired values, like in
          Code:
            value## = COS(angle)
            IF ABS(value##) <= 5e-16 THEN 'value## is near zero
                'do something when zero
            END IF

          or try using the ROUND function
          "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

          Comment


          • #6
            Many thanks to all for the replies.
            The Paul's suggestion worked fine.

            Cheers

            sergio

            Comment


            • #7
              Originally posted by Sergio Tallone View Post

              Many thanks to all for the replies.
              The Paul's suggestion worked fine.

              sergio
              Actually, Paul's approximation to Pi/2 radians is not as accurate as Michael's approximation -- 2 * ATN(1) -- which produces the closest possible value to Pi/2 that can be represented in the extended precision format. As such, it provides a significantly closer value to zero for its cosine than Paul's value.

              Although Paul's approximation to Pi/2 results in a positive sign for the cosine (whereas Michael's approximation results in a negative sign), this is no more or less correct than a negative sign since the true sign of COS(Pi/2) is neither. Paul's approximation simply lands on a value that is slightly less than Pi/2, whereas Michael's lands on a value that is slightly more than Pi/2.

              The main point of this post is that your PowerBASIC calculations will never come up with an angle such that COS(angle) = 0, since that angle is not representable in the extended precision format. The cosine of an angle will go from a positive value in the first quadrant directly to a negative value in the second quadrant without producing a value of zero (90 degrees).
              Last edited by Walter Henn; 31 Mar 2009, 09:16 PM. Reason: Additional clarification.

              Comment

              Working...
              X