CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    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?

    I need a help please

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Big Math problem

    Quote Originally Posted by pm44xl22 View Post
    [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).
    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

  3. #3
    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

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured