# `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¶

ceil(x) int

Rounds up.

Parameters:

x (float) – The value to be rounded.

Returns:

Value rounded towards positive infinity.

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)

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)

Gets the absolute value.

Parameters:

x (float) – The value.

Returns:

Absolute value of `x`.

copysign(x, y)

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.

exp(x)

Gets `e` raised to the power of `x`.

Parameters:

x (float) – The exponent.

Returns:

`e` raised to the power of `x`.

pow(x, y)

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`.

log(x)

Gets the natural logarithm.

Parameters:

x (float) – The value.

Returns:

The natural logarithm of `x`.

sqrt(x)

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)

Converts an angle from radians to degrees.

Parameters:

x (float) – Angle in radians.

Returns:

Angle in degrees.

Converts an angle from degrees to radians.

Parameters:

x (float) – Angle in degrees.

Returns:

sin(x)

Gets the sine of an angle.

Parameters:

x (float) – Angle in radians.

Returns:

Sine of `x`.

asin(x)

Applies the inverse sine operation.

Parameters:

x (float) – Opposite / hypotenuse.

Returns:

Arcsine of `x`, in radians.

cos(x)

Gets the cosine of an angle.

Parameters:

x (float) – Angle in radians.

Returns:

Cosine of `x`.

acos(x)

Applies the inverse cosine operation.

Parameters:

x (float) – Adjacent / hypotenuse.

Returns:

Arccosine of `x`, in radians.

tan(x)

Gets the tangent of an angle.

Parameters:

x (float) – Angle in radians.

Returns:

Tangent of `x`.

atan(x)

Applies the inverse tangent operation.

Parameters:

x (float) – Opposite / adjacent.

Returns:

Arctangent of `x`, in radians.

atan2(b, a)

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)

Computes `m * (2 ** p)`.

Parameters:
• m (float) – The value.

• p (float) – The exponent.

Returns:

Result of `m * (2 ** p)`.