diff options
| author | LemonBoy <thatlemon@gmail.com> | 2021-06-12 15:35:52 +0200 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2021-06-12 19:56:08 +0300 |
| commit | 44cdafd9d46bed36f08effb99aa0bb7fc2145efa (patch) | |
| tree | a4c988e36af2aac626644b1c080d652e943bbdc1 /lib/std/math/complex/ldexp.zig | |
| parent | 0d022eb1d6333e6e027d35e9588b5a10035664b7 (diff) | |
| download | zig-44cdafd9d46bed36f08effb99aa0bb7fc2145efa.tar.gz zig-44cdafd9d46bed36f08effb99aa0bb7fc2145efa.zip | |
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 #9047
Diffstat (limited to 'lib/std/math/complex/ldexp.zig')
| -rw-r--r-- | lib/std/math/complex/ldexp.zig | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/std/math/complex/ldexp.zig b/lib/std/math/complex/ldexp.zig index 9f31aae549..51ac105256 100644 --- a/lib/std/math/complex/ldexp.zig +++ b/lib/std/math/complex/ldexp.zig @@ -13,6 +13,7 @@ const std = @import("../../std.zig"); const debug = std.debug; const math = std.math; const cmath = math.complex; +const testing = std.testing; const Complex = cmath.Complex; /// Returns exp(z) scaled to avoid overflow. @@ -48,7 +49,10 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { const half_expt2 = exptf - half_expt1; const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); - return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2); + return Complex(f32).init( + math.cos(z.im) * exp_x * scale1 * scale2, + math.sin(z.im) * exp_x * scale1 * scale2, + ); } fn frexp_exp64(x: f64, expt: *i32) f64 { @@ -57,7 +61,7 @@ fn frexp_exp64(x: f64, expt: *i32) f64 { const exp_x = math.exp(x - kln2); - const fx = @bitCast(u64, x); + const fx = @bitCast(u64, exp_x); const hx = @intCast(u32, fx >> 32); const lx = @truncate(u32, fx); @@ -73,10 +77,10 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { const exptf = @as(i64, expt + ex_expt); const half_expt1 = @divTrunc(exptf, 2); - const scale1 = @bitCast(f64, (0x3ff + half_expt1) << 20); + const scale1 = @bitCast(f64, (0x3ff + half_expt1) << (20 + 32)); const half_expt2 = exptf - half_expt1; - const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20); + const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32)); return Complex(f64).init( math.cos(z.im) * exp_x * scale1 * scale2, |
