Compare commits

..

No commits in common. "bbab41008f0b9649012ea1ecc9d4d8385e1309b6" and "b02665fe8978838d291762bf768c554596c00401" have entirely different histories.

2 changed files with 8 additions and 10 deletions

View File

@ -61,9 +61,7 @@ pub fn Quantity(comptime T: type, comptime d: Dimensions, comptime s: Scales) ty
const RhsType = @TypeOf(rhs);
const SelfNorm = Quantity(T, dims, scales.min(RhsType.scales));
const RhsNorm = Quantity(T, RhsType.dims, scales.min(RhsType.scales));
if (comptime Self == SelfNorm and RhsType == RhsNorm)
return .{ .value = self.value * rhs.value };
// Only convert the side(s) that actually need it
const lhs_val = if (comptime Self == SelfNorm) self.value else self.to(SelfNorm).value;
const rhs_val = if (comptime RhsType == RhsNorm) rhs.value else rhs.to(RhsNorm).value;
return .{ .value = lhs_val * rhs_val };

View File

@ -3,7 +3,7 @@ const hlp = @import("helper.zig");
const Dimensions = @import("Dimensions.zig");
const Dimension = @import("Dimensions.zig").Dimension;
pub const UnitScale = enum(isize) {
pub const UnitScale = enum(i32) {
P = 15,
T = 12,
G = 9,
@ -28,7 +28,7 @@ pub const UnitScale = enum(isize) {
// Undefined
_,
pub inline fn str(self: @This()) []const u8 {
pub fn str(self: @This()) []const u8 {
var buf: [16]u8 = undefined;
return switch (self) {
inline .none => "",
@ -38,7 +38,7 @@ pub const UnitScale = enum(isize) {
}
/// Helper to get the actual scaling factor
pub inline fn getFactor(self: @This()) comptime_float {
pub fn getFactor(self: @This()) f64 {
return switch (self) {
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))),
inline else => @floatFromInt(@intFromEnum(self)),
@ -46,10 +46,10 @@ pub const UnitScale = enum(isize) {
}
/// Helper to get the actual scaling factor in i32
pub inline fn getFactorInt(self: @This()) comptime_int {
pub fn getFactorInt(self: @This()) i32 {
return switch (self) {
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => comptime std.math.powi(i32, 10.0, @intFromEnum(self)) catch 0,
inline else => comptime @intFromEnum(self),
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.powi(i32, 10.0, @intFromEnum(self)) catch 0,
inline else => @intFromEnum(self),
};
}
};
@ -89,7 +89,7 @@ pub fn min(comptime s1: Scales, comptime s2: Scales) Scales {
return out;
}
pub inline fn getFactor(comptime s: Scales, comptime d: Dimensions) comptime_float {
pub fn getFactor(comptime s: Scales, comptime d: Dimensions) f64 {
comptime var factor: f64 = 1.0;
inline for (std.enums.values(Dimension)) |dim| {
const power = d.get(dim);