Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The math module

Numbers beyond the operators: the math library as it grows.

Bring it in with import math. The floating-point functions (sine, logarithm, cube_root, is_nan, …) are methods of float and double, which every program has without an import; this module holds the constants and the integer functions.

  • Constants: PI, TAU, E, INFINITY, NAN, and the epsilon and limits of float and double
  • Integers: gcd and lcm
  • Parsing: parse_int, deprecated: every number type has a static parse (long.parse(text, base)) without an import

Example

import math;
var radius = 2.0;
var area = math.PI * radius ** 2;
assert((math.PI / 2).sine() == 1.0, "a quarter turn");

Source: runtime/math/module.pep