Skip to content

Instantly share code, notes, and snippets.

@youdie323323
Created June 6, 2025 21:40
Show Gist options
  • Save youdie323323/2909d85cbd79cb5c7c649a62a5a58da8 to your computer and use it in GitHub Desktop.
Save youdie323323/2909d85cbd79cb5c7c649a62a5a58da8 to your computer and use it in GitHub Desktop.
Reading floats in zig
const std = @import("std");
pub fn readFloat16(stream: anytype) !f16 {
var buffer: [2]u8 align(@alignOf(f16)) = undefined;
_ = try stream.readAll(&buffer);
return std.mem.bytesAsValue(f16, &buffer).*;
}
pub fn readFloat32(stream: anytype) !f32 {
var buffer: [4]u8 align(@alignOf(f32)) = undefined;
_ = try stream.readAll(&buffer);
return std.mem.bytesAsValue(f32, &buffer).*;
}
pub fn readFloat64(stream: anytype) !f64 {
var buffer: [8]u8 align(@alignOf(f64)) = undefined;
_ = try stream.readAll(&buffer);
return std.mem.bytesAsValue(f64, &buffer).*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment