aboutsummaryrefslogtreecommitdiff
path: root/std/math/complex
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-06-17 14:40:07 -0400
committerGitHub <noreply@github.com>2018-06-17 14:40:07 -0400
commit431fda414189e012252614c7cf3702b46b305e35 (patch)
tree929ff496c8b03785eb425329318f5b38170d6962 /std/math/complex
parente5956f23ca702b79a3a4b0f0440a2fe88e0231e5 (diff)
parent74ccf56a4b1da78b6cd6b0ac34dd6ded1e15b155 (diff)
downloadzig-431fda414189e012252614c7cf3702b46b305e35.tar.gz
zig-431fda414189e012252614c7cf3702b46b305e35.zip
Merge pull request #1123 from ziglang/remove-number-casting-syntax
Remove number casting syntax
Diffstat (limited to 'std/math/complex')
-rw-r--r--std/math/complex/atan.zig10
-rw-r--r--std/math/complex/cosh.zig4
-rw-r--r--std/math/complex/exp.zig6
-rw-r--r--std/math/complex/index.zig14
-rw-r--r--std/math/complex/ldexp.zig13
-rw-r--r--std/math/complex/sinh.zig10
-rw-r--r--std/math/complex/sqrt.zig19
-rw-r--r--std/math/complex/tanh.zig10
8 files changed, 47 insertions, 39 deletions
diff --git a/std/math/complex/atan.zig b/std/math/complex/atan.zig
index 9bfe5fe724..de60f2546d 100644
--- a/std/math/complex/atan.zig
+++ b/std/math/complex/atan.zig
@@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
-pub fn atan(z: var) Complex(@typeOf(z.re)) {
+pub fn atan(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => atan32(z),
@@ -25,11 +25,11 @@ fn redupif32(x: f32) f32 {
t -= 0.5;
}
- const u = f32(i32(t));
+ const u = @intToFloat(f32, @floatToInt(i32, t));
return ((x - u * DP1) - u * DP2) - t * DP3;
}
-fn atan32(z: *const Complex(f32)) Complex(f32) {
+fn atan32(z: Complex(f32)) Complex(f32) {
const maxnum = 1.0e38;
const x = z.re;
@@ -74,11 +74,11 @@ fn redupif64(x: f64) f64 {
t -= 0.5;
}
- const u = f64(i64(t));
+ const u = @intToFloat(f64, @floatToInt(i64, t));
return ((x - u * DP1) - u * DP2) - t * DP3;
}
-fn atan64(z: *const Complex(f64)) Complex(f64) {
+fn atan64(z: Complex(f64)) Complex(f64) {
const maxnum = 1.0e308;
const x = z.re;
diff --git a/std/math/complex/cosh.zig b/std/math/complex/cosh.zig
index c2f9a47b8d..a2e31631ea 100644
--- a/std/math/complex/cosh.zig
+++ b/std/math/complex/cosh.zig
@@ -83,12 +83,12 @@ fn cosh64(z: *const Complex(f64)) Complex(f64) {
const y = z.im;
const fx = @bitCast(u64, x);
- const hx = u32(fx >> 32);
+ const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;
const fy = @bitCast(u64, y);
- const hy = u32(fy >> 32);
+ const hy = @intCast(u32, fy >> 32);
const ly = @truncate(u32, fy);
const iy = hy & 0x7fffffff;
diff --git a/std/math/complex/exp.zig b/std/math/complex/exp.zig
index 44c354f246..48fb132d97 100644
--- a/std/math/complex/exp.zig
+++ b/std/math/complex/exp.zig
@@ -6,7 +6,7 @@ const Complex = cmath.Complex;
const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
-pub fn exp(z: var) Complex(@typeOf(z.re)) {
+pub fn exp(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
@@ -16,7 +16,7 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
};
}
-fn exp32(z: *const Complex(f32)) Complex(f32) {
+fn exp32(z: Complex(f32)) Complex(f32) {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
@@ -63,7 +63,7 @@ fn exp32(z: *const Complex(f32)) Complex(f32) {
}
}
-fn exp64(z: *const Complex(f64)) Complex(f64) {
+fn exp64(z: Complex(f64)) Complex(f64) {
const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
diff --git a/std/math/complex/index.zig b/std/math/complex/index.zig
index b00296beda..63a2616984 100644
--- a/std/math/complex/index.zig
+++ b/std/math/complex/index.zig
@@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
};
}
- pub fn add(self: *const Self, other: *const Self) Self {
+ pub fn add(self: Self, other: Self) Self {
return Self{
.re = self.re + other.re,
.im = self.im + other.im,
};
}
- pub fn sub(self: *const Self, other: *const Self) Self {
+ pub fn sub(self: Self, other: Self) Self {
return Self{
.re = self.re - other.re,
.im = self.im - other.im,
};
}
- pub fn mul(self: *const Self, other: *const Self) Self {
+ pub fn mul(self: Self, other: Self) Self {
return Self{
.re = self.re * other.re - self.im * other.im,
.im = self.im * other.re + self.re * other.im,
};
}
- pub fn div(self: *const Self, other: *const Self) Self {
+ pub fn div(self: Self, other: Self) Self {
const re_num = self.re * other.re + self.im * other.im;
const im_num = self.im * other.re - self.re * other.im;
const den = other.re * other.re + other.im * other.im;
@@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
};
}
- pub fn conjugate(self: *const Self) Self {
+ pub fn conjugate(self: Self) Self {
return Self{
.re = self.re,
.im = -self.im,
};
}
- pub fn reciprocal(self: *const Self) Self {
+ pub fn reciprocal(self: Self) Self {
const m = self.re * self.re + self.im * self.im;
return Self{
.re = self.re / m,
@@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
};
}
- pub fn magnitude(self: *const Self) T {
+ pub fn magnitude(self: Self) T {
return math.sqrt(self.re * self.re + self.im * self.im);
}
};
diff --git a/std/math/complex/ldexp.zig b/std/math/complex/ldexp.zig
index a56c2ef2eb..e919ef6bec 100644
--- a/std/math/complex/ldexp.zig
+++ b/std/math/complex/ldexp.zig
@@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
-pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
+pub fn ldexp_cexp(z: var, expt: i32) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
@@ -20,11 +20,12 @@ fn frexp_exp32(x: f32, expt: *i32) f32 {
const exp_x = math.exp(x - kln2);
const hx = @bitCast(u32, exp_x);
- expt.* = i32(hx >> 23) - (0x7f + 127) + k;
+ // TODO zig should allow this cast implicitly because it should know the value is in range
+ expt.* = @intCast(i32, hx >> 23) - (0x7f + 127) + k;
return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
}
-fn ldexp_cexp32(z: *const Complex(f32), expt: i32) Complex(f32) {
+fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp32(z.re, &ex_expt);
const exptf = expt + ex_expt;
@@ -45,16 +46,16 @@ fn frexp_exp64(x: f64, expt: *i32) f64 {
const exp_x = math.exp(x - kln2);
const fx = @bitCast(u64, x);
- const hx = u32(fx >> 32);
+ const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
- expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
+ expt.* = @intCast(i32, hx >> 20) - (0x3ff + 1023) + k;
const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
return @bitCast(f64, (u64(high_word) << 32) | lx);
}
-fn ldexp_cexp64(z: *const Complex(f64), expt: i32) Complex(f64) {
+fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp64(z.re, &ex_expt);
const exptf = i64(expt + ex_expt);
diff --git a/std/math/complex/sinh.zig b/std/math/complex/sinh.zig
index 3d196bfd50..ab23c5c74d 100644
--- a/std/math/complex/sinh.zig
+++ b/std/math/complex/sinh.zig
@@ -6,7 +6,7 @@ const Complex = cmath.Complex;
const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
-pub fn sinh(z: var) Complex(@typeOf(z.re)) {
+pub fn sinh(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => sinh32(z),
@@ -15,7 +15,7 @@ pub fn sinh(z: var) Complex(@typeOf(z.re)) {
};
}
-fn sinh32(z: *const Complex(f32)) Complex(f32) {
+fn sinh32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -78,17 +78,17 @@ fn sinh32(z: *const Complex(f32)) Complex(f32) {
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
}
-fn sinh64(z: *const Complex(f64)) Complex(f64) {
+fn sinh64(z: Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
const fx = @bitCast(u64, x);
- const hx = u32(fx >> 32);
+ const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;
const fy = @bitCast(u64, y);
- const hy = u32(fy >> 32);
+ const hy = @intCast(u32, fy >> 32);
const ly = @truncate(u32, fy);
const iy = hy & 0x7fffffff;
diff --git a/std/math/complex/sqrt.zig b/std/math/complex/sqrt.zig
index d4f5a67528..47367816f7 100644
--- a/std/math/complex/sqrt.zig
+++ b/std/math/complex/sqrt.zig
@@ -4,18 +4,17 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
-// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))
-pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
+pub fn sqrt(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => sqrt32(z),
f64 => sqrt64(z),
- else => @compileError("sqrt not implemented for " ++ @typeName(z)),
+ else => @compileError("sqrt not implemented for " ++ @typeName(T)),
};
}
-fn sqrt32(z: *const Complex(f32)) Complex(f32) {
+fn sqrt32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -50,14 +49,20 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {
if (dx >= 0) {
const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
- return Complex(f32).new(f32(t), f32(dy / (2.0 * t)));
+ return Complex(f32).new(
+ @floatCast(f32, t),
+ @floatCast(f32, dy / (2.0 * t)),
+ );
} else {
const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
- return Complex(f32).new(f32(math.fabs(y) / (2.0 * t)), f32(math.copysign(f64, t, y)));
+ return Complex(f32).new(
+ @floatCast(f32, math.fabs(y) / (2.0 * t)),
+ @floatCast(f32, math.copysign(f64, t, y)),
+ );
}
}
-fn sqrt64(z: *const Complex(f64)) Complex(f64) {
+fn sqrt64(z: Complex(f64)) Complex(f64) {
// may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
const threshold = 0x1.a827999fcef32p+1022;
diff --git a/std/math/complex/tanh.zig b/std/math/complex/tanh.zig
index 1d754838a3..e48d438783 100644
--- a/std/math/complex/tanh.zig
+++ b/std/math/complex/tanh.zig
@@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
-pub fn tanh(z: var) Complex(@typeOf(z.re)) {
+pub fn tanh(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => tanh32(z),
@@ -13,7 +13,7 @@ pub fn tanh(z: var) Complex(@typeOf(z.re)) {
};
}
-fn tanh32(z: *const Complex(f32)) Complex(f32) {
+fn tanh32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@@ -51,12 +51,14 @@ fn tanh32(z: *const Complex(f32)) Complex(f32) {
return Complex(f32).new((beta * rho * s) / den, t / den);
}
-fn tanh64(z: *const Complex(f64)) Complex(f64) {
+fn tanh64(z: Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
const fx = @bitCast(u64, x);
- const hx = u32(fx >> 32);
+ // TODO: zig should allow this conversion implicitly because it can notice that the value necessarily
+ // fits in range.
+ const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;