Skip to content

compiler_rt: fmax and fmin do not distinguish 0.0 and -0.0 #24214

@papparapa

Description

@papparapa

Zig Version

0.15.0-dev.833+5f7780c53

Steps to Reproduce and Observed Behavior

When the arguments are 0.0 and -0.0, fmax and fmin do not care about the sign.

const std = @import("std");

fn compare_max() void {}

fn compare_min() void {}

test compare_max {
    try std.testing.expect(@as(u64, @bitCast(@max(@as(f64, 0.0), @as(f64, -0.0)))) == @as(u64, @bitCast(@as(f64, 0.0)))); // This passes
    try std.testing.expect(@as(u64, @bitCast(@max(@as(f64, -0.0), @as(f64, 0.0)))) == @as(u64, @bitCast(@as(f64, 0.0)))); // This fails
}

test compare_min {
    try std.testing.expect(@as(u64, @bitCast(@min(@as(f64, 0.0), @as(f64, -0.0)))) == @as(u64, @bitCast(@as(f64, -0.0)))); // This passes
    try std.testing.expect(@as(u64, @bitCast(@min(@as(f64, -0.0), @as(f64, 0.0)))) == @as(u64, @bitCast(@as(f64, -0.0)))); // This fails
}
$ zig test zero_comparison_test.zig 
1/2 zero_comparison_test.decltest.compare_max...FAIL (TestUnexpectedResult)
/workspaces/Zig/zig/lib/std/testing.zig:586:14: 0x1033019 in expect (std.zig)
    if (!ok) return error.TestUnexpectedResult;
             ^
/workspaces/Zig/mytest/zero_comparison_test.zig:9:5: 0x10330b3 in decltest.compare_max (zero_comparison_test.zig)
    try std.testing.expect(@as(u64, @bitCast(@max(@as(f64, -0.0), @as(f64, 0.0)))) == @as(u64, @bitCast(@as(f64, 0.0)))); // This fails
    ^
2/2 zero_comparison_test.decltest.compare_min...FAIL (TestUnexpectedResult)
/workspaces/Zig/zig/lib/std/testing.zig:586:14: 0x1033019 in expect (std.zig)
    if (!ok) return error.TestUnexpectedResult;
             ^
/workspaces/Zig/mytest/zero_comparison_test.zig:14:5: 0x1033264 in decltest.compare_min (zero_comparison_test.zig)
    try std.testing.expect(@as(u64, @bitCast(@min(@as(f64, -0.0), @as(f64, 0.0)))) == @as(u64, @bitCast(@as(f64, -0.0)))); // This fails
    ^
0 passed; 0 skipped; 2 failed.
error: the following test command failed with exit code 1:

ISO/IEC 9899:1999(E) states that

Ideally, fmax would be sensitive to the sign of zero, for example fmax(−0.0, +0.0) would return +0; however, implementation in software might be impractical.

Actually musl's fmax and fmin behave ideally.

Expected Behavior

I expect these tests pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcompiler-rt

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions