Library reference

Raise to a power

Multiply a base by itself the number of times given by the whole-number exponent in these examples.

std.math.pow

Simple example

Multiply two by itself three times.

Start2 · 3
Result8
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{d}\n", .{std.math.pow(f64, @as(f64, 2.0), @as(f64, 3.0))});
}
Output
8
Advertisements

Everyday examples

A tray has five rows of five cakes. How many cakes are on the tray?

Start5 · 2
Result25
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{d}\n", .{std.math.pow(f64, @as(f64, 5.0), @as(f64, 2.0))});
}
Output
25

A display doubles in size each day. Starting with one tile, how many tiles are there after four doublings?

Start2 · 4
Result16
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{d}\n", .{std.math.pow(f64, @as(f64, 2.0), @as(f64, 4.0))});
}
Output
16

A small cube is three blocks wide, high and deep. How many blocks fill it?

Start3 · 3
Result27
lesson.zig
const std = @import("std");

pub fn main() !void {
    std.debug.print("{d}\n", .{std.math.pow(f64, @as(f64, 3.0), @as(f64, 3.0))});
}
Output
27
Run the example
Terminal
zig run lesson.zig
Official documentation