You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Project 1 of this book, when calculating the size of an encoded or decoded string, ceiled division of usize is done by casting to f64 and calling the @ceil function.
While this approach works, it may lost precision in extreme cases (unlikely though), and floating point arithmetic is generally much more expensive than integer arithmetic. A better approach would be to use std.math.divCeil(usize, input.len, 3) or simply (input.len + 2) / 3. This is also easier to read IMO.
For reference, here's how the same functionality implemented in the standard library.
In Project 1 of this book, when calculating the size of an encoded or decoded string, ceiled division of
usize
is done by casting tof64
and calling the@ceil
function.zig-book/Chapters/01-base64.qmd
Lines 260 to 279 in 5879b29
While this approach works, it may lost precision in extreme cases (unlikely though), and floating point arithmetic is generally much more expensive than integer arithmetic. A better approach would be to use
std.math.divCeil(usize, input.len, 3)
or simply(input.len + 2) / 3
. This is also easier to read IMO.For reference, here's how the same functionality implemented in the standard library.
https://github.com/ziglang/zig/blob/218cf059dd215282aa96d6b4715e68d533a4238e/lib/std/base64.zig#L93-L100
The text was updated successfully, but these errors were encountered: