Add support for Num.abs to Dec type

This commit is contained in:
Sekerez 2023-09-24 23:10:25 -04:00
parent 8f210241ab
commit fa65d89a3c
4 changed files with 56 additions and 47 deletions

View file

@ -235,6 +235,11 @@ pub const RocDec = extern struct {
return if (negated) |n| .{ .num = n } else null;
}
pub fn abs(self: RocDec) !RocDec {
const absolute = try math.absInt(self.num);
return RocDec{ .num = absolute };
}
pub fn addWithOverflow(self: RocDec, other: RocDec) WithOverflow(RocDec) {
var answer: i128 = undefined;
const overflowed = @addWithOverflow(i128, self.num, other.num, &answer);
@ -1244,6 +1249,11 @@ pub fn negateC(arg: RocDec) callconv(.C) i128 {
return if (@call(.{ .modifier = always_inline }, RocDec.negate, .{arg})) |dec| dec.num else @panic("TODO overflow for negating RocDec");
}
pub fn absC(arg: RocDec) callconv(.C) i128 {
var result = @call(.{ .modifier = always_inline }, RocDec.abs, .{arg}) catch @panic("TODO overflow for calling absolute value on RocDec");
return result.num;
}
pub fn addC(arg1: RocDec, arg2: RocDec) callconv(.C) WithOverflow(RocDec) {
return @call(.{ .modifier = always_inline }, RocDec.addWithOverflow, .{ arg1, arg2 });
}

View file

@ -18,40 +18,37 @@ const STR = "str";
const dec = @import("dec.zig");
comptime {
exportDecFn(dec.fromStr, "from_str");
exportDecFn(dec.toStr, "to_str");
exportDecFn(dec.fromU64C, "from_u64");
exportDecFn(dec.toI128, "to_i128");
exportDecFn(dec.toF64, "to_f64");
exportDecFn(dec.eqC, "eq");
exportDecFn(dec.neqC, "neq");
exportDecFn(dec.negateC, "negate");
exportDecFn(dec.divC, "div");
exportDecFn(dec.sinC, "sin");
exportDecFn(dec.cosC, "cos");
exportDecFn(dec.tanC, "tan");
exportDecFn(dec.asinC, "asin");
exportDecFn(dec.absC, "abs");
exportDecFn(dec.acosC, "acos");
exportDecFn(dec.atanC, "atan");
exportDecFn(dec.addC, "add_with_overflow");
exportDecFn(dec.addOrPanicC, "add_or_panic");
exportDecFn(dec.addSaturatedC, "add_saturated");
exportDecFn(dec.subC, "sub_with_overflow");
exportDecFn(dec.subOrPanicC, "sub_or_panic");
exportDecFn(dec.subSaturatedC, "sub_saturated");
exportDecFn(dec.asinC, "asin");
exportDecFn(dec.atanC, "atan");
exportDecFn(dec.cosC, "cos");
exportDecFn(dec.divC, "div");
exportDecFn(dec.eqC, "eq");
exportDecFn(dec.fromF32C, "from_float.f32");
exportDecFn(dec.fromF64C, "from_float.f64");
exportDecFn(dec.fromStr, "from_str");
exportDecFn(dec.fromU64C, "from_u64");
exportDecFn(dec.mulC, "mul_with_overflow");
exportDecFn(dec.mulOrPanicC, "mul_or_panic");
exportDecFn(dec.mulSaturatedC, "mul_saturated");
exportDecFn(dec.negateC, "negate");
exportDecFn(dec.neqC, "neq");
exportDecFn(dec.sinC, "sin");
exportDecFn(dec.subC, "sub_with_overflow");
exportDecFn(dec.subOrPanicC, "sub_or_panic");
exportDecFn(dec.subSaturatedC, "sub_saturated");
exportDecFn(dec.tanC, "tan");
exportDecFn(dec.toF64, "to_f64");
exportDecFn(dec.toI128, "to_i128");
exportDecFn(dec.toStr, "to_str");
inline for (INTEGERS) |T| {
dec.exportFromInt(T, ROC_BUILTINS ++ ".dec.from_int.");
}
exportDecFn(dec.fromF32C, "from_float.f32");
exportDecFn(dec.fromF64C, "from_float.f64");
}
// List Module