| ... | ... | @@ -1190,18 +1190,18 @@ pub fn lossyCast(comptime T: type, value: anytype) T { |
| 1190 | 1190 | .Int => { |
| 1191 | 1191 | switch (@typeInfo(@TypeOf(value))) { |
| 1192 | 1192 | .Int, .ComptimeInt => { |
| 1193 | | if (value > maxInt(T)) { |
| 1193 | if (value >= maxInt(T)) { |
| 1194 | 1194 | return @as(T, maxInt(T)); |
| 1195 | | } else if (value < minInt(T)) { |
| 1195 | } else if (value <= minInt(T)) { |
| 1196 | 1196 | return @as(T, minInt(T)); |
| 1197 | 1197 | } else { |
| 1198 | 1198 | return @intCast(T, value); |
| 1199 | 1199 | } |
| 1200 | 1200 | }, |
| 1201 | 1201 | .Float, .ComptimeFloat => { |
| 1202 | | if (value > maxInt(T)) { |
| 1202 | if (value >= maxInt(T)) { |
| 1203 | 1203 | return @as(T, maxInt(T)); |
| 1204 | | } else if (value < minInt(T)) { |
| 1204 | } else if (value <= minInt(T)) { |
| 1205 | 1205 | return @as(T, minInt(T)); |
| 1206 | 1206 | } else { |
| 1207 | 1207 | return @floatToInt(T, value); |
| ... | ... | @@ -1218,6 +1218,7 @@ test "lossyCast" { |
| 1218 | 1218 | try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767)); |
| 1219 | 1219 | try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0)); |
| 1220 | 1220 | try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200)); |
| 1221 | try testing.expect(lossyCast(u32, @as(f32, maxInt(u32))) == maxInt(u32)); |
| 1221 | 1222 | } |
| 1222 | 1223 | |
| 1223 | 1224 | test "f64_min" { |