Add RocDec.sub

This commit is contained in:
Jared Ramirez 2021-06-15 19:19:03 -07:00
parent 19d8c41e98
commit dd2ac10ab9
2 changed files with 24 additions and 0 deletions

1
.gitignore vendored
View file

@ -14,6 +14,7 @@ vgcore.*
#editors #editors
.idea/ .idea/
.vscode/ .vscode/
.ignore
#files too big to track in git #files too big to track in git
editor/benches/resources/100000_lines.roc editor/benches/resources/100000_lines.roc

View file

@ -213,6 +213,17 @@ pub const RocDec = struct {
} }
} }
pub fn sub(self: RocDec, other: RocDec) RocDec {
var answer: i128 = undefined;
const overflowed = @subWithOverflow(i128, self.num, other.num, &answer);
if (!overflowed) {
return RocDec{ .num = answer };
} else {
std.debug.panic("TODO runtime exception for overflow!", .{});
}
}
pub fn mul(self: RocDec, other: RocDec) RocDec { pub fn mul(self: RocDec, other: RocDec) RocDec {
const self_i128 = self.num; const self_i128 = self.num;
const other_i128 = other.num; const other_i128 = other.num;
@ -652,6 +663,18 @@ test "add: 1" {
try expectEqual(RocDec{ .num = 1 }, dec.add(.{ .num = 1 })); try expectEqual(RocDec{ .num = 1 }, dec.add(.{ .num = 1 }));
} }
test "sub: 0" {
var dec: RocDec = .{ .num = 1 };
try expectEqual(RocDec{ .num = 1 }, dec.sub(.{ .num = 0 }));
}
test "sub: 1" {
var dec: RocDec = .{ .num = 1 };
try expectEqual(RocDec{ .num = 0 }, dec.sub(.{ .num = 1 }));
}
test "mul: by 0" { test "mul: by 0" {
var dec: RocDec = .{ .num = 0 }; var dec: RocDec = .{ .num = 0 };