Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

How do I calculate sine/cosine/tangent from CORDIC, Taylor Series, or alternative in Python?

I am making a library for math functions, intended to test and challenge my programming and math skills. A necessary part of this is trig: Sine, Cosine, Tangent, and derivatives/inverses, using degrees or radians.

I have spent hours searching the web for implementation of Taylor Series or CORDIC algorithm using degrees AND/OR radians, to no avail.

def sin(x):
    if numerics.degrad == 'deg':
        #compute sin(x) using degrees or radians, make sure to return the result in degrees

Please be informed, numerics.degrad is a string value to be referenced from any function in the library or terminal. Usual values are 'deg' or 'rad'. Also, I am trying to avoid importing libraries like numpy or math.

UPDATE: I tried today(11-12-19) to emulate sine by generating a parabola which intersected one waveform. The hope was to check which waveform the angle would land on, then use the according quadratic equation. That did not work. Results in DESMOS were off by up to 0.2 units, and my python implementation was just plain wrong.

# desmos implementation, where y is the output and x is the angle:
# y=0.0001234567901234(x-90)^2+1
# lays closely over a sine wave graphed with y=sin(x).

# here is my python implementation:

def sin(anglein):    #return the result of sine of the supplied angle, using the mode
    if degrad == 'deg':
        const = 0.0001234567901234
        angle = anglein
        while angle > 360:    #should return the corresponding angle within 0-360 degrees
            angle -= 360

        return((const * ((angle - 90) * (angle - 90))) + 1)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Taylor series for the sin function is straightforward to implement. Note that any reference which gives a Taylor series for a trigonometric function will assume the input is in radians, unless specified otherwise.

PI = 3.141592653589793238462643383

def sin_taylor_series(x, terms=9):
    # map x into the range -PI/2 to PI/2 for greatest accuracy
    x %= 2*PI
    if x > PI:
        x -= 2*PI
        if x < -PI/2:
            x = -PI - x
    elif x > PI/2:
        x = PI - x

    total = 0
    denominator = 1
    for i in range(1, 2*terms+2, 2):
        denominator *= i
        total += (x ** i) / denominator
        denominator *= -(i + 1)
    return total

At 9 terms, the max absolute error (compared with math.sin) is under 10 ** -15, which is about as good as you're going to get by adding a lot of floating point numbers together.

Note that this is not a particularly efficient way of approximating the sin function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...