Math.h not working properly?

295 views Asked by At

I'm having a very strange problem which has been driving me crazy for a while. Part of my code requires c++ to calculate some simple arithmetic using math.h, but it is spitting out completely incorrect values! Here is this section of the code:

for(int i = 0; i < data.size(); i++)  {

cout << (data[i][8]/dp)/2 << " : " << -log(tan(acos(data[i][8]/dp)/2)) << endl;

}

which gives the following output:

0.5 : inf
0.5 : inf
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
0.5 : inf
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
0.5 : inf
0.5 : inf
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
-0.5 : -37.3319
-0.5 : -37.3319
0.5 : inf
0.5 : inf
-0.5 : -37.3319
0.5 : inf
0.5 : inf

Which of course, is completely wrong, plugging 0.5 or -0.5 into a -ln(tan(arcos())) on any calculator gives you -0.54 and -.54+3.14i, while math.h is somehow returning inf and -37. Does anyone have any insight on how this could be happening? Thanks in advance!

1

There are 1 answers

1
OttoK On BEST ANSWER

I think you have missing parentheses. In the output line, the first value is (data[i][8]/dp)/2, but that's not what you're using in function call (spaces added for clarity):

 -log( tan( acos( data[i][8]/dp ) /2) )

Correct line should be:

cout << (data[i][8]/dp)/2 << " : " << -log(tan(acos((data[i][8]/dp)/2))) << endl;