Skip to content

std.math.pow doesn't work at comptime if argument is between 1 and 2 #4259

@ghost

Description

Calling pow with any input (first argument) between 1.0 and 2.0 returns 1.0.

Works fine when called at runtime.

const std = @import("std");

const a = std.math.pow(f32, -1.1, 1.0);
const b = std.math.pow(f32, -1.0, 1.0);
const c = std.math.pow(f32, -0.1, 1.0);
const d = std.math.pow(f32, 0.1, 1.0);
const e = std.math.pow(f32, 1.0, 1.0);
const f = std.math.pow(f32, 1.1, 1.0);
const g = std.math.pow(f32, 1.5, 1.0);
const h = std.math.pow(f32, 1.8, 1.0);
const i = std.math.pow(f32, 2.0, 1.0);
const j = std.math.pow(f32, 2.5, 1.0);
const k = std.math.pow(f32, 3.0, 1.0);

pub fn main() void {
    std.debug.warn("comptime:\n", .{});
    std.debug.warn("  a = {d}\n", .{ a });
    std.debug.warn("  b = {d}\n", .{ b });
    std.debug.warn("  c = {d}\n", .{ c });
    std.debug.warn("  d = {d}\n", .{ d });
    std.debug.warn("  e = {d}\n", .{ e });
    std.debug.warn("  f = {d}\n", .{ f });
    std.debug.warn("  g = {d}\n", .{ g });
    std.debug.warn("  h = {d}\n", .{ h });
    std.debug.warn("  i = {d}\n", .{ i });
    std.debug.warn("  j = {d}\n", .{ j });
    std.debug.warn("  k = {d}\n", .{ k });
    std.debug.warn("\n", .{});
    std.debug.warn("runtime:\n", .{});
    std.debug.warn("  a = {d}\n", .{ std.math.pow(f32, -1.1, 1.0) });
    std.debug.warn("  b = {d}\n", .{ std.math.pow(f32, -1.0, 1.0) });
    std.debug.warn("  c = {d}\n", .{ std.math.pow(f32, -0.1, 1.0) });
    std.debug.warn("  d = {d}\n", .{ std.math.pow(f32, 0.1, 1.0) });
    std.debug.warn("  e = {d}\n", .{ std.math.pow(f32, 1.0, 1.0) });
    std.debug.warn("  f = {d}\n", .{ std.math.pow(f32, 1.1, 1.0) });
    std.debug.warn("  g = {d}\n", .{ std.math.pow(f32, 1.5, 1.0) });
    std.debug.warn("  h = {d}\n", .{ std.math.pow(f32, 1.8, 1.0) });
    std.debug.warn("  i = {d}\n", .{ std.math.pow(f32, 2.0, 1.0) });
    std.debug.warn("  j = {d}\n", .{ std.math.pow(f32, 2.5, 1.0) });
    std.debug.warn("  k = {d}\n", .{ std.math.pow(f32, 3.0, 1.0) });
    std.debug.warn("\n", .{});
}
comptime:
  a = -1.100000023841858
  b = -1
  c = -0.10000000149011612
  d = 0.10000000149011612
  e = 1
  f = 1 <-- damn
  g = 1 <-- damn
  h = 1 <-- damn
  i = 2
  j = 2.5
  k = 3

runtime:
  a = -1.100000023841858
  b = -1
  c = -0.10000000149011612
  d = 0.10000000149011612
  e = 1
  f = 1.100000023841858
  g = 1.5
  h = 1.7999999523162842
  i = 2
  j = 2.5
  k = 3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.stage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions