diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-09-03 19:50:47 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-09-03 19:50:47 +0200 |
| commit | 2173abbd1a52f4af808c3356b63f554f3d2c5f76 (patch) | |
| tree | d2783c4f3bdf7d34c5219dea0ba86d464fca195c /src/number.zig | |
| parent | 6c2e06ed20cdea1a67629554d8aa1d5fcda030f9 (diff) | |
Diffstat (limited to 'src/number.zig')
| -rw-r--r-- | src/number.zig | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/number.zig b/src/number.zig new file mode 100644 index 0000000..253dda3 --- /dev/null +++ b/src/number.zig @@ -0,0 +1,156 @@ +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Number = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn add(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Add(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn sub(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Subtract(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn mul(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Multiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn matrixMultiply(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_MatrixMultiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn floorDiv(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_floorDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn div(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_TrueDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn remainder(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Remainder(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn divmod(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Divmod(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn power(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Power(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn negative(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Negative(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn positive(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Positive(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn absolute(self: *Self) !*Self { + return @ptrCast(c.PyNumber_Absolute(@ptrCast(self)) orelse return error.Exception); + } + + pub fn invert(self: *Self) !*Self { + return @ptrCast(c.PyNumber_Invert(@ptrCast(self)) orelse return error.Exception); + } + + pub fn lshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Lshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn rshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Rshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn @"and"(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_And(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn xor(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Xor(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn @"or"(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Or(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceAdd(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceAdd(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceSub(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceSubtract(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceMul(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceMultiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceMatrixMultiply(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceMatrixMultiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceFloorDiv(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlacefloorDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceDiv(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceTrueDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceRemainder(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceRemainder(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceDivmod(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceDivmod(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlacePower(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlacePower(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceLshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceLshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceRshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceRshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceAnd(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceAnd(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceXor(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceXor(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceOr(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceOr(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn toInt(self: *Self) !*zpy.Int { + return @ptrCast(c.PyNumber_Long(@ptrCast(self)) orelse return error.Exception); + } + + pub fn toFloat(self: *Self) !*zpy.Float { + return @ptrCast(c.PyNumber_Float(@ptrCast(self)) orelse return error.Exception); + } + + pub fn toBase(self: *Self, n: usize) !*zpy.Str { + return @ptrCast(c.PyNumber_ToBase(@ptrCast(self), @intCast(n)) orelse return error.Exception); + } + + pub fn as(self: *Self, T: type) T { + return @intCast(c.PyNumber_AsSize_t(@ptrCast(self), null)); + } + + pub fn asObject(self: *Self) !*zpy.Object { + return @ptrCast(self); + } +}; |