In this link, we can see the source code of System.Math
class. But I cannot find the source code of how sine is defined.
Is there anything I am missing here?
In this link, we can see the source code of System.Math
class. But I cannot find the source code of how sine is defined.
Is there anything I am missing here?
The signature of the method is:
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern double Sin(double a);
The extern
in the method means it is defined elsewhere. In this case, it will be implemented directly in the CLR (probably written in C or Assembly) which means there is no .NET implementation available.
You can't thought the .NET Framework source - it's an extern
function, meaning it's implemented in a lower-level library (probably the CLR itself, but I'm not certain).
You might try the Shared Source CLI
No you cannot see the source code of the sin function as its an extern function and embedded inside CLR. sine is implemented in microcode inside microprocessors, so this makes it really hard to check the implementation of the same as it may differ from platform to platform.
The extern modifier is used to declare a method that is implemented externally
And the sin function is declared as
public static extern double Sin(double x);
So it is not possible to see the source code for the sin function
I am not sure if this can be of any help but you may check the C version of the sin fucntion and also check sin function implementation.
If you want to see reconstructed source code you could try Telerik's JustDecompile:
http://www.telerik.com/products/decompiler.aspx
Used this when viewing a library's source code when it is not available. It generates clear output IMO.
If you want just one implementation, you can study the math library libmath
of the basic calculator bc
, the gnu version can be read at http://code.metager.de/source/xref/gnu/bc/1.06/bc/libmath.b
It normalizes the argument wrt. pi and then uses the Taylor series of sin.
According to Rahul Tripathi, if the C# Sin is the C Sin, it uses a 13 degree polynomial. http://www.netlib.org/fdlibm/k_sin.c
Algorithm
/* __kernel_sin( x, y, iy)
* kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
* Input x is assumed to be bounded by ~pi/4 in magnitude.
* Input y is the tail of x.
* Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
*
* Algorithm
* 1. Since sin(-x) = -sin(x), we need only to consider positive x.
* 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
* 3. sin(x) is approximated by a polynomial of degree 13 on
* [0,pi/4]
* 3 13
* sin(x) ~ x + S1*x + ... + S6*x
* where
*
* |sin(x) 2 4 6 8 10 12 | -58
* |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
* | x |
*
* 4. sin(x+y) = sin(x) + sin'(x')*y
* ~ sin(x) + (1-x*x/2)*y
* For better accuracy, let
* 3 2 2 2 2
* r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
* then 3 2
* sin(x) = x + (S1*x + (x *(r-y/2)+y))
*/