| ... | ... | @@ -1214,7 +1214,9 @@ pub fn lossyCast(comptime T: type, value: anytype) T { |
| 1214 | 1214 | } |
| 1215 | 1215 | }, |
| 1216 | 1216 | .Float, .ComptimeFloat => { |
| 1217 | | if (value >= maxInt(T)) { |
| 1217 | if (isNan(value)) { |
| 1218 | return 0; |
| 1219 | } else if (value >= maxInt(T)) { |
| 1218 | 1220 | return @as(T, maxInt(T)); |
| 1219 | 1221 | } else if (value <= minInt(T)) { |
| 1220 | 1222 | return @as(T, minInt(T)); |
| ... | ... | @@ -1234,6 +1236,7 @@ test "lossyCast" { |
| 1234 | 1236 | try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0)); |
| 1235 | 1237 | try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200)); |
| 1236 | 1238 | try testing.expect(lossyCast(u32, @as(f32, maxInt(u32))) == maxInt(u32)); |
| 1239 | try testing.expect(lossyCast(u32, nan(f32)) == 0); |
| 1237 | 1240 | } |
| 1238 | 1241 | |
| 1239 | 1242 | /// Performs linear interpolation between *a* and *b* based on *t*. |