authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-09 10:22:02-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-09 10:22:02-04:00
log0bf6796b76501efef486815946ce12932d6f6a21
treeef773f6f656298e3fb93c848f04bd5fa7a600507
parente6ef00233e77758fb0ae33a9ad52ed9011005e45
signaturelock-open Commit is signed but in an unrecognized format.

fix regression in std.math.min

closes #3035

1 files changed, 17 insertions(+), 17 deletions(-)

std/math.zig+17-17
......@@ -245,25 +245,20 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
245245/// Given two types, returns the smallest one which is capable of holding the
246246/// full range of the minimum value.
247247pub fn Min(comptime A: type, comptime B: type) type {
248 return switch (@typeInfo(A)) {
248 switch (@typeInfo(A)) {
249249 .Int => |a_info| switch (@typeInfo(B)) {
250 .Int => |b_info| blk: {
251 if (a_info.is_signed == b_info.is_signed) {
252 break :blk if (a_info.bits < b_info.bits) A else B;
253 } else if (a_info.is_signed) {
254 break :blk A;
250 .Int => |b_info| if (!a_info.is_signed and !b_info.is_signed) {
251 if (a_info.bits < b_info.bits) {
252 return A;
255253 } else {
256 break :blk B;
254 return B;
257255 }
258256 },
259 .ComptimeInt => A,
260 else => @compileError("unsupported type: " ++ @typeName(B)),
257 else => {},
261258 },
262 .Float => |a_info| if (a_info.bits < @typeInfo(B).Float.bits) A else B,
263 .ComptimeInt => B,
264 .ComptimeFloat => B,
265 else => @compileError("unsupported type: " ++ @typeName(A)),
266 };
259 else => {},
260 }
261 return @typeOf(A(0) + B(0));
267262}
268263
269264/// Returns the smaller number. When one of the parameter's type's full range fits in the other,
......@@ -275,7 +270,6 @@ pub fn min(x: var, y: var) Min(@typeOf(x), @typeOf(y)) {
275270 // scope it is known to fit in the return type.
276271 switch (@typeInfo(Result)) {
277272 .Int => return @intCast(Result, x),
278 .Float => return @floatCast(Result, x),
279273 else => return x,
280274 }
281275 } else {
......@@ -283,7 +277,6 @@ pub fn min(x: var, y: var) Min(@typeOf(x), @typeOf(y)) {
283277 // scope it is known to fit in the return type.
284278 switch (@typeInfo(Result)) {
285279 .Int => return @intCast(Result, y),
286 .Float => return @floatCast(Result, y),
287280 else => return y,
288281 }
289282 }
......@@ -302,9 +295,16 @@ test "math.min" {
302295 var a: f64 = 10.34;
303296 var b: f32 = 999.12;
304297 var result = min(a, b);
305 testing.expect(@typeOf(result) == f32);
298 testing.expect(@typeOf(result) == f64);
306299 testing.expect(result == 10.34);
307300 }
301 {
302 var a: i8 = -127;
303 var b: i16 = -200;
304 var result = min(a, b);
305 testing.expect(@typeOf(result) == i16);
306 testing.expect(result == -200);
307 }
308308}
309309
310310pub fn max(x: var, y: var) @typeOf(x + y) {