umath – Math functions

This MicroPython module is similar to the math module in Python. It is available on these hubs:

Rounding and sign

ceil(x: float)int

Rounds up.

Parameters

x – The value x.

Returns

Value rounded towards positive infinity.

floor(x: float)int

Rounds down.

Parameters

x – The value x.

Returns

Value rounded towards negative infinity.

trunc(x: float)int

Truncates decimals to get the integer part of a value.

This is the same as rounding towards 0.

Parameters

x – The value x.

Returns

Integer part of the value.

fmod(x: float, y: float)float

Gets the remainder of x / y.

Not to be confused with modf().

Parameters
  • x – The numerator.

  • y – The denominator.

Returns

Remainder after division

fabs(x: float)float

Gets the absolute value of x.

Parameters

x – The value.

Returns

Absolute value.

copysign(x: float, y: float)float

Gets x with the sign of y.

Parameters
  • x – Determines the magnitude of the return value.

  • y – Determines the sign of the return value.

Returns

x with the sign of y.

Powers and logarithms

e = 2.718282

The mathematical constant e.

exp(x: float)float

Gets e raised to the power of x.

Parameters

x – The exponent.

Returns

e raised to the power of x.

pow(x: float, y: float)float

Gets x raised to the power of y.

Parameters
  • x – The base number.

  • y – The exponent.

Returns

x raised to the power of y.

log(x: float)float

Gets the natural logarithm of x.

Parameters

x – The value x.

Returns

The natural logarithm of x.

sqrt(x: float)float

Gets the square root of x.

Parameters

x – The value x.

Returns

The square root of x.

Trigonomety

pi = 3.141593

The mathematical constant π.

degrees(x: float)float

Converts an angle x from radians to degrees.

Parameters

x – Angle in radians.

Returns

Angle in degrees.

radians(x: float)float

Converts an angle x from degrees to radians.

Parameters

x – Angle in degrees.

Returns

Angle in radians.

sin(x: float)float

Gets the sine of the given angle x.

Parameters

x – Angle in radians.

Returns

Sine of x.

asin(x: float)float

Applies the inverse sine operation on x.

Parameters

x – Opposite / hypotenuse.

Returns

Arcsine of x, in radians.

cos(x: float)float

Gets the cosine of the given angle x.

Parameters

x – Angle in radians.

Returns

Cosine of x.

acos(x: float)float

Applies the inverse cosine operation on x.

Parameters

x – Adjacent / hypotenuse.

Returns

Arccosine of x, in radians.

tan(x: float)float

Gets the tangent of the given angle x.

Parameters

x – Angle in radians.

Returns

Tangent of x.

atan(x: float)float

Applies the inverse tangent operation on x.

Parameters

x – Opposite / adjacent.

Returns

Arctangent of x, in radians.

atan2(b: float, a: float)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 – Opposite side of the triangle.

  • a – Adjacent side of the triangle.

Returns

Arctangent of b / a, in radians.

Other math functions

isfinite(x: float)bool

Checks if x is finite.

Returns

True if x is finite, else False.

isinfinite(x: float)bool

Checks if x is infinite.

Returns

True if x is infinite, else False.

isnan(x: float)bool

Checks if x is not-a-number.

Returns

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

modf(x: float)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 – The value to be decomposed.

Returns

Tuple of fractional and integral parts.

frexp(x: float)Tuple[float, int]

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

Parameters

x – The value to be decomposed.

Returns

Tuple of m and p.

ldexp(m: float, p: int)float

Computes m * (2 ** p).

Parameters
  • m – The value.

  • p – The exponent.

Returns

Result of m * (2 ** p).