user687459
user687459

Reputation: 163

wrong calculation by borland builder c++

I'm using Borland BCB6 C++ Builder. My code contains the following computation:

float result= (152*pow(cos(80),2)) + (70*pow(sin(80),2));

In debug mode, I find that this expression evaluates to about 70.992, whereas Wolfram Alpha tells me that the value should be about 72.4726. What could be the reason for this discrepancy?

Upvotes: 1

Views: 164

Answers (1)

Spektre
Spektre

Reputation: 51845

In math.h the goniometric functions use radians!!!

  • so instead 80.0 [deg] use 80.0*M_PI/180.0 [rad]
  • that will convert the angle from degrees to radians
  • I also usually define deg,rad constants and use them:

    const double deg=M_PI/180.0;
    const double rad=180.0/M_PI;
    double result= (152.0*pow(cos(80.0*deg),2.0)) + (70.0*pow(sin(80.0*deg),2.0));
    

Upvotes: 1

Related Questions