typestar

Math functions in Mojo

std.math covers the usual scalar functions and applies them lanewise to SIMD.

from std.math import sqrt, sin, cos, pi


def main():
    print(sqrt(2.0), pi)
    print(sin(pi / 6.0), cos(pi / 3.0))
    var squares = SIMD[DType.float64, 4](1.0, 4.0, 9.0, 16.0)
    print(sqrt(squares))

How it works

  1. sqrt and the constant pi are imported by name.
  2. sin and cos take radians.
  3. That same sqrt accepts a four-lane vector and returns four roots.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
189
Tokens
62
Three-star pace
80 tpm

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

Type this snippet

Step 1 of 5 in Numbers & time, step 12 of 22 in Parameters & SIMD.

← Previous Next →