MicroPython Builtin Mathematical Functions

Contents

Introduction

MicroPython is a “slim” version of Python specifically designed with a small footprint to efficiently run on memory constrained microcontrollers.

MicroPython only provides four built-in mathematical functions; min(), max(), pow() and abs(). Never despair though as MicroPython also has a standard library which includes the math module that provides a rich set of computational functions similar to that found in the C language. The MicroPython math module tutorial can be found here.

In addition to these four mathematical functions, MicroPython does have a reasonably comprehensive list of other types of built-in functions. A complete list of all MicroPython built-in functions can be found here.

min()

The min() function returns the item with the lowest value. If the values are strings, an alphabetical comparison is done.

Syntax


min(n1, n2, n3, ...)

Parameters:
    n1, n2, n3, ... : One or more items to compare.

Example
    min(4, 2, 10, 6)  → 2
          

Copy the following code to the Mu Editor, Save and Flash to the micro:bit. The string and list data types are discussed in other postings in this MicroPython series.

Example 1


# Demonstrating the use of min()

x1 = 15
x2 = 20
x3 = 30

# This is a list
L = [1, 2, 6, 4, 3] 

# The following are strings
s1 = 'bat'
s2 = 'cat'
s3 = 'eat'

print('min(x1, x2, x3) =', min(x1, x2, x3))
print('min(L) =', min(L))
print('min(s1, s2, s3) =', min(s1, s2, s3))
          

Output:


min(x1, x2, x3) = 15
min(L) = 1
min(s1, s2, s3) = bat
          

max()

The max() function returns the item with the highest value. If the values are strings, an alphabetically comparison is done.

Syntax


max(n1, n2, n3, ...)

Parameters:
    n1, n2, n3, ... : One or more items to compare.

Example
    max(4, 2, 10, 6)  → 10
          

Try the following code:

Example 2


# Demonstrating the use of max()

x1 = 15
x2 = 20
x3 = 30

# This is a list
L = [1, 2, 6, 4, 3] 

# The following are strings
s1 = 'bat'
s2 = 'cat'
s3 = 'eat'

print('max(x1, x2, x3) =', max(x1, x2, x3))
print('max(L) =', max(L))
print('max(s1, s2, s3) =', max(s1, s2, s3))
          

Output:


max(x1, x2, x3) = 30
max(L) = 6
max(s1, s2, s3) = eat
        

pow()

The pow() function returns the value of x to the power of y (i.e. xy). An alternative to this function is the ** operator.

Syntax


pow(x, y)

Parameters:
    Returns xy

Example
    pow(2, 5)  → 2*2*2*2*2 = 32
          

The following code illustrates pow() usage:

Example 3


# Demonstrating the use of pow()

x = pow(5, 3)
y = 5 ** 3
z = pow(64, 0.5)

# Cube of a value
print('pow(5, 3) =', x)
# Alternative to pow()
print('5 ** 3 =', y)

# Square root of a value
print('pow(64, 0.5) =', z)
          

Output:


pow(5, 3) = 125
5 ** 3 = 125
pow(64, 0.5) = 8.0

Code Explanation

x = pow(5, 3)
  = 53
  = 5 x 5 x 5
  = 125

y = pow(64, 0.5)
  = 640.5
  = √(64)
  = 8
          

abs()

The abs() function returns the absolute (positive) value of the specified number.

Syntax


abs(x)

Examples:
    abs(5)  → 5
    abs(-5)  → 5
    abs(-5.3)  → 5.3
          

The following code illustrates abs() usage:

Example 4


# Demonstrating the use of abs()

x = abs(2.4 - 9.7)
y = abs(pow(-12.65, 1/5))

print('abs(2.4 - 9.7) =', x)
print('abs(pow(-12.65, 1/5)) =', y)
          

Output:


abs(2.4 - 9.7) = 7.3
abs(pow(-12.65, 1/5)) = 1.661186