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
sqrtand the constantpiare imported by name.sinandcostake radians.- That same
sqrtaccepts a four-lane vector and returns four roots.
Keywords and builtins used here
DTypeSIMDdefmainprintvar
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.
Step 1 of 5 in Numbers & time, step 12 of 22 in Parameters & SIMD.