MicroPython, a lightweight version of Python designed for microcontrollers, includes a subset of Python’s math module.
The math module provides essential mathematical functions and constants that allow you to perform mathematical calculations efficiently on small devices like ESP8266, ESP32, and others.
In this tutorial, we will cover:
Let’s explore each concept with examples.
1. Overview of the MicroPython math Module
The math module in MicroPython provides a collection of functions to perform various mathematical operations. Since MicroPython is designed for constrained environments, the math module is smaller compared to the full Python version. However, it still includes the most commonly used mathematical functions such as trigonometry, logarithmic functions, power functions, and more.
You can import the math module in MicroPython as follows:
import math
2. Mathematical Constants
The math module includes some commonly used mathematical constants such as pi (π) and e.
Example: Mathematical Constants
import math # Pi constant print("Pi:", math.pi) # Output: 3.141592653589793 # Euler's number (e) print("Euler's number (e):", math.e) # Output: 2.718281828459045
Output:
Pi: 3.141592653589793 Euler's number (e): 2.718281828459045
- math.pi: Represents the mathematical constant pi (π), approximately 3.14159.
- math.e: Represents Euler’s number (e), approximately 2.71828.
3. Basic Mathematical Functions
The math module provides several basic mathematical functions, such as absolute value, floor, ceil, modf, and more.
Example 1: Absolute Value (fabs())
import math x = -4.7 print("Absolute value of -4.7:", math.fabs(x)) # Output: 4.7
Example 2: Floor and Ceiling (floor() and ceil())
import math x = 3.14 print("Floor of 3.14:", math.floor(x)) # Output: 3 print("Ceiling of 3.14:", math.ceil(x)) # Output: 4
Example 3: modf() Function
The modf() function returns the fractional and integer parts of a number as a tuple.
import math x = 3.14 fractional, integer = math.modf(x) print("Fractional part:", fractional) # Output: 0.14 print("Integer part:", integer) # Output: 3.0
4. Trigonometric Functions
MicroPython’s math module provides several trigonometric functions to perform calculations involving angles.
Example: Trigonometric Functions
import math # Angle in radians angle = math.pi / 4 # 45 degrees # Trigonometric functions print("sin(45°):", math.sin(angle)) # Output: 0.7071067811865475 print("cos(45°):", math.cos(angle)) # Output: 0.7071067811865476 print("tan(45°):", math.tan(angle)) # Output: 1.0
Output:
sin(45°): 0.7071067811865475 cos(45°): 0.7071067811865476 tan(45°): 1.0
- math.sin(): Returns the sine of an angle (in radians).
- math.cos(): Returns the cosine of an angle (in radians).
- math.tan(): Returns the tangent of an angle (in radians).
Example: Converting Between Degrees and Radians
You can use math.radians() to convert degrees to radians and math.degrees() to convert radians to degrees.
import math # Convert 90 degrees to radians radians = math.radians(90) print("90 degrees in radians:", radians) # Output: 1.5707963267948966 # Convert pi/2 radians to degrees degrees = math.degrees(math.pi / 2) print("Pi/2 radians in degrees:", degrees) # Output: 90.0
5. Hyperbolic Functions
MicroPython supports hyperbolic functions such as sinh, cosh, and tanh, which are analogous to trigonometric functions but for hyperbolic geometry.
Example: Hyperbolic Functions
import math x = 1 print("sinh(1):", math.sinh(x)) # Output: 1.1752011936438014 print("cosh(1):", math.cosh(x)) # Output: 1.5430806348152437 print("tanh(1):", math.tanh(x)) # Output: 0.7615941559557649
Output:
sinh(1): 1.1752011936438014 cosh(1): 1.5430806348152437 tanh(1): 0.7615941559557649
- math.sinh(): Returns the hyperbolic sine of x.
- math.cosh(): Returns the hyperbolic cosine of x.
- math.tanh(): Returns the hyperbolic tangent of x.
6. Exponential and Logarithmic Functions
MicroPython’s math module provides functions for exponential calculations and logarithms.
Example 1: Exponentiation (exp())
The exp() function returns e raised to the power of x.
import math x = 1 print("exp(1):", math.exp(x)) # Output: 2.718281828459045
Example 2: Logarithmic Functions
MicroPython supports natural logarithms (log()) and logarithms with base 10 (log10()).
import math # Natural logarithm (log base e) print("log(2):", math.log(2)) # Output: 0.6931471805599453 # Logarithm base 10 print("log10(100):", math.log10(100)) # Output: 2.0
Output:
log(2): 0.6931471805599453 log10(100): 2.0
- math.exp(x): Returns e^x.
- math.log(x): Returns the natural logarithm (log base e) of x.
- math.log10(x): Returns the base-10 logarithm of x.
7. Power and Square Root Functions
The math module provides functions for calculating powers and square roots.
Example 1: Power Function (pow())
The pow() function returns x raised to the power y.
import math x = 2 y = 3 print(f"{x} raised to the power {y}:", math.pow(x, y)) # Output: 8.0
Example 2: Square Root (sqrt())
The sqrt() function returns the square root of a number.
import math x = 16 print(f"Square root of {x}:", math.sqrt(x)) # Output: 4.0
Output:
Square root of 16: 4.0
8. Examples and Use Cases
Example 1: Calculating the Hypotenuse of a Triangle
You can use the math.hypot() function to calculate the hypotenuse of a right triangle using the Pythagorean theorem.
import math # Sides of the triangle a = 3 b = 4 # Hypotenuse calculation hypotenuse = math.hypot(a, b) print(f"Hypotenuse: {hypotenuse}") # Output: 5.0
Output:
Hypotenuse: 5.0
- math.hypot(a, b): Returns the length of the hypotenuse of a right triangle with sides a and b.
Example 2: Calculating Compound Interest
You can use exponential and logarithmic functions to calculate compound interest.
import math # Parameters for compound interest calculation principal = 1000 # Initial investment rate = 5 / 100 # Annual interest rate (5%) time = 10 # Time in years n = 12 # Compounded monthly # Compound interest formula: A = P * (1 + r/n)^(nt) amount = principal * math.pow((1 + rate / n), n * time) print(f"Future value after {time} years: {amount:.2f}")
Output:
Future value after 10 years: 1647.01
Summary of MicroPython math Module Functions:
Function | Description |
---|---|
math.pi | Mathematical constant π (pi). |
math.e | Euler’s number (e). |
math.fabs(x) | Absolute value of x. |
math.floor(x) | Floor of x (largest integer ≤ x). |
math.ceil(x) | Ceiling of x (smallest integer ≥ x). |
math.sin(x) | Sine of x (in radians). |
math.cos(x) | Cosine of x (in radians). |
math.tan(x) | Tangent of x (in radians). |
math.exp(x) | Exponential function e^x. |
math.log(x) | Natural logarithm (log base e). |
math.log10(x) | Logarithm base 10. |
math.pow(x, y) | x raised to the power of y. |
math.sqrt(x) | Square root of x. |
math.radians(x) | Convert degrees to radians. |
math.degrees(x) | Convert radians to degrees. |
math.hypot(x, y) | Hypotenuse of a right triangle with sides x and y. |
Conclusion
The MicroPython math module provides a variety of essential mathematical functions for performing calculations on microcontrollers.
In this tutorial, we covered:
- Mathematical constants like pi and e.
- Basic mathematical functions such as fabs(), floor(), and ceil().
- Trigonometric and hyperbolic functions.
- Exponential, logarithmic, and power functions.
- Real-world examples like calculating the hypotenuse and compound interest.