diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-01-10 19:04:22 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-01-10 19:04:22 -0700 |
| commit | 1295525a7afe76d0ac384b3f74927ac22b27ec09 (patch) | |
| tree | 3609306627c17f83745046cccbde57760088eb4d /lib/std/math.zig | |
| parent | e1d8073d2fc0df6fbc5ce983312e3da374a9889b (diff) | |
| parent | 169810b20fb6ce036ed0ff2990f06751aa11e623 (diff) | |
| download | zig-1295525a7afe76d0ac384b3f74927ac22b27ec09.tar.gz zig-1295525a7afe76d0ac384b3f74927ac22b27ec09.zip | |
Merge branch 'AdamGoertz-master'
closes #5080
closes #6887
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig index 9bc5d75ae2..77eed37304 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1070,16 +1070,52 @@ test "std.math.log2_int_ceil" { testing.expect(log2_int_ceil(u32, 10) == 4); } +///Cast a value to a different type. If the value doesn't fit in, or can't be perfectly represented by, +///the new type, it will be converted to the closest possible representation. pub fn lossyCast(comptime T: type, value: anytype) T { - switch (@typeInfo(@TypeOf(value))) { - .Int => return @intToFloat(T, value), - .Float => return @floatCast(T, value), - .ComptimeInt => return @as(T, value), - .ComptimeFloat => return @as(T, value), - else => @compileError("bad type"), + switch (@typeInfo(T)) { + .Float => { + switch (@typeInfo(@TypeOf(value))) { + .Int => return @intToFloat(T, value), + .Float => return @floatCast(T, value), + .ComptimeInt => return @as(T, value), + .ComptimeFloat => return @as(T, value), + else => @compileError("bad type"), + } + }, + .Int => { + switch (@typeInfo(@TypeOf(value))) { + .Int, .ComptimeInt => { + if (value > maxInt(T)) { + return @as(T, maxInt(T)); + } else if (value < minInt(T)) { + return @as(T, minInt(T)); + } else { + return @intCast(T, value); + } + }, + .Float, .ComptimeFloat => { + if (value > maxInt(T)) { + return @as(T, maxInt(T)); + } else if (value < minInt(T)) { + return @as(T, minInt(T)); + } else { + return @floatToInt(T, value); + } + }, + else => @compileError("bad type"), + } + }, + else => @compileError("bad result type"), } } +test "math.lossyCast" { + testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767)); + testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0)); + testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200)); +} + test "math.f64_min" { const f64_min_u64 = 0x0010000000000000; const fmin: f64 = f64_min; |
