Gavrilo Zmaj Aleksic
Gavrilo Zmaj Aleksic

Reputation: 39

How does Java calculates trigonometry

I am wondering how Java finds sin(x). I looked at the Math.sin and it doesn't really give an answer. Please don't just say it's Taylor Series, I study math so I know it's not that simple. :)

I also checked Strict Math class and since all those JVM have different algorithms, can someone give an example on one?

So far, this is my algorithm based on Taylor series :

if( (i%4) == 0)
            suma = suma + Math.sin(x0) * Math.pow(x-x0, i) / faktorijal(i);
        if( (i%4) == 1)
            suma = suma - Math.cos(x0) * Math.pow(x-x0, i) / faktorijal(i);
        if( (i%4) == 2)
            suma = suma - Math.sin(x0) * Math.pow(x-x0, i) / faktorijal(i);
        if( (i%4) == 3)
            suma = suma + Math.cos(x0) * Math.pow(x-x0, i) / faktorijal(i);

Where x0 is the point around which I am looking for sin(x) and faktorijal is !i;

Upvotes: 1

Views: 834

Answers (3)

user3835277
user3835277

Reputation:

I'm fairly certain that java does use Taylor Series to find trigonometric values. I would imagine the functions/methods would be fairly easy to implement using modulus to move the amount degrees and radians into their respective trigonometric domains. All the rest is just Taylor Series accurate to a certain decimal point.


@Gavrilo, here's some Python 2.7 code I wrote really quick to simulate a Taylor Series sin(x) function:

from math import factorial as f
from math import sin
from math import pi

def taylorSin(n, d):
    # calculates sin(n) using a taylor-
    # polynomial of degree d

    # implement some sort of modulus to shift n in-
    # to the domain of [0, 2*pi] or [-pi, pi]

    return helper(n, d, 0)

def helper(n, d, x):
    # helper function for taylorSin(n, d)
    if d < 1:
        return x
    else:
        if d % 4 == 1:
            return helper(n, d - 2, x + float(n**d)/f(d))
        elif d % 4 == 3:
            return helper(n, d - 2, x + -1*float(n**d)/f(d))
    print 'Something screwed up'
    return None

testval = 2.13
print 'taylorSin',taylorSin(testval, 25)
print 'sin',sin(testval)

The output:

taylorSin 0.847677840134
sin 0.847677840134

Upvotes: 2

Wyzard
Wyzard

Reputation: 34581

It depends on the JVM implementation, but in OpenJDK, Math.sin just calls StrictMath.sin, which is implemented with a native method that calls the sine function provided by the fdlibm library.

Here's the C source code for the fdlibm sine function in OpenJDK 8:

  • s_sin.c (the sin function itself)
  • k_sin.c (the __kernel_sin function used in the implementation of sin)

The latter file has comments that describe the algorithm. The key point seems to be that "sin(x) is approximated by a polynomial of degree 13". It looks like sin transforms the x argument into an equivalent value in the range (-pi/4, +pi/4), and __kernel_sin performs a polynomial approximation that's accurate enough within that range.


Note that although the actual Java bytecode of the Math.sin method just calls StrictMath.sin, the JVM may execute Math.sin calls using other means besides actually calling the method. In particular, it's likely to to translate a Math.sin call into the corresponding native CPU instruction (such as the x86 FSIN). But a call to StrictMath.sin will always use the fdlibm implementation.

Upvotes: 4

Chris Martin
Chris Martin

Reputation: 30756

The implementation details for Math are not specified.

By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods.

In other words, different JVMs can implement Math.sin differently.

But StrictMath is more formalized:

To help ensure portability of Java programs, the definitions of some of the numeric functions in this package require that they produce the same results as certain published algorithms. These algorithms are available from the well-known network library netlib as the package "Freely Distributable Math Library," fdlibm. These algorithms, which are written in the C programming language, are then to be understood as executed with all floating-point operations following the rules of Java floating-point arithmetic.

Upvotes: 0

Related Questions