mathtrigonometryxbeemicropython

Xbee3 - Calculation of trigonometric values without math module


I'm using MicroPython on an Xbee3-24 and it has no math module internally. I need to calculate sin, cos and arctan values.

How can I use trigonometric functions without using the math module?


Solution

  • You can use power series to approximate trigonometric functions and inverse trigonometric functions (and most other functions, for that matter).

    For example, you could define a function that approximately calculates sin(x) as:

    def approx_sin(x):
        return x - (pow(x, 3)/6) + (pow(x, 5)/120) - (pow(x, 7)/5040)
    

    That has a maximum error of 0.0752 from the true value in the range -pi <= x <= pi (on desktop Python 3.6). If you need better accuracy, add more terms - and if you need to handle x outside that range, bring it in to that range first.