authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-16 21:32:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-16 21:32:53-04:00
log06a26f0965deff3d752da3d448b34872010d80f3
tree0e41e31ae4370f43011ba1eadfe1d2e4fb6493b2
parent751518787ae9772eb063a4911a10bf30b2c2a19c

std.Complex: use better arg passing convention and fix a TODO


2 files changed, 11 insertions(+), 12 deletions(-)

std/math/complex/index.zig+7-7
......@@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
3737 };
3838 }
3939
40 pub fn add(self: *const Self, other: *const Self) Self {
40 pub fn add(self: Self, other: Self) Self {
4141 return Self{
4242 .re = self.re + other.re,
4343 .im = self.im + other.im,
4444 };
4545 }
4646
47 pub fn sub(self: *const Self, other: *const Self) Self {
47 pub fn sub(self: Self, other: Self) Self {
4848 return Self{
4949 .re = self.re - other.re,
5050 .im = self.im - other.im,
5151 };
5252 }
5353
54 pub fn mul(self: *const Self, other: *const Self) Self {
54 pub fn mul(self: Self, other: Self) Self {
5555 return Self{
5656 .re = self.re * other.re - self.im * other.im,
5757 .im = self.im * other.re + self.re * other.im,
5858 };
5959 }
6060
61 pub fn div(self: *const Self, other: *const Self) Self {
61 pub fn div(self: Self, other: Self) Self {
6262 const re_num = self.re * other.re + self.im * other.im;
6363 const im_num = self.im * other.re - self.re * other.im;
6464 const den = other.re * other.re + other.im * other.im;
......@@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
6969 };
7070 }
7171
72 pub fn conjugate(self: *const Self) Self {
72 pub fn conjugate(self: Self) Self {
7373 return Self{
7474 .re = self.re,
7575 .im = -self.im,
7676 };
7777 }
7878
79 pub fn reciprocal(self: *const Self) Self {
79 pub fn reciprocal(self: Self) Self {
8080 const m = self.re * self.re + self.im * self.im;
8181 return Self{
8282 .re = self.re / m,
......@@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
8484 };
8585 }
8686
87 pub fn magnitude(self: *const Self) T {
87 pub fn magnitude(self: Self) T {
8888 return math.sqrt(self.re * self.re + self.im * self.im);
8989 }
9090 };
std/math/complex/sqrt.zig+4-5
......@@ -4,18 +4,17 @@ const math = std.math;
44const cmath = math.complex;
55const Complex = cmath.Complex;
66
7// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))
8pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
7pub fn sqrt(z: var) @typeOf(z) {
98 const T = @typeOf(z.re);
109
1110 return switch (T) {
1211 f32 => sqrt32(z),
1312 f64 => sqrt64(z),
14 else => @compileError("sqrt not implemented for " ++ @typeName(z)),
13 else => @compileError("sqrt not implemented for " ++ @typeName(T)),
1514 };
1615}
1716
18fn sqrt32(z: *const Complex(f32)) Complex(f32) {
17fn sqrt32(z: Complex(f32)) Complex(f32) {
1918 const x = z.re;
2019 const y = z.im;
2120
......@@ -57,7 +56,7 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {
5756 }
5857}
5958
60fn sqrt64(z: *const Complex(f64)) Complex(f64) {
59fn sqrt64(z: Complex(f64)) Complex(f64) {
6160 // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
6261 const threshold = 0x1.a827999fcef32p+1022;
6362