June 8th, 2010, 04:20 PM
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)
% 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
% condition is FALSE
Unfortunately the acos function does not work with complex numbers.
I tried to arrange things this way:
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
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?
I need a help please
June 8th, 2010, 04:43 PM
Re: Big Math problem
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).
Originally Posted by pm44xl22
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).
Cheers, D Drmmr
Please put [code][/code] tags
around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
June 9th, 2010, 05:32 AM
Re: Big Math problem
Thank you D_Drmrr, you solved my problem. I did not notice that, you was absolutely right
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!