Thursday, March 3, 2011

Don't assume those funcstions are exactly the same!

If you see functions sharing the same name in different programming environments, don't jump to a conclusion that they are exactly the same!
Today it took me a lot of time to find a bug in a VBA macro. I just didn't understand why the function atan2(a,b) cannot return the correct answer.
After carefully reading the description of this function in the Microsoft Visual Basic Help, I realized that I mistakenly assumed that the atan2() in VBA (worksheet functions) is the same as the atan2() in c++. I was wrong!!

In Excel Developer Reference:
Atan2(x,y)
returns the arctangent of the specified x and y coordinates.

In C++ Developer Reference:
C Library, cmath (math.h)
     double atan2 (      double y,      double x );
long double atan2 ( long double y, long double x );
float atan2 ( float y, float x );
y: Floating point value representing an y-coordinate.
x: Floating point value representing an x-coordinate

Difference:

See?! The meanings of the first and the second parameters in C++ are switched in VBA!!




No comments:

Post a Comment