Skip to content

Passing f32 signalling NaN to a function converts to quiet NaN #10449

@LewisGaul

Description

@LewisGaul

Zig Version

0.10.0-dev.63+6b8e33d14

Steps to Reproduce

const std = @import("std");
const expectEqual = std.testing.expectEqual;
const print = std.debug.print;

const snan_u32 = @as(u32, 0x7F800123);
const snan_f32 = @bitCast(f32, snan_u32);

fn bitcast(x: f32, u: u32) u32 {
    print("{}\n", .{x});
    print("0x{X:0>8}\n", .{u});
    print("0x{X:0>8}\n", .{@bitCast(u32, x)}); // What?!
    return @bitCast(u32, x);
}

test {
    // This is all as expected...
    print("0x{X:0>8}\n", .{snan_u32});
    print("0x{X:0>8}\n", .{@bitCast(u32, snan_f32)});
    try expectEqual(snan_u32, @bitCast(u32, snan_f32));
    print("-----\n", .{});
    // Passing into a function converts to a quiet NaN...
    const u = bitcast(snan_f32, snan_u32);
    try expectEqual(snan_u32, u); // Fails
}

Expected Behavior

Signalling NaN should not be changed when passed into a function.

Actual Behavior

32-bit signalling NaN gets converted to quiet NaN when passed into a function (high bit in the mantissa gets set).

This makes it impossible to correctly implement math.isSignalNan(), as I attempted to do in #10432!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions