Skip to content

Commit fc95ad1

Browse files
committed
std: Fix complex ldexp implementation
Two bugs in the implementation ported from musl made all the complex functions relying on ldexp return incorrect results in some cases. Spotted in ziglang#9047
1 parent 986a712 commit fc95ad1

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

lib/std/math/complex/exp.zig

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,42 @@ fn exp64(z: Complex(f64)) Complex(f64) {
124124
}
125125
}
126126

127-
const epsilon = 0.0001;
128-
129127
test "complex.cexp32" {
130-
const a = Complex(f32).init(5, 3);
131-
const c = exp(a);
128+
const tolerance_f32 = math.sqrt(math.epsilon(f32));
129+
130+
{
131+
const a = Complex(f32).init(5, 3);
132+
const c = exp(a);
133+
134+
try testing.expectApproxEqRel(@as(f32, -1.46927917e+02), c.re, tolerance_f32);
135+
try testing.expectApproxEqRel(@as(f32, 2.0944065e+01), c.im, tolerance_f32);
136+
}
137+
138+
{
139+
const a = Complex(f32).init(88.8, 0x1p-149);
140+
const c = exp(a);
132141

133-
try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon));
134-
try testing.expect(math.approxEqAbs(f32, c.im, 20.944065, epsilon));
142+
try testing.expectApproxEqAbs(math.inf(f32), c.re, tolerance_f32);
143+
try testing.expectApproxEqAbs(@as(f32, 5.15088629e-07), c.im, tolerance_f32);
144+
}
135145
}
136146

137147
test "complex.cexp64" {
138-
const a = Complex(f64).init(5, 3);
139-
const c = exp(a);
148+
const tolerance_f64 = math.sqrt(math.epsilon(f64));
140149

141-
try testing.expect(math.approxEqAbs(f64, c.re, -146.927917, epsilon));
142-
try testing.expect(math.approxEqAbs(f64, c.im, 20.944065, epsilon));
150+
{
151+
const a = Complex(f64).init(5, 3);
152+
const c = exp(a);
153+
154+
try testing.expectApproxEqRel(@as(f64, -1.469279139083189e+02), c.re, tolerance_f64);
155+
try testing.expectApproxEqRel(@as(f64, 2.094406620874596e+01), c.im, tolerance_f64);
156+
}
157+
158+
{
159+
const a = Complex(f64).init(709.8, 0x1p-1074);
160+
const c = exp(a);
161+
162+
try testing.expectApproxEqAbs(math.inf(f64), c.re, tolerance_f64);
163+
try testing.expectApproxEqAbs(@as(f64, 9.036659362159884e-16), c.im, tolerance_f64);
164+
}
143165
}

lib/std/math/complex/ldexp.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const std = @import("../../std.zig");
1313
const debug = std.debug;
1414
const math = std.math;
1515
const cmath = math.complex;
16+
const testing = std.testing;
1617
const Complex = cmath.Complex;
1718

1819
/// Returns exp(z) scaled to avoid overflow.
@@ -48,7 +49,10 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
4849
const half_expt2 = exptf - half_expt1;
4950
const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
5051

51-
return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
52+
return Complex(f32).init(
53+
math.cos(z.im) * exp_x * scale1 * scale2,
54+
math.sin(z.im) * exp_x * scale1 * scale2,
55+
);
5256
}
5357

5458
fn frexp_exp64(x: f64, expt: *i32) f64 {
@@ -57,7 +61,7 @@ fn frexp_exp64(x: f64, expt: *i32) f64 {
5761

5862
const exp_x = math.exp(x - kln2);
5963

60-
const fx = @bitCast(u64, x);
64+
const fx = @bitCast(u64, exp_x);
6165
const hx = @intCast(u32, fx >> 32);
6266
const lx = @truncate(u32, fx);
6367

@@ -73,10 +77,10 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
7377
const exptf = @as(i64, expt + ex_expt);
7478

7579
const half_expt1 = @divTrunc(exptf, 2);
76-
const scale1 = @bitCast(f64, (0x3ff + half_expt1) << 20);
80+
const scale1 = @bitCast(f64, (0x3ff + half_expt1) << (20 + 32));
7781

7882
const half_expt2 = exptf - half_expt1;
79-
const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20);
83+
const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32));
8084

8185
return Complex(f64).init(
8286
math.cos(z.im) * exp_x * scale1 * scale2,

0 commit comments

Comments
 (0)