Big Math problem
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
May 2010
Posts
33

## Big Math problem

I am developing a project in plain C++ using VS2010, I need to calculate the arc cosine of a real value (can be out of -1;1 range, so need to deal with complex numbers) and compare it with a double to see whether or not a threshold is satisfied

To make things clear, I need to convert this code (which works in matlab)
Code:
```   % dotprods is a vector of real values

tempvar=acos(dotprods); % inverse cosine, some of these values are complex

[vals,indx] = sort(tempvar);  % Take inverse cosine and sort results

% value compare
if (vals(1) < threshold * secondvalue)
% condition is TRUE
else
% condition is FALSE
end```

Unfortunately the acos function does not work with complex numbers.

I tried to arrange things this way:

Code:
```for(int j...) // cycle values of the vector
{
// dotprods is a ublas double vector with real values

complex<double> c(0,1);	// i
complex<double> one(1,0);    // 1

complex<double> _acos = -c*log(dotprods[j]+c*pow(one-pow(dotprods[j],2),1/2));

// here there is a cycle to sort values

if( abs(_acos) < threshold * secondvalue) // threshold and secondvalue are double
// TRUE
else
// FALSE
}```

The problem is that the formula I wrote (should be equal to arccosine function theorically) is not working. Results are wrong.

Can I use something like "cacos" function in a VS2010 C++ program?

2. ## Re: Big Math problem

Originally Posted by pm44xl22
[code]
complex<double> _acos = -c*log(dotprods[j]+c*pow(one-pow(dotprods[j],2),1/2));
You have integer division here. The result of 1 / 2 is 0. Use 0.5 if you want a floating point number. Better yet use the function sqrt (square-root).
Also, instead of using pow(x, 2) just use x * x. It's faster if the compiler is not able to do this for you (I doubt it is).

3. Member
Join Date
May 2010
Posts
33

## Re: Big Math problem

Thank you D_Drmrr, you solved my problem. I did not notice that, you was absolutely right

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•