umath – Math functions

This MicroPython module is similar to the math module in Python.

See also the built-in math functions that can be used without importing anything.

Rounding and sign

../_images/pybricks_blockMathOp_roundup.svg

ceil(x) int

Rounds up.

Parameters:

x (float) – The value to be rounded.

Returns:

Value rounded towards positive infinity.

../_images/pybricks_blockMathOp_rounddown.svg

floor(x) int

Rounds down.

Parameters:

x (float) – The value to be rounded.

Returns:

Value rounded towards negative infinity.

trunc(x) int

Truncates decimals to get the integer part of a value.

This is the same as rounding towards 0.

Parameters:

x (float) – The value to be truncated.

Returns:

Integer part of the value.

fmod(x, y) float

Gets the remainder of x / y.

Not to be confused with modf().

Parameters:
  • x (float) – The numerator.

  • y (float) – The denominator.

Returns:

Remainder after division

fabs(x) float

Gets the absolute value.

Parameters:

x (float) – The value.

Returns:

Absolute value of x.

copysign(x, y) float

Gets x with the sign of y.

Parameters:
  • x (float) – Determines the magnitude of the return value.

  • y (float) – Determines the sign of the return value.

Returns:

x with the sign of y.

Powers and logarithms

e = 2.718282

The mathematical constant e.

../_images/pybricks_blockMathOp_exp.svg

exp(x) float

Gets e raised to the power of x.

Parameters:

x (float) – The exponent.

Returns:

e raised to the power of x.

../_images/pybricks_blockMathArithmetic_power.svg

../_images/pybricks_blockMathOp_pow10.svg

pow(x, y) float

Gets x raised to the power of y.

Parameters:
  • x (float) – The base number.

  • y (float) – The exponent.

Returns:

x raised to the power of y.

../_images/pybricks_blockMathOp_ln.svg

log(x) float

Gets the natural logarithm.

Parameters:

x (float) – The value.

Returns:

The natural logarithm of x.

../_images/pybricks_blockMathOp_root.svg

sqrt(x) float

Gets the square root.

Parameters:

x (float) – The value x.

Returns:

The square root of x.

Trigonometry

pi = 3.141593

The mathematical constant π.

degrees(x) float

Converts an angle from radians to degrees.

Parameters:

x (float) – Angle in radians.

Returns:

Angle in degrees.

radians(x) float

Converts an angle from degrees to radians.

Parameters:

x (float) – Angle in degrees.

Returns:

Angle in radians.

../_images/pybricks_blockMathOp_sin.svg

sin(x) float

Gets the sine of an angle.

Parameters:

x (float) – Angle in radians.

Returns:

Sine of x.

../_images/pybricks_blockMathOp_asin.svg

asin(x) float

Applies the inverse sine operation.

Parameters:

x (float) – Opposite / hypotenuse.

Returns:

Arcsine of x, in radians.

../_images/pybricks_blockMathOp_cos.svg

cos(x) float

Gets the cosine of an angle.

Parameters:

x (float) – Angle in radians.

Returns:

Cosine of x.

../_images/pybricks_blockMathOp_acos.svg

acos(x) float

Applies the inverse cosine operation.

Parameters:

x (float) – Adjacent / hypotenuse.

Returns:

Arccosine of x, in radians.

../_images/pybricks_blockMathOp_tan.svg

tan(x) float

Gets the tangent of an angle.

Parameters:

x (float) – Angle in radians.

Returns:

Tangent of x.

../_images/pybricks_blockMathOp_atan.svg

atan(x) float

Applies the inverse tangent operation.

Parameters:

x (float) – Opposite / adjacent.

Returns:

Arctangent of x, in radians.

../_images/pybricks_blockMathOp_atan2.svg

atan2(b, a) float

Applies the inverse tangent operation on b / a, and accounts for the signs of b and a to produce the expected angle.

Parameters:
  • b (float) – Opposite side of the triangle.

  • a (float) – Adjacent side of the triangle.

Returns:

Arctangent of b / a, in radians.

Other math functions

isfinite(x) bool

Checks if a value is finite.

Parameters:

x (float) – The value to be checked.

Returns:

True if x is finite, else False.

isinfinite(x) bool

Checks if a value is infinite.

Parameters:

x (float) – The value to be checked.

Returns:

True if x is infinite, else False.

isnan(x) bool

Checks if a value is not-a-number.

Parameters:

x (float) – The value to be checked.

Returns:

True if x is not-a-number, else False.

modf(x) Tuple[float, float]

Gets the fractional and integral parts of x, both with the same sign as x.

Not to be confused with fmod().

Parameters:

x (float) – The value to be decomposed.

Returns:

Tuple of fractional and integral parts.

frexp(x) Tuple[float, float]

Decomposes a value x into a tuple (m, p), such that x == m * (2 ** p).

Parameters:

x (float) – The value to be decomposed.

Returns:

Tuple of m and p.

ldexp(m, p) float

Computes m * (2 ** p).

Parameters:
  • m (float) – The value.

  • p (float) – The exponent.

Returns:

Result of m * (2 ** p).