parent
e6ef00233e
commit
0bf6796b76
34
std/math.zig
34
std/math.zig
|
@ -245,25 +245,20 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
|
||||||
/// Given two types, returns the smallest one which is capable of holding the
|
/// Given two types, returns the smallest one which is capable of holding the
|
||||||
/// full range of the minimum value.
|
/// full range of the minimum value.
|
||||||
pub fn Min(comptime A: type, comptime B: type) type {
|
pub fn Min(comptime A: type, comptime B: type) type {
|
||||||
return switch (@typeInfo(A)) {
|
switch (@typeInfo(A)) {
|
||||||
.Int => |a_info| switch (@typeInfo(B)) {
|
.Int => |a_info| switch (@typeInfo(B)) {
|
||||||
.Int => |b_info| blk: {
|
.Int => |b_info| if (!a_info.is_signed and !b_info.is_signed) {
|
||||||
if (a_info.is_signed == b_info.is_signed) {
|
if (a_info.bits < b_info.bits) {
|
||||||
break :blk if (a_info.bits < b_info.bits) A else B;
|
return A;
|
||||||
} else if (a_info.is_signed) {
|
|
||||||
break :blk A;
|
|
||||||
} else {
|
} else {
|
||||||
break :blk B;
|
return B;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.ComptimeInt => A,
|
else => {},
|
||||||
else => @compileError("unsupported type: " ++ @typeName(B)),
|
|
||||||
},
|
},
|
||||||
.Float => |a_info| if (a_info.bits < @typeInfo(B).Float.bits) A else B,
|
else => {},
|
||||||
.ComptimeInt => B,
|
}
|
||||||
.ComptimeFloat => B,
|
return @typeOf(A(0) + B(0));
|
||||||
else => @compileError("unsupported type: " ++ @typeName(A)),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the smaller number. When one of the parameter's type's full range fits in the other,
|
/// 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)) {
|
||||||
// scope it is known to fit in the return type.
|
// scope it is known to fit in the return type.
|
||||||
switch (@typeInfo(Result)) {
|
switch (@typeInfo(Result)) {
|
||||||
.Int => return @intCast(Result, x),
|
.Int => return @intCast(Result, x),
|
||||||
.Float => return @floatCast(Result, x),
|
|
||||||
else => return x,
|
else => return x,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -283,7 +277,6 @@ pub fn min(x: var, y: var) Min(@typeOf(x), @typeOf(y)) {
|
||||||
// scope it is known to fit in the return type.
|
// scope it is known to fit in the return type.
|
||||||
switch (@typeInfo(Result)) {
|
switch (@typeInfo(Result)) {
|
||||||
.Int => return @intCast(Result, y),
|
.Int => return @intCast(Result, y),
|
||||||
.Float => return @floatCast(Result, y),
|
|
||||||
else => return y,
|
else => return y,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -302,9 +295,16 @@ test "math.min" {
|
||||||
var a: f64 = 10.34;
|
var a: f64 = 10.34;
|
||||||
var b: f32 = 999.12;
|
var b: f32 = 999.12;
|
||||||
var result = min(a, b);
|
var result = min(a, b);
|
||||||
testing.expect(@typeOf(result) == f32);
|
testing.expect(@typeOf(result) == f64);
|
||||||
testing.expect(result == 10.34);
|
testing.expect(result == 10.34);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
var a: i8 = -127;
|
||||||
|
var b: i16 = -200;
|
||||||
|
var result = min(a, b);
|
||||||
|
testing.expect(@typeOf(result) == i16);
|
||||||
|
testing.expect(result == -200);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn max(x: var, y: var) @typeOf(x + y) {
|
pub fn max(x: var, y: var) @typeOf(x + y) {
|
||||||
|
|
Loading…
Reference in New Issue