diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-01-01 18:08:40 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-01-01 18:08:40 -0500 |
| commit | 5575e2a168c07d2dcc0e58146231e490ef8a898e (patch) | |
| tree | 7cd93a54d6012066a9daf63465c40b342946ecdd /lib/std/math.zig | |
| parent | 7b62d5dfd872de8719cc05c2486f77b261e863e9 (diff) | |
| download | zig-5575e2a168c07d2dcc0e58146231e490ef8a898e.tar.gz zig-5575e2a168c07d2dcc0e58146231e490ef8a898e.zip | |
std.mem.compare: breaking API changes
* `std.mem.Compare` is now `std.math.Order` and the enum tags
renamed to follow new style convention.
* `std.mem.compare` is renamed to `std.mem.order`.
* new function `std.math.order`
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig index a12bf5f4e9..051dc341a2 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -926,9 +926,6 @@ test "minInt and maxInt" { } test "max value type" { - // If the type of maxInt(i32) was i32 then this implicit cast to - // u32 would not work. But since the value is a number literal, - // it works fine. const x: u32 = maxInt(i32); testing.expect(x == 2147483647); } @@ -944,7 +941,32 @@ test "math.mulWide" { testing.expect(mulWide(u8, 100, 100) == 10000); } -/// Not to be confused with `std.mem.Compare`. +/// See also `CompareOperator`. +pub const Order = enum { + /// Less than (`<`) + lt, + + /// Equal (`==`) + eq, + + /// Greater than (`>`) + gt, +}; + +/// Given two numbers, this function returns the order they are with respect to each other. +pub fn order(a: var, b: var) Order { + if (a == b) { + return .eq; + } else if (a < b) { + return .lt; + } else if (a > b) { + return .gt; + } else { + unreachable; + } +} + +/// See also `Order`. pub const CompareOperator = enum { /// Less than (`<`) lt, @@ -979,7 +1001,7 @@ pub fn compare(a: var, op: CompareOperator, b: var) bool { }; } -test "math.lt, et al < <= > >= between signed and unsigned" { +test "compare between signed and unsigned" { testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255))); testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1))); testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255))); |
