typestar

The math module in Python

Exact integer maths, careful float maths, and the constants.

import math

print(math.pi, math.tau)
print(math.isclose(0.1 + 0.2, 0.3), math.isclose(0.1 + 0.2, 0.3, abs_tol=1e-9))
print(math.floor(2.7), math.ceil(2.1), round(2.5), round(3.5))
print(math.gcd(84, 36), math.isqrt(50), math.comb(10, 3))
print(math.hypot(3, 4), math.log(math.e), math.log10(1000))
print(math.inf > 1e308, math.isnan(float("nan")))

How it works

  1. isclose is how you compare floats.
  2. comb and factorial are exact for integers.
  3. hypot, gcd and isqrt save you writing them.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
348
Tokens
140
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 88 seconds.

Type this snippet

Step 1 of 3 in Numbers & text, step 59 of 72 in Language basics.

← Previous Next →