Jonny
Jonny

Reputation: 1378

Pure Python way of calculating trig functions?

I have a small Python script that I want to convert into a single file executable using pyinstaller.

The script essentially takes an input file, manipulates it and writes an output file.

However, I need to calculate the tangent function in the script, and when I do this using either numpy.tan() or math.tan(), the final executable ends up being ridiculously huge because pyinstaller bundles either the whole numpy or math modules in the executable.

Thus, my question is, is there a pure python method to calculate trig functions?

My first though was that it must be possible to define sin, cos and tan purely mathematically, but I could not find a way to do this.

Upvotes: 3

Views: 1117

Answers (2)

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 2914

It's possible to express trigonometric functions with complex numbers: Eulers formula

As this would however require you to perform complex math, and you would have to import cmath, you would need to implement complex math on your own if you want to go this way.

Otherwise, a simple approximation can be acquired by evaluating a taylor series. Again, as import math is not an option, this requires you to implement some pretty fundamental stuff like powers and factorials on your own.

Upvotes: 3

Chris
Chris

Reputation: 967

http://www.efunda.com/math/taylor_series/trig.cfm

the taylor series expansion is a usual numerical method to solve to arbitary precision.

By the way, you should take care of the cyclic property of the trig functions as the error is proportional to the input (well to some power of it), if you do not expect mostly well behaved usage.

Upvotes: 3

Related Questions