I study Java and became interested in the work of the sin ( cos ) method. I noticed that the speed of obtaining a sine by j varies depending on the argument of the function. If the angle is less than 0.785 ... happy, then it calculates quickly, and if the larger one, respectively, slowly.

Compared with C #, it turned out about the opposite:

enter image description here

Checked in milliseconds for 10 million sine calculations.
Why are the results?


Here is the result for java cosine / sine from 0 to 4 * pi:

enter image description here


 public static void main(String[] args) { long a; for (double x = 0; x < 4 * Math.PI; x = x + 0.05) { a = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { //Math.cos(x); Math.sin(x); } System.out.printf("%.6f, %d \n", x, (System.currentTimeMillis() - a)); } } 

Here is the result for the cosine / sine of C # from 0 to 4 * pi:

enter image description here

 public static void Main(string[] args) { for (double x = 0; x < 4 * Math.PI; x = x + 0.05) { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 10000000; i++) { Math.Cos(x); // Math.Sin(x); } sw.Stop(); Console.WriteLine(x +" - " +(sw.ElapsedMilliseconds).ToString()); } } 
  • You use different ways to measure time, in my opinion the reason may well be this. How exactly does the currentTimeMillis method work in java ? Try to increase the number of calculations by an order of magnitude. Will this picture persist? - Monk
  • sin and cos are opposite in some ways, therefore cos can be fully calculated in C #, and java - sin - hedgehogues
  • one
    Shoot the cosine in the same test, interesting in the light of the @Barmaley answer. - Eugene Krivenja
  • one
    I checked in my C # .Net Framework v4.0.30319, I observe a picture similar to Java - the increasing part. On cosine, exactly opposite indicators are decreasing. - Alex Krass
  • @Monk default time accuracy is one millisecond. Yes, the cartna is saved. - Dmitriy Chistyakov

1 answer 1

In C #, I won’t say for sure, but in Java it looks like this:

  1. The sine is calculated through the library. OpenJDK sources here .
  2. To approximate sine, a 13th degree polynomial (something like Chebyshev) is used.
  3. In this case, for argument values ​​less than PI/4=0.785 , the true sine is calculated.
  4. If the argument greater than PI/4 calculated already through the cosine, which is already approximated by a 14th degree polynomial, from there a jump occurs on the graph.

Most likely, with C # the story is similar, only another library is used.

  • Thank. So far it is not entirely clear, because with cosine on j, the picture is identical to sine. And there should be a different schedule, if I understood correctly. - Dmitriy Chistyakov
  • Here is the result from 0 to 4 * pi per j for sin and cos. lh4.googleusercontent.com/… - Dmitriy Chistyakov
  • Here is C # from 0 to 4 * pi: lh5.googleusercontent.com/… - Dmitriy Chistyakov
  • Honestly, I have little understanding of C / C ++, but maybe this is the math source for the core. - Vladislav Khapin