1

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!

khfrekek
  • 211
  • 1
  • 5
  • 12
  • What data type is contained in the array `data`? If it's integers, you may need to add some casts to double. https://stackoverflow.com/questions/7571326/why-cant-i-return-a-double-from-two-ints-being-divided – mittmemo Jun 25 '15 at 16:22
  • The array data[][] is full of doubles, and the variable dp is also of type double. :/ – khfrekek Jun 25 '15 at 16:28

1 Answers1

2

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;
OttoK
  • 166
  • 3
  • Wow I can't believe I missed that! It works fine now, I guess It was just dividing the output of acos() by 2 instead of the argument.. Thank you so much! – khfrekek Jun 25 '15 at 16:36