5

I was looking for the .NET implementation of Atan2 in a reflector and found the following line:

public static extern double Atan2(double y, double x);

That is not surprising since it makes sense for most arithmetic functions to be implemented in native code. However, there was no DllImport call associated with this or other functions in System.Math.

The core question is about how the function implemented in native code but I would also like to know which native Dll it resides in. Also, why is there no DllImport? Is that because compilation strips it away?

Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • +1 see also: http://stackoverflow.com/questions/4162232/atan2-in-c-sharp-or-similar-lanaguge – Jeremy Thompson Jun 26 '13 at 05:34
  • There's no DllImport because the method uses the `MethodImp(MethodImplOptions.InternalCall)]` attribute (which is used for native code which is called directly by CLR methods). This is a special attribute for the CLR only. Also see this answer from Hans Passant: http://stackoverflow.com/a/8870593/106159 – Matthew Watson Jun 26 '13 at 06:20

2 Answers2

1

Looking at Math.cs you'll notice that Atan2 is implemented directly as an internal call.

[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern double Atan2(double y, double x);

This basically tells .NET to call an underlying C++ function.

More information at: Is it possible to link a method marked with MethodImplOptions.InternalCall to its implementation?

Download at: http://www.microsoft.com/en-us/download/details.aspx?id=4917

from comfloat.cpp:

/*=====================================Atan2=====================================
**
==============================================================================*/
FCIMPL2_VV(double, COMDouble::Atan2, double x, double y) 
    WRAPPER_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

        // the intrinsic for Atan2 does not produce Nan for Atan2(+-inf,+-inf)
    if (IS_DBL_INFINITY(x) && IS_DBL_INFINITY(y)) {
        return(x / y);      // create a NaN
    }
    return (double) atan2(x, y);
FCIMPLEND
Community
  • 1
  • 1
Mataniko
  • 2,212
  • 16
  • 18
0

Atan2 is just a quick bit of code wrapping around the actual mathematical function Atan (Useful for gamedev and a few other random programming situation.)

It's likely just directly defined, and then the Atan code it uses is external.

mcmonkey4eva
  • 1,359
  • 7
  • 19
  • Well Atan2 introduces some logic around quadrants, etc. It would use Atan internally but I was interested in seeing the logic as MS would have implemented it. – Raheel Khan Jun 26 '13 at 05:34