diff options
| author | Michael Bradshaw <github@mjb.io> | 2023-10-20 07:20:01 -0600 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2023-10-21 20:51:51 +0300 |
| commit | cc56577edfadd7685de7fb14c013c5e3a69c28ce (patch) | |
| tree | 4bef172448f765f0526324bdd23f6ff1fc98910b /lib/std/math.zig | |
| parent | b403ca0aabb4529949b48e68f1392afc31098a98 (diff) | |
| download | zig-cc56577edfadd7685de7fb14c013c5e3a69c28ce.tar.gz zig-cc56577edfadd7685de7fb14c013c5e3a69c28ce.zip | |
Return zero for NaN-to-int lossy casts
Fixes #15038.
The goal here is to guarantee lossyCast() is panic-free and always safe.
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig index 336007be57..817a98f314 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1214,7 +1214,9 @@ pub fn lossyCast(comptime T: type, value: anytype) T { } }, .Float, .ComptimeFloat => { - if (value >= maxInt(T)) { + if (isNan(value)) { + return 0; + } else if (value >= maxInt(T)) { return @as(T, maxInt(T)); } else if (value <= minInt(T)) { return @as(T, minInt(T)); @@ -1234,6 +1236,7 @@ test "lossyCast" { try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0)); try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200)); try testing.expect(lossyCast(u32, @as(f32, maxInt(u32))) == maxInt(u32)); + try testing.expect(lossyCast(u32, nan(f32)) == 0); } /// Performs linear interpolation between *a* and *b* based on *t*. |
