7

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?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
kiss my armpit
  • 3,413
  • 1
  • 27
  • 50
  • The method is declared with `extern`, so it is probably implemented in some external library. – FrustratedWithFormsDesigner Aug 15 '14 at 14:03
  • @FrustratedWithFormsDesigner: Is the source code of the external library available for us? – kiss my armpit Aug 15 '14 at 14:04
  • Do you really mean the source code, or just the prototype? Why do you need it anyway? It will be some very fast unreadable, piece of code that will use processor internal optimizations. To just calculate the sine of an angle in Radians (and not degrees) – Pieter21 Aug 15 '14 at 14:04
  • @Pieter21: Source code, because I want to see how the sine is implemented. – kiss my armpit Aug 15 '14 at 14:05
  • 1
    Indeed interesting subject, but I wouldn't expect to find it in this library. Look for it with Google. Or from this site: http://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions – Pieter21 Aug 15 '14 at 14:10
  • 3
    Pretty much the exact same story as [this answer](http://stackoverflow.com/a/8870593/17034), except that Math.Sin() can be directly translated to a machine code instruction (FSIN) for 32-bit code. – Hans Passant Aug 15 '14 at 14:16

6 Answers6

12

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    How do you know that is "implemented directly in the CLR"? Is there any evidence in the source code of `System.Math`? – Luca Cremonesi Aug 15 '14 at 14:09
  • 3
    Look at this answer from Hans Passant about another function in the `Math` class. http://stackoverflow.com/questions/8870442/how-is-math-pow-implemented-in-net-framework/8870593#8870593. It's about `Math.Exp` but I guess the same will be for all basic mathematical operations. – Patrick Hofman Aug 15 '14 at 14:10
  • Interesting. But can you answer the question though? Where is the source code of System.Math.Sin? – Divan Nov 08 '20 at 14:54
6

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

D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

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)) */

pushStack
  • 3,315
  • 1
  • 15
  • 14
0

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.

Lenny
  • 333
  • 5
  • 19
  • 2
    It has no use, even if Microsoft didn't give the source, how would you find it there? – Patrick Hofman Aug 15 '14 at 14:45
  • The decompiler reconstructs .Net Source code form the binary, you can then review it. It won't give you the original variable names but it's a helpful tool when source is not available and you must see what is going on inside the compiled library. – Lenny Aug 15 '14 at 14:49
  • 1
    But he already has the code in the library. See the link in the question. You can't go further than the .NET code. – Patrick Hofman Aug 15 '14 at 14:49
0

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.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51