Hi Guys.
Sorry to bug you but it would seem 0.15.2 handles f32 differently to f16,f64,f128. I solved my issue by moving to f64. But it would appear that f16 would also work. I tested the same code in 0.12.0 and got the same result. So clearly I am missing something here.
When adding 9.6 and minus 4 I get 5.6 for F16,F64,F128 but 5.6000004 for F32. I would expect them all to be the same but with different levels of precision. The fact F16 gives me the correct answer but F32 does not is perplexing.
Tried it on windows 11 and Linux (output in uploaded main.zig)
// a:f16 + b:f16 = 5.6e0 , 5.6
// a:f32 + b:f32 = 5.6000004e0,5.6000004
// a:f64 + b:f64 = 5.6e0,5.6
// a:f128 + b:f128 = 5.6e0,5.6
Is this a bug or have I stuffed up somewhere? I am guessing the latter but just can’t see it.
9.6 and plus 4 is just fine.
All the best
main.zig (1.1 KB)
const std = @import("std");
const zig_bug = @import("zig_bug");
pub fn main() !void {
const a16:f16 = 9.6;
const b16:f16 = -4;
std.debug.print("a:f16 + b:f16 = {e} , {d} \n",.{a16+b16,a16+b16});
const a32:f32 = 9.6;
const b32:f32 = -4;
std.debug.print("a:f32 + b:f32 = {e},{d} \n",.{a32+b32,a32+b32});
const a64:f64 = 9.6;
const b64:f64 = -4;
std.debug.print("a:f64 + b:f64 = {e},{d} \n",.{a64+b64,a64+b64});
const a128:f128 = 9.6;
const b128:f128 = -4;
std.debug.print("a:f128 + b:f128 = {e},{d} \n",.{a128+b128,a128+b128});
// windows output
//a:f16 + b:f16 = 5.6e0 , 5.6
//a:f32 + b:f32 = 5.6000004e0,5.6000004
//a:f64 + b:f64 = 5.6e0,5.6
//a:f128 + b:f128 = 5.6e0,5.6
//
//zig version
//0.15.2
//winver
//24H2 (OS Bild 26100.7171)
// linux output
// a:f16 + b:f16 = 5.6e0 , 5.6
// a:f32 + b:f32 = 5.6000004e0,5.6000004
// a:f64 + b:f64 = 5.6e0,5.6
// a:f128 + b:f128 = 5.6e0,5.6
// uname -a
// Linux debian 6.10.3-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.10.3-1 (2024-08-04) x86_64 GNU/Linux
//zig version
0.15.2
}