18. math module
The math module provides some basic mathematical functions for working with floating-point numbers.
math.degrees() and math.radians() to convert.18.1. Functions
- math.acos(x)
Return the inverse cosine of
x.
from microbit import *
import math
display.scroll(round(math.degrees(math.acos(0.5)), 1), delay=80) # 60.0
- math.asin(x)
Return the inverse sine of
x.
from microbit import *
import math
display.scroll(round(math.degrees(math.asin(0.5)), 1), delay=80) # 30.0
- math.atan(x)
Return the inverse tangent of
x.
from microbit import *
import math
display.scroll(round(math.degrees(math.atan(1)), 1), delay=80) # 45.0
- math.atan2(y, x)
Return the principal value of the inverse tangent of
y/x.
atan, atan2 uses the signs of y and x to give the angle in the correct quarter of the circle.from microbit import *
import math
display.scroll(round(math.degrees(math.atan2(1, 1)), 1), delay=80) # 45.0
display.scroll(round(math.degrees(math.atan2(1, -1)), 1), delay=80) # 135.0
- math.ceil(x)
Return an integer, being
xrounded towards positive infinity.
from microbit import *
import math
display.scroll(math.ceil(2.1), delay=80) # 3
display.scroll(math.ceil(-2.1), delay=80) # -2
- math.copysign(x, y)
Return
xwith the sign ofy.
from microbit import *
import math
display.scroll(math.copysign(5, -1), delay=80) # -5.0
display.scroll(math.copysign(-5, 1), delay=80) # 5.0
- math.cos(x)
Return the cosine of
x.
from microbit import *
import math
display.scroll(round(math.cos(math.radians(60)), 2), delay=80) # 0.5
- math.degrees(x)
Return radians
xconverted to degrees.
from microbit import *
import math
display.scroll(round(math.degrees(math.pi), 1), delay=80) # 180.0
- math.exp(x)
Return the exponential of
x.
exp(1) is the number e.from microbit import *
import math
display.scroll(round(math.exp(1), 3), delay=80) # 2.718
display.scroll(round(math.exp(2), 2), delay=80) # 7.39
- math.fabs(x)
Return the absolute value of
x.
abs(), fabs always returns a float.from microbit import *
import math
display.scroll(math.fabs(-3), delay=80) # 3.0
display.scroll(abs(-3), delay=80) # 3
- math.floor(x)
Return an integer, being
xrounded towards negative infinity.
from microbit import *
import math
display.scroll(math.floor(2.7), delay=80) # 2
display.scroll(math.floor(-2.7), delay=80) # -3
- math.fmod(x, y)
Return the remainder of
x/y.
fmod gives a different answer to the % operator.from microbit import *
import math
display.scroll(math.fmod(7, 3), delay=80) # 1.0
display.scroll(math.fmod(-7, 3), delay=80) # -1.0
display.scroll(-7 % 3, delay=80) # 2
- math.frexp(x)
Decomposes a floating-point number into its mantissa and exponent. The returned value is the tuple
(m, e)such thatx == m * 2**eexactly. Ifx == 0then the function returns(0.0, 0), otherwise the relation0.5 <= abs(m) < 1holds.
from microbit import *
import math
display.scroll(str(math.frexp(8)), delay=80) # (0.5, 4)
- math.isfinite(x)
Return
Trueifxis finite.
from microbit import *
import math
display.scroll(str(math.isfinite(1.5)), delay=80) # True
display.scroll(str(math.isfinite(float('inf'))), delay=80) # False
- math.isinf(x)
Return
Trueifxis infinite.
from microbit import *
import math
display.scroll(str(math.isinf(float('inf'))), delay=80) # True
display.scroll(str(math.isinf(1e30)), delay=80) # False
- math.isnan(x)
Return
Trueifxis not-a-number
from microbit import *
import math
display.scroll(str(math.isnan(float('nan'))), delay=80) # True
display.scroll(str(math.isnan(0.0)), delay=80) # False
- math.ldexp(x, exp)
Return
x * (2**exp).
ldexp is the reverse of frexp.from microbit import *
import math
display.scroll(math.ldexp(0.5, 4), delay=80) # 8.0
- math.log(x)
- math.log(x, base)
- With one argument, return the natural logarithm of x.With two arguments, return the logarithm of x to the given base.
from microbit import *
import math
display.scroll(round(math.log(math.e), 2), delay=80) # 1.0
display.scroll(round(math.log(100, 10), 2), delay=80) # 2.0
display.scroll(round(math.log(8, 2), 2), delay=80) # 3.0
- math.modf(x)
Return a tuple of two floats, being the fractional and integral parts of
x. Both return values have the same sign asx.
from microbit import *
import math
display.scroll(str(math.modf(3.75)), delay=80) # (0.75, 3.0)
- math.pow(x, y)
Returns
xto the power ofy.
** operator, math.pow always returns a float.from microbit import *
import math
display.scroll(math.pow(2, 10), delay=80) # 1024.0
display.scroll(2 ** 10, delay=80) # 1024
- math.radians(x)
Return degrees
xconverted to radians.
from microbit import *
import math
display.scroll(round(math.radians(180), 4), delay=80) # 3.1416
- math.sin(x)
Return the sine of
x.
from microbit import *
import math
display.scroll(round(math.sin(math.radians(30)), 2), delay=80) # 0.5
- math.sqrt(x)
Return the square root of
x.
from microbit import *
import math
display.scroll(math.sqrt(16), delay=80) # 4.0
display.scroll(round(math.sqrt(2), 3), delay=80) # 1.414
- math.tan(x)
Return the tangent of
x.
from microbit import *
import math
display.scroll(round(math.tan(math.radians(45)), 2), delay=80) # 1.0
- math.trunc(x)
Return an integer, being
xrounded towards 0.
trunc rounds towards 0, unlike floor.from microbit import *
import math
display.scroll(math.trunc(-2.7), delay=80) # -2
display.scroll(math.floor(-2.7), delay=80) # -3
18.2. Constants
- math.e
base of the natural logarithm
from microbit import *
import math
display.scroll(round(math.e, 4), delay=80) # 2.7183
- math.pi
the ratio of a circle’s circumference to its diameter
from microbit import *
import math
radius = 3
area = math.pi * radius ** 2
display.scroll(round(area, 2), delay=80) # 28.27
18.3. Tilt strength using sqrt
from microbit import *
import math
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
strength = math.sqrt(x**2 + y**2 + z**2)
display.scroll(int(strength))
sleep(500)
18.4. Tilt angle using atan2
atan2 gives the angle of tilt from the x and z accelerometer values.degrees converts the angle from radians to degrees.from microbit import *
import math
while True:
x = accelerometer.get_x()
z = accelerometer.get_z()
angle = math.degrees(math.atan2(x, -z))
display.scroll(round(angle))
sleep(500)
18.5. Rounding with floor and ceil
from microbit import *
import math
# print(math.floor(2.7)) # 2
display.scroll(str(math.floor(2.7)), delay=80)
# print(math.ceil(2.1)) # 3
display.scroll(str(math.ceil(2.1)), delay=80)
# print(math.trunc(-2.7)) # -2
display.scroll(str(math.trunc(-2.7)), delay=80)
# print(math.pi) # 3.141593
display.scroll(str(math.pi), delay=80)