Created
June 6, 2025 21:40
-
-
Save youdie323323/2909d85cbd79cb5c7c649a62a5a58da8 to your computer and use it in GitHub Desktop.
Reading floats in zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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