#Why is this a comptime value?

1 messages · Page 1 of 1 (latest)

lone anchor
#

I have the following code for a toy project to learn zig.

const std = @import("std");
const ArrayList = std.ArrayList;

pub const Map = struct {
    const Self = @This();

    num_locations: usize,
    distances: ArrayList(f32),

    pub fn init(allocator: std.mem.Allocator, num: usize) !Map {
        return .{
            .num_locations = num,
            .distances = try ArrayList(f32).initCapacity(allocator, num * (num - 1) / 2),
        };
    }

    pub fn deinit(self: Self) void {
        self.distances.deinit();
    }

    pub fn distanceBetween(self: Self, a: usize, b: usize) f32 {
        if (b < a) return self.distanceBetween(b, a);
        if (a == b) return 0.0;
        if (b >= self.num_locations) return std.math.inf(f32);
        if (a == 0) return self.distances.items[b - a];
        return self.distances.items[a * self.num_locations - a * (a + 1) / 2 + b - a];
    }
};

test "distance between" {
    const alloc = std.testing.allocator;
    var map = try Map.init(alloc, 4);
    try map.distances.replaceRange(0, 6, &[_]f32{ 0.1, 0.2, 0.3, 1.2, 1.3, 2.3 });
    var i: usize = 0;
    while (i < 4) : (i += 1) {
        var j: usize = 0;
        while (j < 4) : (j += 1) {
            if (i == j) {
                try std.testing.expectApproxEqRel(0.0, map.distanceBetween(i, j), 1e-9);
            } else {
                try std.testing.expectApproxEqRel( //
                    @as(f32, @floatFromInt(@min(i, j))) + 0.1 * @as(f32, @floatFromInt(@max(i, j))), //
                    map.distanceBetween(i, j), //
                    1e-9 //
                );
            }
        }
    }
}

#

and I get the following error when running zig test src/file.zig

❯ zig test src/small_example.zig
src/small_example.zig:39:75: error: unable to resolve comptime value
                try std.testing.expectApproxEqRel(0.0, map.distanceBetween(i, j), 1e-9);
                                                       ~~~~~~~~~~~~~~~~~~~^~~~~~
src/small_example.zig:39:75: note: argument to parameter with comptime-only type must be comptime-known
referenced by:
    test.distance between: src/small_example.zig:39:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I honestly don't understand the error here. Why is it related to comptime?

#

Sorry for the two messages, I was over the character limit.

desert kraken
#

this is due to how the arguments of expectApproxEqRel are arranged where the second and third arguments take the type of the first

#

since you passed a float literal as the first argument it has the type comptime_float which, as the name implies, is comptime only

#

the compiler tries to turn your runtime value into that comptime only type which isnt possible

#

that function is actually changed on the latest version so afaik it should work how youre doing it but the general workaround is to do try std.testing.expectApproxEqRel(@as(f32, 0.0), map.distanceBetween(i, j), 1e-9);