MicroPython ‘math’ Module
Contents
Introduction
It is assumed in this module that the reader is familiar with the syntax for the import statement which is required to use a MicroPython library. If in doubt, a guide to MicroPython library use can be found here.
MicroPython comes installed with a standard library of modules that simplifies common programming tasks. One such is the math module which provides a rich set of computational functions similar to that found in the C language.
Technically speaking, since MicroPython is an object-oriented programming (OOP) language, the correct term is method however this tutorial will continue with function.
Trigonometry Functions
Function | Description |
---|---|
sin(x) | Returns sine of an angle math.sin(1.0) ⇒ 0.841471 |
asin(x) | Returns arc sine (angle) math.asin(0.841471) ⇒ 1.0 |
cos(x) | Returns cosine of an angle math.cos(1.0) ⇒ 0.5403023 |
acos(x) | Returns arc cosine (angle) math.asin(0.5403023) ⇒ 1.0 |
tan(x) | Returns tangent (angle) math.tan(1) ⇒ 1.557408 |
atan(x) | Returns arc tangent (angle) math.atan(1.557408) ⇒ 1.0 |
atan2(y, x) | Returns the arc tangent of y/x math.atan(8, 5) ⇒ 1.012197 |
degrees(x) | Converts radians to degrees math.degrees(0.7853982) ⇒ 45 |
radians(x) | Converts degrees to radians math.radians(45) ⇒ 0.7853982 |
Trigonometry functions in a microcontroller context are useful on occasions for processing output from some sensor classes.
Beware that these functions work in units of radians. Usually calculations based on sensor data needs to be performed using angle degrees. The function degrees() is provided for this conversion purpose. For example, to calculate the sine of 45° the following would be used:
math.sin(math.degrees(45))
= 0.806087
Mathematical Functions
The following table provides the general mathematical functions that might be found as a subset on a scientific calculator.
Function | Description |
---|---|
exp(x) | Returns 'e' raised to a power math.exp(5) ⇒ 148.4132 |
log(x) | Returns the natural logarithm math.log(148.4132) ⇒ 5.0 |
log(x,y) | Returns the logarithm of x to base y math.log(100,10) ⇒ 2.0 |
pow(x,y) | Returns value of x raised to power y math.pow(5,3) ⇒ 125.0 |
sqrt(x) | Returns the square root math.sqrt(144) ⇒ 12.0 |
fmod(x,y) | Returns the remainder of x/y math.fmod(15,4) ⇒ 3.0 |
frexp(x) | Returns the mantissa and the exponent math.frexp(10) ⇒ (0.625, 4) |
ldexp(mantissa,
exponent) |
The inverse of math.frexp()
math.ldexp(0.625, 4) ⇒ 10.0 |
e | Constant that returns the Eular's number math.e ⇒ 2.718282 |
pi | Constant that returns Pi math.pi ⇒ 3.141593 |
Rounding, Truncation and Sign Functions
Function | Description |
---|---|
floor(x) | Rounds a number down to the nearest integer math.floor(5.8) ⇒ 5
Alternative: round(). |
ceil(x) | Rounds a number up to the nearest integer
math.ceil(5.8) ⇒ 6
|
trunc(x) | Returns the truncated integer part of a number math.trunc(5.8) ⇒ 5
Alternative: int() |
fabs(x) | Returns the absolute value, always as a float math.fabs(5.8) ⇒ 5.8
Alternative: abs(). |
copysign(x,y) | Returns value of x with the sign(+/-) of y math.copysign(5.8, -2.1) ⇒ -5.8
|
Boolean Functions
Boolean functions determine whether a value has a given property and return True or False.
Function | Description |
---|---|
isnan(x) | Returns True if a value is NaN[1] (‘Not a Number’) math.isnan(float("NaN")) ⇒ True
|
isfinite(x) | Returns True if a number is a finite math.isfinite(1E10 * 1E10) ⇒ True |
isinf(x) | Inverse function to isfinite() math.isinf(1E1000 * 1E1000) ⇒ True (overflow) |