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?
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.
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)) */
If you want just one implementation, you can study the math library
libmath
of the basic calculatorbc
, the gnu version can be read at http://code.metager.de/source/xref/gnu/bc/1.06/bc/libmath.bIt normalizes the argument wrt. pi and then uses the Taylor series of sin.
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.
And the sin function is declared as
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.
The signature of the method is:
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