Borland bug

This is a discussion on Borland bug within the C++ Programming forums, part of the General Programming Boards category; Hey try this: Code: #include <iostream.h> #include <math.h> int main(void) { cout << cos(90); return 0; } The answer I ...

  1. #1
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82

    Borland bug

    Hey try this:

    Code:
    #include <iostream.h>
    #include <math.h>
    
    int main(void)
    {
      cout << cos(90);
      return 0;
    }
    The answer I get is -0.448074, instead of 0. This seems to be consistent throughout all Borland compilers, even with their latest release. It's seriously ruining my matrix calculations. Any thoughts?
    Last edited by Rez; 12-02-2003 at 08:17 AM.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I'm just guessing, but it looks like the cos() uses radians. Try

    Code:
    #include <iostream.h>
    #include <math.h>
    #define PI 3.141592654
    
    int main(void)
    {
      cout << cos(PI/2);
      return 0;
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The answer I get is -0.448074.
    And this is unexpected, how?

    >but it looks like the cos() uses radians
    Yes, it does. This is well documented too.
    My best code is written with the delete key.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    also, try something like this:

    Code:
    float cosdeg(float deg)
    {
        return cos(deg * (PI/180));
    }
    Try using that function with degrees (like 90 in your example). I'm surprised if borland's math.h doesn't have trig functions that use degrees.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Originally posted by Prelude
    >>but it looks like the cos() uses radians
    Yes, it does. This is well documented too.
    Sorry Prelude, haven't used them so wouldn't know. Was just guessing.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Checked my Borland compiler. It doesn't say anything about cos() using deg or rad parameters, so I assumed it's deg. However, the acos() function returns a radian, so now I'm kinda confused. I'll try your cosdeg() idea neandrake.
    Last edited by Rez; 12-02-2003 at 08:36 AM.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Great, tell me how it goes
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Hey you guys are right. Borland trig functions accept radians as parameter inputs. And that means some of my programs I submitted in class 2 years ago were incorrect

    Anyway, here's my modified code. Thanks for your info.

    Code:
    #include <iostream.h>
    #include <math.h>
    
    #define PI            acos(-1)
    #define deg(x)     (x * PI/180)  //x is in degrees
    
    int main(void)
    {
      cout << cos(deg(90));
      return 0;
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > And that means some of my programs I submitted in class 2 years ago were incorrect
    Should you be worried if your tutors failed to spot them?
    How wrong could a program be before they notice I wonder...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    >>Should you be worried if your tutors failed to spot them?<<

    I know They were too fascinated with my GUI's to even notice it.

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation FYI

    Borland trig functions accept radians...
    Rez,
    FYI - This is NOT a Borland-specific issue. It's defined in the C++ standard. Here's a reference for ya' : cppreference.com

  12. #12
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Okay thanks

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21