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

Table 1: MicroPython for MicroBit math 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
(x, y are coordinates of a point)

math.atan(8, 5) ⇒ 1.012197
(for point x = 5, y = 8)

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.

Table 2: MicroPython for MicroBit math Mathematical Functions
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
(number = mantissa * 2exponent)

math.frexp(10) ⇒ (0.625, 4)
i.e. 10 = 0.625 x 24

ldexp(mantissa,
           exponent)

The inverse of math.frexp()
(number = mantissa * 2exponent)

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

Table 3: MicroPython for MicroBit math Rounding, Truncation and Sign Functions
Function Description
floor(x)

Rounds a number down to the nearest integer

math.floor(5.8) ⇒ 5
math.floor(-5.8) ⇒ -6

Alternative: round().

ceil(x)

Rounds a number up to the nearest integer

math.ceil(5.8) ⇒ 6
math.floor(-5.8) ⇒ -5

trunc(x)

Returns the truncated integer part of a number

math.trunc(5.8) ⇒ 5
math.trunc(-5.8) ⇒ -5

Alternative: int()

fabs(x)

Returns the absolute value, always as a float

math.fabs(5.8) ⇒ 5.8
fabs(-5.8) ⇒ 5.8
math.fabs(-5) ⇒ 5.0

Alternative: abs().

copysign(x,y)

Returns value of x with the sign(+/-) of y

math.copysign(5.8, -2.1) ⇒ -5.8
math.copysign(-5.8, 2.1) ⇒ 5.8
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.

Table 4: MicroPython for MicroBit math Boolean Functions
Function Description
isnan(x)

Returns True if a value is NaN[1] (‘Not a Number’)

math.isnan(float("NaN")) ⇒ True
math.isnan(5.2) ⇒ False

isfinite(x)

Returns True if a number is a finite

math.isfinite(1E10 * 1E10) ⇒ True
math.isfinite(1E1000 * 1E1000) ⇒ False (overflow)

isinf(x)

Inverse function to isfinite()

math.isinf(1E1000 * 1E1000) ⇒ True (overflow)