| ... | ... | @@ -245,25 +245,20 @@ pub fn floatExponentBits(comptime T: type) comptime_int { |
| 245 | 245 | /// Given two types, returns the smallest one which is capable of holding the |
| 246 | 246 | /// full range of the minimum value. |
| 247 | 247 | pub fn Min(comptime A: type, comptime B: type) type { |
| 248 | | return switch (@typeInfo(A)) { |
| 248 | switch (@typeInfo(A)) { |
| 249 | 249 | .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; |
| 255 | 253 | } else { |
| 256 | | break :blk B; |
| 254 | return B; |
| 257 | 255 | } |
| 258 | 256 | }, |
| 259 | | .ComptimeInt => A, |
| 260 | | else => @compileError("unsupported type: " ++ @typeName(B)), |
| 257 | else => {}, |
| 261 | 258 | }, |
| 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)); |
| 267 | 262 | } |
| 268 | 263 | |
| 269 | 264 | /// 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)) { |
| 275 | 270 | // scope it is known to fit in the return type. |
| 276 | 271 | switch (@typeInfo(Result)) { |
| 277 | 272 | .Int => return @intCast(Result, x), |
| 278 | | .Float => return @floatCast(Result, x), |
| 279 | 273 | else => return x, |
| 280 | 274 | } |
| 281 | 275 | } else { |
| ... | ... | @@ -283,7 +277,6 @@ pub fn min(x: var, y: var) Min(@typeOf(x), @typeOf(y)) { |
| 283 | 277 | // scope it is known to fit in the return type. |
| 284 | 278 | switch (@typeInfo(Result)) { |
| 285 | 279 | .Int => return @intCast(Result, y), |
| 286 | | .Float => return @floatCast(Result, y), |
| 287 | 280 | else => return y, |
| 288 | 281 | } |
| 289 | 282 | } |
| ... | ... | @@ -302,9 +295,16 @@ test "math.min" { |
| 302 | 295 | var a: f64 = 10.34; |
| 303 | 296 | var b: f32 = 999.12; |
| 304 | 297 | var result = min(a, b); |
| 305 | | testing.expect(@typeOf(result) == f32); |
| 298 | testing.expect(@typeOf(result) == f64); |
| 306 | 299 | testing.expect(result == 10.34); |
| 307 | 300 | } |
| 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 | } |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | pub fn max(x: var, y: var) @typeOf(x + y) { |