Compare commits

...

3 Commits

Author SHA1 Message Date
Jay Petacat
7903b1a937
Merge f2b43f9549 into 87863a834b 2024-11-26 15:11:19 +01:00
Chris Boesch
87863a834b
std.math.complex: Add squared magnitude function (#21998)
Some checks are pending
ci / x86_64-linux-debug (push) Waiting to run
ci / x86_64-linux-release (push) Waiting to run
ci / aarch64-linux-debug (push) Waiting to run
ci / aarch64-linux-release (push) Waiting to run
ci / x86_64-macos-release (push) Waiting to run
ci / aarch64-macos-debug (push) Waiting to run
ci / aarch64-macos-release (push) Waiting to run
ci / x86_64-windows-debug (push) Waiting to run
ci / x86_64-windows-release (push) Waiting to run
ci / aarch64-windows (push) Waiting to run
2024-11-26 13:03:48 +00:00
Jay Petacat
f2b43f9549 std.Build: Detect pkg-config names with "lib" prefix 2024-11-25 00:01:36 -07:00
2 changed files with 18 additions and 5 deletions

View File

@ -695,6 +695,7 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
// -lSDL2 -> pkg-config sdl2
// -lgdk-3 -> pkg-config gdk-3.0
// -latk-1.0 -> pkg-config atk
// -lpulse -> pkg-config libpulse
const pkgs = try getPkgConfigList(b);
// Exact match means instant winner.
@ -711,13 +712,14 @@ fn runPkgConfig(compile: *Compile, lib_name: []const u8) !PkgConfigResult {
}
}
// Now try appending ".0".
// Prefixed "lib" or suffixed ".0".
for (pkgs) |pkg| {
if (std.ascii.indexOfIgnoreCase(pkg.name, lib_name)) |pos| {
if (pos != 0) continue;
if (mem.eql(u8, pkg.name[lib_name.len..], ".0")) {
break :match pkg.name;
}
const prefix = pkg.name[0..pos];
const suffix = pkg.name[pos + lib_name.len ..];
if (prefix.len > 0 and !mem.eql(u8, prefix, "lib")) continue;
if (suffix.len > 0 and !mem.eql(u8, suffix, ".0")) continue;
break :match pkg.name;
}
}

View File

@ -115,6 +115,10 @@ pub fn Complex(comptime T: type) type {
pub fn magnitude(self: Self) T {
return @sqrt(self.re * self.re + self.im * self.im);
}
pub fn squaredMagnitude(self: Self) T {
return self.re * self.re + self.im * self.im;
}
};
}
@ -189,6 +193,13 @@ test "magnitude" {
try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
}
test "squaredMagnitude" {
const a = Complex(f32).init(5, 3);
const c = a.squaredMagnitude();
try testing.expect(math.approxEqAbs(f32, c, math.pow(f32, a.magnitude(), 2), epsilon));
}
test {
_ = @import("complex/abs.zig");
_ = @import("complex/acosh.zig");