ratfactor / ziglings

Learn the Zig programming language by fixing tiny broken programs.
MIT License
4.29k stars 485 forks source link

Mistake in 094? #371

Closed yuval-herman closed 6 months ago

yuval-herman commented 6 months ago

While going over the exercise I saw the line:

But if we write "765.2 % 360", it won't work, because the standard modulo function works only with integer values.

However as I tested to see if it's true, running the following code:

const std = @import("std");

pub fn main() !void {
    const a: f16 = 11.5;
    const b: u8 = 5;
    std.debug.print("{d}", .{a % b});
}

Produces 1.5 as expected. I also searched for mentions of the modulo operator working on integers only in the official zig repo but found nothing related. Either I am missing something or this might be outdated.

chrboesch commented 6 months ago

In general you are right, but "signed or floating-point operands must be comptime-known and positive." That's what prompted me to choose this example. However, I agree that this should be mentioned in the description. Not to confuse anyone. In general, it has become quite difficult to find simple examples for the use of C, as this is no longer necessary with Zig. Because even for this example I can also use Zig @mod(a, b). Perhaps you have an idea.

chrboesch commented 6 months ago

Explanation revised: https://codeberg.org/ziglings/exercises/pulls/53