-3

I need help converting this Excel formula to C (using pure Win32 API, with atan2 from msvcrt). The Excel formula is:

=IF(AND(L9=0;M9=0);0;IF(M9>=0;DEGREES(ATAN2(L9;M9));DEGREES(ATAN2(L9;M9))+360))

I tried to port it, but I don´t understand the degree convention, because the values are not matching.

What is the correspondent C function for this Excel formula?


Ok, I edited the answer here. I suceeded to make a function that works exactly as the one in excel. I wrote it in assembler (using RosAsm assembler), but it shouldn´t be hard to port to C.

[Float_DoublePi: R$ (2*3.1415926535897932384626433832795028841971693993751058)]
[Float_180_Pi: R$ (3.1415926535897932384626433832795028841971693993751058/2)]
[ConvRadianToDegree: R$ (180/3.14159286)]
[FloatZero: R$ 0]

Proc ComputedH:
    Arguments @aFact, @bFact, @HFact
    Uses ebx, esi, ecx, edi, eax

    mov ebx D@aFact
    mov esi D@bFact
    mov edi D@HFact

    .Fpu_If_And R$ebx = R$FloatZero, R$esi = R$FloatZero
        fldz | fstp R$edi
    .Fpu_Else_If R$esi >= R$FloatZero
        Fpu_If R$ebx >= R$FloatZero
            fld R$Float_180_Pi
            C_call 'msvcrt.atan2' D$ebx, D$ebx+4, D$esi, D$esi+4
            fsubrp ST1 ST0 | fabs
        Fpu_Else
            fld R$Float_180_Pi
            C_call 'msvcrt.atan2' D$ebx, D$ebx+4, D$esi, D$esi+4
            fsubrp ST1 ST0 | fabs
        Fpu_End_If
    .Fpu_Else
        Fpu_If R$ebx >= R$FloatZero
            C_call 'msvcrt.atan2' D$esi, D$esi+4, D$ebx, D$ebx+4 | fadd R$Float_DoublePi
        Fpu_Else
            ; 360-X-(pi+pi/2) ==> 2PI-X-(pi+pi/2) = pi/2-X
            fld R$Float_180_Pi
            C_call 'msvcrt.atan2' D$ebx, D$ebx+4, D$esi, D$esi+4
            fsubrp ST1 ST0 | fabs
        Fpu_End_If
    .Fpu_End_If

    fmul R$ConvRadianToDegree |  fstp R$edi

EndP
guga
  • 79
  • 10
  • For example, if L9 = 2.6774 and M9 = -79,7751, the resultant value is 271,9222 (In degrees). Buyt i couldn´t achieve this result when tried to port – guga Sep 04 '15 at 23:17
  • You convert radians to degrees with a formula like `radians*(180/M_PI)` (That is what the function`DEGREES` does. No such formula in C but you just write your own simple function from that formula. The `M_PI ` constant is defined in `math.h` in C and is the value of _PI_ – Michael Petch Sep 04 '15 at 23:19
  • 1
    You need to add *What you tried* to the question. What did your `c` code look like. – Fantastic Mr Fox Sep 04 '15 at 23:38
  • The degree to radian and vice-versa is k, but ´m not being able to achieve the same results. For example, when i feed with the inputed values 92.6774 and -79.7751, the results are not the same as in excel. for example a call to atan2(2.6774,-79.7751)) = 178º, but, p´resuming i need to add 360, the resulatnt value is 538 and not 271.92 – guga Sep 04 '15 at 23:43

1 Answers1

3

Very simple lets look at it step by step:

=IF(AND(L9=0;M9=0) // Check this condition
  ;0               // Do this if true
  ;IF(M9>=0        // Check this if false
      ;DEGREES(ATAN2(L9;M9))
      ;DEGREES(ATAN2(L9;M9))+360))

so we can convert this very easily:

double output; // our output angle

if (L9 == 0 && M9 == 0) {
    output = 0;
} else {
    if (M9 >= 0) {
        output = atan2(L9, M9);
    } else {
        output = atan2(L9, M9) + 2*PI;
    }
}
output *= 180/PI;
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • thanks, i´ll give a try, and compare the results – guga Sep 04 '15 at 23:48
  • Thanks, but sill not working. If L9 = 2.6774 and M9 = -79,7751, the result is something around 538º, but why in excel the resultant value is 271,922 ??? – guga Sep 04 '15 at 23:53
  • I guess i suceed to create a proper function that works exactly as in excel. It seems that excel works in quadrants. And the "+360" it reverses the "x,y" coordinates and add a 2*PI. I built a function in asm for it (with RosAsm assembler). Not sure how to port it to C, but it should not be hard ate all. – guga Sep 05 '15 at 03:10
  • Don´w know how to put here the function i built to you guys see:( – guga Sep 05 '15 at 03:12
  • Thanks for all you guys. suceeded to make a Function that works exactly as in excel. edited the topic of the question and putted the answer there. – guga Sep 05 '15 at 03:20
  • And, for a better result. The function does simply this: result = (90-(180/pi)*atan2(x,y)). But, if result < 0, add to result 360. So, what excell function degree(atan2) seems to be doing is simply this:(90-(180/pi)*atan2(x,y)) – guga Sep 05 '15 at 03:53