| author | |
| committer | |
| log | 85a9bd932f5fd77afecf2443a291f82f0f4a6eaa |
| tree | 0431daa515c3bf2b64a4d02e6fcd2c8116c14067 |
| parent | 1d532f12b568c924baead9de78701f66a526e16b |
| parent | 951ab802a38ede9f5329aeae1fed00161321d558 |
| signature |
std.math: simplify signbit and copysign14 files changed, 67 insertions(+), 191 deletions(-)
lib/compiler_rt/fma.zig+2-2| ... | @@ -57,7 +57,7 @@ pub fn fma(x: f64, y: f64, z: f64) callconv(.C) f64 { | ... | @@ -57,7 +57,7 @@ pub fn fma(x: f64, y: f64, z: f64) callconv(.C) f64 { |
| 57 | if (spread <= 53 * 2) { | 57 | if (spread <= 53 * 2) { |
| 58 | zs = math.scalbn(zs, -spread); | 58 | zs = math.scalbn(zs, -spread); |
| 59 | } else { | 59 | } else { |
| 60 | zs = math.copysign(f64, math.floatMin(f64), zs); | 60 | zs = math.copysign(math.floatMin(f64), zs); |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | const xy = dd_mul(xs, ys); | 63 | const xy = dd_mul(xs, ys); |
| ... | @@ -116,7 +116,7 @@ pub fn fmaq(x: f128, y: f128, z: f128) callconv(.C) f128 { | ... | @@ -116,7 +116,7 @@ pub fn fmaq(x: f128, y: f128, z: f128) callconv(.C) f128 { |
| 116 | if (spread <= 113 * 2) { | 116 | if (spread <= 113 * 2) { |
| 117 | zs = math.scalbn(zs, -spread); | 117 | zs = math.scalbn(zs, -spread); |
| 118 | } else { | 118 | } else { |
| 119 | zs = math.copysign(f128, math.floatMin(f128), zs); | 119 | zs = math.copysign(math.floatMin(f128), zs); |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | const xy = dd_mul128(xs, ys); | 122 | const xy = dd_mul128(xs, ys); |
lib/std/math/atanh.zig+2-2| ... | @@ -33,7 +33,7 @@ fn atanh_32(x: f32) f32 { | ... | @@ -33,7 +33,7 @@ fn atanh_32(x: f32) f32 { |
| 33 | var y = @bitCast(f32, i); // |x| | 33 | var y = @bitCast(f32, i); // |x| |
| 34 | 34 | ||
| 35 | if (y == 1.0) { | 35 | if (y == 1.0) { |
| 36 | return math.copysign(f32, math.inf(f32), x); | 36 | return math.copysign(math.inf(f32), x); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | if (u < 0x3F800000 - (1 << 23)) { | 39 | if (u < 0x3F800000 - (1 << 23)) { |
| ... | @@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 { | ... | @@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 { |
| 62 | var y = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x| | 62 | var y = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x| |
| 63 | 63 | ||
| 64 | if (y == 1.0) { | 64 | if (y == 1.0) { |
| 65 | return math.copysign(f64, math.inf(f64), x); | 65 | return math.copysign(math.inf(f64), x); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | if (e < 0x3FF - 1) { | 68 | if (e < 0x3FF - 1) { |
lib/std/math/complex/cosh.zig+10-10| ... | @@ -45,13 +45,13 @@ fn cosh32(z: Complex(f32)) Complex(f32) { | ... | @@ -45,13 +45,13 @@ fn cosh32(z: Complex(f32)) Complex(f32) { |
| 45 | if (ix < 0x42b17218) { | 45 | if (ix < 0x42b17218) { |
| 46 | // x < 88.7: exp(|x|) won't overflow | 46 | // x < 88.7: exp(|x|) won't overflow |
| 47 | const h = @exp(@fabs(x)) * 0.5; | 47 | const h = @exp(@fabs(x)) * 0.5; |
| 48 | return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y)); | 48 | return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y)); |
| 49 | } | 49 | } |
| 50 | // x < 192.7: scale to avoid overflow | 50 | // x < 192.7: scale to avoid overflow |
| 51 | else if (ix < 0x4340b1e7) { | 51 | else if (ix < 0x4340b1e7) { |
| 52 | const v = Complex(f32).init(@fabs(x), y); | 52 | const v = Complex(f32).init(@fabs(x), y); |
| 53 | const r = ldexp_cexp(v, -1); | 53 | const r = ldexp_cexp(v, -1); |
| 54 | return Complex(f32).init(r.re, r.im * math.copysign(f32, 1, x)); | 54 | return Complex(f32).init(r.re, r.im * math.copysign(@as(f32, 1.0), x)); |
| 55 | } | 55 | } |
| 56 | // x >= 192.7: result always overflows | 56 | // x >= 192.7: result always overflows |
| 57 | else { | 57 | else { |
| ... | @@ -61,14 +61,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) { | ... | @@ -61,14 +61,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) { |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | if (ix == 0 and iy >= 0x7f800000) { | 63 | if (ix == 0 and iy >= 0x7f800000) { |
| 64 | return Complex(f32).init(y - y, math.copysign(f32, 0, x * (y - y))); | 64 | return Complex(f32).init(y - y, math.copysign(@as(f32, 0.0), x * (y - y))); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | if (iy == 0 and ix >= 0x7f800000) { | 67 | if (iy == 0 and ix >= 0x7f800000) { |
| 68 | if (hx & 0x7fffff == 0) { | 68 | if (hx & 0x7fffff == 0) { |
| 69 | return Complex(f32).init(x * x, math.copysign(f32, 0, x) * y); | 69 | return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y); |
| 70 | } | 70 | } |
| 71 | return Complex(f32).init(x, math.copysign(f32, 0, (x + x) * y)); | 71 | return Complex(f32).init(x, math.copysign(@as(f32, 0.0), (x + x) * y)); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | if (ix < 0x7f800000 and iy >= 0x7f800000) { | 74 | if (ix < 0x7f800000 and iy >= 0x7f800000) { |
| ... | @@ -113,13 +113,13 @@ fn cosh64(z: Complex(f64)) Complex(f64) { | ... | @@ -113,13 +113,13 @@ fn cosh64(z: Complex(f64)) Complex(f64) { |
| 113 | if (ix < 0x40862e42) { | 113 | if (ix < 0x40862e42) { |
| 114 | // x < 710: exp(|x|) won't overflow | 114 | // x < 710: exp(|x|) won't overflow |
| 115 | const h = @exp(@fabs(x)) * 0.5; | 115 | const h = @exp(@fabs(x)) * 0.5; |
| 116 | return Complex(f64).init(h * @cos(y), math.copysign(f64, h, x) * @sin(y)); | 116 | return Complex(f64).init(h * @cos(y), math.copysign(h, x) * @sin(y)); |
| 117 | } | 117 | } |
| 118 | // x < 1455: scale to avoid overflow | 118 | // x < 1455: scale to avoid overflow |
| 119 | else if (ix < 0x4096bbaa) { | 119 | else if (ix < 0x4096bbaa) { |
| 120 | const v = Complex(f64).init(@fabs(x), y); | 120 | const v = Complex(f64).init(@fabs(x), y); |
| 121 | const r = ldexp_cexp(v, -1); | 121 | const r = ldexp_cexp(v, -1); |
| 122 | return Complex(f64).init(r.re, r.im * math.copysign(f64, 1, x)); | 122 | return Complex(f64).init(r.re, r.im * math.copysign(@as(f64, 1.0), x)); |
| 123 | } | 123 | } |
| 124 | // x >= 1455: result always overflows | 124 | // x >= 1455: result always overflows |
| 125 | else { | 125 | else { |
| ... | @@ -129,14 +129,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) { | ... | @@ -129,14 +129,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) { |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | if (ix | lx == 0 and iy >= 0x7ff00000) { | 131 | if (ix | lx == 0 and iy >= 0x7ff00000) { |
| 132 | return Complex(f64).init(y - y, math.copysign(f64, 0, x * (y - y))); | 132 | return Complex(f64).init(y - y, math.copysign(@as(f64, 0.0), x * (y - y))); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | if (iy | ly == 0 and ix >= 0x7ff00000) { | 135 | if (iy | ly == 0 and ix >= 0x7ff00000) { |
| 136 | if ((hx & 0xfffff) | lx == 0) { | 136 | if ((hx & 0xfffff) | lx == 0) { |
| 137 | return Complex(f64).init(x * x, math.copysign(f64, 0, x) * y); | 137 | return Complex(f64).init(x * x, math.copysign(@as(f64, 0.0), x) * y); |
| 138 | } | 138 | } |
| 139 | return Complex(f64).init(x * x, math.copysign(f64, 0, (x + x) * y)); | 139 | return Complex(f64).init(x * x, math.copysign(@as(f64, 0.0), (x + x) * y)); |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | if (ix < 0x7ff00000 and iy >= 0x7ff00000) { | 142 | if (ix < 0x7ff00000 and iy >= 0x7ff00000) { |
lib/std/math/complex/proj.zig+1-1| ... | @@ -9,7 +9,7 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) { | ... | @@ -9,7 +9,7 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) { |
| 9 | const T = @TypeOf(z.re); | 9 | const T = @TypeOf(z.re); |
| 10 | 10 | ||
| 11 | if (math.isInf(z.re) or math.isInf(z.im)) { | 11 | if (math.isInf(z.re) or math.isInf(z.im)) { |
| 12 | return Complex(T).init(math.inf(T), math.copysign(T, 0, z.re)); | 12 | return Complex(T).init(math.inf(T), math.copysign(@as(T, 0.0), z.re)); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | return Complex(T).init(z.re, z.im); | 15 | return Complex(T).init(z.re, z.im); |
lib/std/math/complex/sinh.zig+8-8| ... | @@ -45,13 +45,13 @@ fn sinh32(z: Complex(f32)) Complex(f32) { | ... | @@ -45,13 +45,13 @@ fn sinh32(z: Complex(f32)) Complex(f32) { |
| 45 | if (ix < 0x42b17218) { | 45 | if (ix < 0x42b17218) { |
| 46 | // x < 88.7: exp(|x|) won't overflow | 46 | // x < 88.7: exp(|x|) won't overflow |
| 47 | const h = @exp(@fabs(x)) * 0.5; | 47 | const h = @exp(@fabs(x)) * 0.5; |
| 48 | return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y)); | 48 | return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y)); |
| 49 | } | 49 | } |
| 50 | // x < 192.7: scale to avoid overflow | 50 | // x < 192.7: scale to avoid overflow |
| 51 | else if (ix < 0x4340b1e7) { | 51 | else if (ix < 0x4340b1e7) { |
| 52 | const v = Complex(f32).init(@fabs(x), y); | 52 | const v = Complex(f32).init(@fabs(x), y); |
| 53 | const r = ldexp_cexp(v, -1); | 53 | const r = ldexp_cexp(v, -1); |
| 54 | return Complex(f32).init(r.re * math.copysign(f32, 1, x), r.im); | 54 | return Complex(f32).init(r.re * math.copysign(@as(f32, 1.0), x), r.im); |
| 55 | } | 55 | } |
| 56 | // x >= 192.7: result always overflows | 56 | // x >= 192.7: result always overflows |
| 57 | else { | 57 | else { |
| ... | @@ -61,14 +61,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) { | ... | @@ -61,14 +61,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) { |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | if (ix == 0 and iy >= 0x7f800000) { | 63 | if (ix == 0 and iy >= 0x7f800000) { |
| 64 | return Complex(f32).init(math.copysign(f32, 0, x * (y - y)), y - y); | 64 | return Complex(f32).init(math.copysign(@as(f32, 0.0), x * (y - y)), y - y); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | if (iy == 0 and ix >= 0x7f800000) { | 67 | if (iy == 0 and ix >= 0x7f800000) { |
| 68 | if (hx & 0x7fffff == 0) { | 68 | if (hx & 0x7fffff == 0) { |
| 69 | return Complex(f32).init(x, y); | 69 | return Complex(f32).init(x, y); |
| 70 | } | 70 | } |
| 71 | return Complex(f32).init(x, math.copysign(f32, 0, y)); | 71 | return Complex(f32).init(x, math.copysign(@as(f32, 0.0), y)); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | if (ix < 0x7f800000 and iy >= 0x7f800000) { | 74 | if (ix < 0x7f800000 and iy >= 0x7f800000) { |
| ... | @@ -112,13 +112,13 @@ fn sinh64(z: Complex(f64)) Complex(f64) { | ... | @@ -112,13 +112,13 @@ fn sinh64(z: Complex(f64)) Complex(f64) { |
| 112 | if (ix < 0x40862e42) { | 112 | if (ix < 0x40862e42) { |
| 113 | // x < 710: exp(|x|) won't overflow | 113 | // x < 710: exp(|x|) won't overflow |
| 114 | const h = @exp(@fabs(x)) * 0.5; | 114 | const h = @exp(@fabs(x)) * 0.5; |
| 115 | return Complex(f64).init(math.copysign(f64, h, x) * @cos(y), h * @sin(y)); | 115 | return Complex(f64).init(math.copysign(h, x) * @cos(y), h * @sin(y)); |
| 116 | } | 116 | } |
| 117 | // x < 1455: scale to avoid overflow | 117 | // x < 1455: scale to avoid overflow |
| 118 | else if (ix < 0x4096bbaa) { | 118 | else if (ix < 0x4096bbaa) { |
| 119 | const v = Complex(f64).init(@fabs(x), y); | 119 | const v = Complex(f64).init(@fabs(x), y); |
| 120 | const r = ldexp_cexp(v, -1); | 120 | const r = ldexp_cexp(v, -1); |
| 121 | return Complex(f64).init(r.re * math.copysign(f64, 1, x), r.im); | 121 | return Complex(f64).init(r.re * math.copysign(@as(f64, 1.0), x), r.im); |
| 122 | } | 122 | } |
| 123 | // x >= 1455: result always overflows | 123 | // x >= 1455: result always overflows |
| 124 | else { | 124 | else { |
| ... | @@ -128,14 +128,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) { | ... | @@ -128,14 +128,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) { |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | if (ix | lx == 0 and iy >= 0x7ff00000) { | 130 | if (ix | lx == 0 and iy >= 0x7ff00000) { |
| 131 | return Complex(f64).init(math.copysign(f64, 0, x * (y - y)), y - y); | 131 | return Complex(f64).init(math.copysign(@as(f64, 0.0), x * (y - y)), y - y); |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | if (iy | ly == 0 and ix >= 0x7ff00000) { | 134 | if (iy | ly == 0 and ix >= 0x7ff00000) { |
| 135 | if ((hx & 0xfffff) | lx == 0) { | 135 | if ((hx & 0xfffff) | lx == 0) { |
| 136 | return Complex(f64).init(x, y); | 136 | return Complex(f64).init(x, y); |
| 137 | } | 137 | } |
| 138 | return Complex(f64).init(x, math.copysign(f64, 0, y)); | 138 | return Complex(f64).init(x, math.copysign(@as(f64, 0.0), y)); |
| 139 | } | 139 | } |
| 140 | 140 | ||
| 141 | if (ix < 0x7ff00000 and iy >= 0x7ff00000) { | 141 | if (ix < 0x7ff00000 and iy >= 0x7ff00000) { |
lib/std/math/complex/sqrt.zig+6-6| ... | @@ -43,9 +43,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { | ... | @@ -43,9 +43,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { |
| 43 | // sqrt(-inf + i nan) = nan +- inf i | 43 | // sqrt(-inf + i nan) = nan +- inf i |
| 44 | // sqrt(-inf + iy) = 0 + inf i | 44 | // sqrt(-inf + iy) = 0 + inf i |
| 45 | if (math.signbit(x)) { | 45 | if (math.signbit(x)) { |
| 46 | return Complex(f32).init(@fabs(x - y), math.copysign(f32, x, y)); | 46 | return Complex(f32).init(@fabs(x - y), math.copysign(x, y)); |
| 47 | } else { | 47 | } else { |
| 48 | return Complex(f32).init(x, math.copysign(f32, y - y, y)); | 48 | return Complex(f32).init(x, math.copysign(y - y, y)); |
| 49 | } | 49 | } |
| 50 | } | 50 | } |
| 51 | 51 | ||
| ... | @@ -65,7 +65,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { | ... | @@ -65,7 +65,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { |
| 65 | const t = @sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5); | 65 | const t = @sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5); |
| 66 | return Complex(f32).init( | 66 | return Complex(f32).init( |
| 67 | @floatCast(f32, @fabs(y) / (2.0 * t)), | 67 | @floatCast(f32, @fabs(y) / (2.0 * t)), |
| 68 | @floatCast(f32, math.copysign(f64, t, y)), | 68 | @floatCast(f32, math.copysign(t, y)), |
| 69 | ); | 69 | ); |
| 70 | } | 70 | } |
| 71 | } | 71 | } |
| ... | @@ -94,9 +94,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { | ... | @@ -94,9 +94,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 94 | // sqrt(-inf + i nan) = nan +- inf i | 94 | // sqrt(-inf + i nan) = nan +- inf i |
| 95 | // sqrt(-inf + iy) = 0 + inf i | 95 | // sqrt(-inf + iy) = 0 + inf i |
| 96 | if (math.signbit(x)) { | 96 | if (math.signbit(x)) { |
| 97 | return Complex(f64).init(@fabs(x - y), math.copysign(f64, x, y)); | 97 | return Complex(f64).init(@fabs(x - y), math.copysign(x, y)); |
| 98 | } else { | 98 | } else { |
| 99 | return Complex(f64).init(x, math.copysign(f64, y - y, y)); | 99 | return Complex(f64).init(x, math.copysign(y - y, y)); |
| 100 | } | 100 | } |
| 101 | } | 101 | } |
| 102 | 102 | ||
| ... | @@ -116,7 +116,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { | ... | @@ -116,7 +116,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 116 | result = Complex(f64).init(t, y / (2.0 * t)); | 116 | result = Complex(f64).init(t, y / (2.0 * t)); |
| 117 | } else { | 117 | } else { |
| 118 | const t = @sqrt((-x + math.hypot(f64, x, y)) * 0.5); | 118 | const t = @sqrt((-x + math.hypot(f64, x, y)) * 0.5); |
| 119 | result = Complex(f64).init(@fabs(y) / (2.0 * t), math.copysign(f64, t, y)); | 119 | result = Complex(f64).init(@fabs(y) / (2.0 * t), math.copysign(t, y)); |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | if (scale) { | 122 | if (scale) { |
lib/std/math/complex/tanh.zig+4-4| ... | @@ -34,7 +34,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { | ... | @@ -34,7 +34,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { |
| 34 | } | 34 | } |
| 35 | const xx = @bitCast(f32, hx - 0x40000000); | 35 | const xx = @bitCast(f32, hx - 0x40000000); |
| 36 | const r = if (math.isInf(y)) y else @sin(y) * @cos(y); | 36 | const r = if (math.isInf(y)) y else @sin(y) * @cos(y); |
| 37 | return Complex(f32).init(xx, math.copysign(f32, 0, r)); | 37 | return Complex(f32).init(xx, math.copysign(@as(f32, 0.0), r)); |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | if (!math.isFinite(y)) { | 40 | if (!math.isFinite(y)) { |
| ... | @@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { | ... | @@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { |
| 45 | // x >= 11 | 45 | // x >= 11 |
| 46 | if (ix >= 0x41300000) { | 46 | if (ix >= 0x41300000) { |
| 47 | const exp_mx = @exp(-@fabs(x)); | 47 | const exp_mx = @exp(-@fabs(x)); |
| 48 | return Complex(f32).init(math.copysign(f32, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx); | 48 | return Complex(f32).init(math.copysign(@as(f32, 1.0), x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | // Kahan's algorithm | 51 | // Kahan's algorithm |
| ... | @@ -77,7 +77,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { | ... | @@ -77,7 +77,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { |
| 77 | 77 | ||
| 78 | const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx); | 78 | const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx); |
| 79 | const r = if (math.isInf(y)) y else @sin(y) * @cos(y); | 79 | const r = if (math.isInf(y)) y else @sin(y) * @cos(y); |
| 80 | return Complex(f64).init(xx, math.copysign(f64, 0, r)); | 80 | return Complex(f64).init(xx, math.copysign(@as(f64, 0.0), r)); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | if (!math.isFinite(y)) { | 83 | if (!math.isFinite(y)) { |
| ... | @@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { | ... | @@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { |
| 88 | // x >= 22 | 88 | // x >= 22 |
| 89 | if (ix >= 0x40360000) { | 89 | if (ix >= 0x40360000) { |
| 90 | const exp_mx = @exp(-@fabs(x)); | 90 | const exp_mx = @exp(-@fabs(x)); |
| 91 | return Complex(f64).init(math.copysign(f64, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx); | 91 | return Complex(f64).init(math.copysign(@as(f64, 1.0), x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx); |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | // Kahan's algorithm | 94 | // Kahan's algorithm |
lib/std/math/copysign.zig+17-84| ... | @@ -1,92 +1,25 @@ | ... | @@ -1,92 +1,25 @@ |
| 1 | // Ported from musl, which is licensed under the MIT license: | ||
| 2 | // https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT | ||
| 3 | // | ||
| 4 | // https://git.musl-libc.org/cgit/musl/tree/src/math/copysignf.c | ||
| 5 | // https://git.musl-libc.org/cgit/musl/tree/src/math/copysign.c | ||
| 6 | |||
| 7 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| 8 | const math = std.math; | 2 | const math = std.math; |
| 9 | const expect = std.testing.expect; | 3 | const expect = std.testing.expect; |
| 10 | const maxInt = std.math.maxInt; | ||
| 11 | |||
| 12 | /// Returns a value with the magnitude of x and the sign of y. | ||
| 13 | pub fn copysign(comptime T: type, x: T, y: T) T { | ||
| 14 | return switch (T) { | ||
| 15 | f16 => copysign16(x, y), | ||
| 16 | f32 => copysign32(x, y), | ||
| 17 | f64 => copysign64(x, y), | ||
| 18 | f128 => copysign128(x, y), | ||
| 19 | else => @compileError("copysign not implemented for " ++ @typeName(T)), | ||
| 20 | }; | ||
| 21 | } | ||
| 22 | 4 | ||
| 23 | fn copysign16(x: f16, y: f16) f16 { | 5 | /// Returns a value with the magnitude of `magnitude` and the sign of `sign`. |
| 24 | const ux = @bitCast(u16, x); | 6 | pub fn copysign(magnitude: anytype, sign: @TypeOf(magnitude)) @TypeOf(magnitude) { |
| 25 | const uy = @bitCast(u16, y); | 7 | const T = @TypeOf(magnitude); |
| 26 | 8 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); | |
| 27 | const h1 = ux & (maxInt(u16) / 2); | 9 | const sign_bit_mask = @as(TBits, 1) << (@bitSizeOf(T) - 1); |
| 28 | const h2 = uy & (@as(u16, 1) << 15); | 10 | const mag = @bitCast(TBits, magnitude) & ~sign_bit_mask; |
| 29 | return @bitCast(f16, h1 | h2); | 11 | const sgn = @bitCast(TBits, sign) & sign_bit_mask; |
| 30 | } | 12 | return @bitCast(T, mag | sgn); |
| 31 | |||
| 32 | fn copysign32(x: f32, y: f32) f32 { | ||
| 33 | const ux = @bitCast(u32, x); | ||
| 34 | const uy = @bitCast(u32, y); | ||
| 35 | |||
| 36 | const h1 = ux & (maxInt(u32) / 2); | ||
| 37 | const h2 = uy & (@as(u32, 1) << 31); | ||
| 38 | return @bitCast(f32, h1 | h2); | ||
| 39 | } | ||
| 40 | |||
| 41 | fn copysign64(x: f64, y: f64) f64 { | ||
| 42 | const ux = @bitCast(u64, x); | ||
| 43 | const uy = @bitCast(u64, y); | ||
| 44 | |||
| 45 | const h1 = ux & (maxInt(u64) / 2); | ||
| 46 | const h2 = uy & (@as(u64, 1) << 63); | ||
| 47 | return @bitCast(f64, h1 | h2); | ||
| 48 | } | ||
| 49 | |||
| 50 | fn copysign128(x: f128, y: f128) f128 { | ||
| 51 | const ux = @bitCast(u128, x); | ||
| 52 | const uy = @bitCast(u128, y); | ||
| 53 | |||
| 54 | const h1 = ux & (maxInt(u128) / 2); | ||
| 55 | const h2 = uy & (@as(u128, 1) << 127); | ||
| 56 | return @bitCast(f128, h1 | h2); | ||
| 57 | } | 13 | } |
| 58 | 14 | ||
| 59 | test "math.copysign" { | 15 | test "math.copysign" { |
| 60 | try expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0)); | 16 | inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| { |
| 61 | try expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0)); | 17 | try expect(copysign(@as(T, 1.0), @as(T, 1.0)) == 1.0); |
| 62 | try expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0)); | 18 | try expect(copysign(@as(T, 2.0), @as(T, -2.0)) == -2.0); |
| 63 | try expect(copysign(f128, 1.0, 1.0) == copysign128(1.0, 1.0)); | 19 | try expect(copysign(@as(T, -3.0), @as(T, 3.0)) == 3.0); |
| 64 | } | 20 | try expect(copysign(@as(T, -4.0), @as(T, -4.0)) == -4.0); |
| 65 | 21 | try expect(copysign(@as(T, 5.0), @as(T, -500.0)) == -5.0); | |
| 66 | test "math.copysign16" { | 22 | try expect(copysign(math.inf(T), @as(T, -0.0)) == -math.inf(T)); |
| 67 | try expect(copysign16(5.0, 1.0) == 5.0); | 23 | try expect(copysign(@as(T, 6.0), -math.nan(T)) == -6.0); |
| 68 | try expect(copysign16(5.0, -1.0) == -5.0); | 24 | } |
| 69 | try expect(copysign16(-5.0, -1.0) == -5.0); | ||
| 70 | try expect(copysign16(-5.0, 1.0) == 5.0); | ||
| 71 | } | ||
| 72 | |||
| 73 | test "math.copysign32" { | ||
| 74 | try expect(copysign32(5.0, 1.0) == 5.0); | ||
| 75 | try expect(copysign32(5.0, -1.0) == -5.0); | ||
| 76 | try expect(copysign32(-5.0, -1.0) == -5.0); | ||
| 77 | try expect(copysign32(-5.0, 1.0) == 5.0); | ||
| 78 | } | ||
| 79 | |||
| 80 | test "math.copysign64" { | ||
| 81 | try expect(copysign64(5.0, 1.0) == 5.0); | ||
| 82 | try expect(copysign64(5.0, -1.0) == -5.0); | ||
| 83 | try expect(copysign64(-5.0, -1.0) == -5.0); | ||
| 84 | try expect(copysign64(-5.0, 1.0) == 5.0); | ||
| 85 | } | ||
| 86 | |||
| 87 | test "math.copysign128" { | ||
| 88 | try expect(copysign128(5.0, 1.0) == 5.0); | ||
| 89 | try expect(copysign128(5.0, -1.0) == -5.0); | ||
| 90 | try expect(copysign128(-5.0, -1.0) == -5.0); | ||
| 91 | try expect(copysign128(-5.0, 1.0) == 5.0); | ||
| 92 | } | 25 | } |
lib/std/math/isfinite.zig+1-4| ... | @@ -5,10 +5,7 @@ const expect = std.testing.expect; | ... | @@ -5,10 +5,7 @@ const expect = std.testing.expect; |
| 5 | /// Returns whether x is a finite value. | 5 | /// Returns whether x is a finite value. |
| 6 | pub fn isFinite(x: anytype) bool { | 6 | pub fn isFinite(x: anytype) bool { |
| 7 | const T = @TypeOf(x); | 7 | const T = @TypeOf(x); |
| 8 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); | 8 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 9 | if (@typeInfo(T) != .Float) { | ||
| 10 | @compileError("isFinite not implemented for " ++ @typeName(T)); | ||
| 11 | } | ||
| 12 | const remove_sign = ~@as(TBits, 0) >> 1; | 9 | const remove_sign = ~@as(TBits, 0) >> 1; |
| 13 | return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T)); | 10 | return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T)); |
| 14 | } | 11 | } |
lib/std/math/isinf.zig+1-4| ... | @@ -5,10 +5,7 @@ const expect = std.testing.expect; | ... | @@ -5,10 +5,7 @@ const expect = std.testing.expect; |
| 5 | /// Returns whether x is an infinity, ignoring sign. | 5 | /// Returns whether x is an infinity, ignoring sign. |
| 6 | pub fn isInf(x: anytype) bool { | 6 | pub fn isInf(x: anytype) bool { |
| 7 | const T = @TypeOf(x); | 7 | const T = @TypeOf(x); |
| 8 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); | 8 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 9 | if (@typeInfo(T) != .Float) { | ||
| 10 | @compileError("isInf not implemented for " ++ @typeName(T)); | ||
| 11 | } | ||
| 12 | const remove_sign = ~@as(TBits, 0) >> 1; | 9 | const remove_sign = ~@as(TBits, 0) >> 1; |
| 13 | return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T)); | 10 | return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T)); |
| 14 | } | 11 | } |
lib/std/math/isnormal.zig+1-4| ... | @@ -5,10 +5,7 @@ const expect = std.testing.expect; | ... | @@ -5,10 +5,7 @@ const expect = std.testing.expect; |
| 5 | /// Returns whether x is neither zero, subnormal, infinity, or NaN. | 5 | /// Returns whether x is neither zero, subnormal, infinity, or NaN. |
| 6 | pub fn isNormal(x: anytype) bool { | 6 | pub fn isNormal(x: anytype) bool { |
| 7 | const T = @TypeOf(x); | 7 | const T = @TypeOf(x); |
| 8 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); | 8 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 9 | if (@typeInfo(T) != .Float) { | ||
| 10 | @compileError("isNormal not implemented for " ++ @typeName(T)); | ||
| 11 | } | ||
| 12 | 9 | ||
| 13 | const increment_exp = 1 << math.floatMantissaBits(T); | 10 | const increment_exp = 1 << math.floatMantissaBits(T); |
| 14 | const remove_sign = ~@as(TBits, 0) >> 1; | 11 | const remove_sign = ~@as(TBits, 0) >> 1; |
lib/std/math/ldexp.zig+1-4| ... | @@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) { | ... | @@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) { |
| 15 | var shift = n; | 15 | var shift = n; |
| 16 | 16 | ||
| 17 | const T = @TypeOf(base); | 17 | const T = @TypeOf(base); |
| 18 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); | 18 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 19 | if (@typeInfo(T) != .Float) { | ||
| 20 | @compileError("ldexp not implemented for " ++ @typeName(T)); | ||
| 21 | } | ||
| 22 | 19 | ||
| 23 | const mantissa_bits = math.floatMantissaBits(T); | 20 | const mantissa_bits = math.floatMantissaBits(T); |
| 24 | const exponent_min = math.floatExponentMin(T); | 21 | const exponent_min = math.floatExponentMin(T); |
lib/std/math/pow.zig+1-1| ... | @@ -60,7 +60,7 @@ pub fn pow(comptime T: type, x: T, y: T) T { | ... | @@ -60,7 +60,7 @@ pub fn pow(comptime T: type, x: T, y: T) T { |
| 60 | if (y < 0) { | 60 | if (y < 0) { |
| 61 | // pow(+-0, y) = +- 0 for y an odd integer | 61 | // pow(+-0, y) = +- 0 for y an odd integer |
| 62 | if (isOddInteger(y)) { | 62 | if (isOddInteger(y)) { |
| 63 | return math.copysign(T, math.inf(T), x); | 63 | return math.copysign(math.inf(T), x); |
| 64 | } | 64 | } |
| 65 | // pow(+-0, y) = +inf for y an even integer | 65 | // pow(+-0, y) = +inf for y an even integer |
| 66 | else { | 66 | else { |
lib/std/math/signbit.zig+12-57| ... | @@ -5,64 +5,19 @@ const expect = std.testing.expect; | ... | @@ -5,64 +5,19 @@ const expect = std.testing.expect; |
| 5 | /// Returns whether x is negative or negative 0. | 5 | /// Returns whether x is negative or negative 0. |
| 6 | pub fn signbit(x: anytype) bool { | 6 | pub fn signbit(x: anytype) bool { |
| 7 | const T = @TypeOf(x); | 7 | const T = @TypeOf(x); |
| 8 | return switch (T) { | 8 | const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 9 | f16 => signbit16(x), | 9 | return @bitCast(TBits, x) >> (@bitSizeOf(T) - 1) != 0; |
| 10 | f32 => signbit32(x), | ||
| 11 | f64 => signbit64(x), | ||
| 12 | f80 => signbit80(x), | ||
| 13 | f128 => signbit128(x), | ||
| 14 | else => @compileError("signbit not implemented for " ++ @typeName(T)), | ||
| 15 | }; | ||
| 16 | } | ||
| 17 | |||
| 18 | fn signbit16(x: f16) bool { | ||
| 19 | const bits = @bitCast(u16, x); | ||
| 20 | return bits >> 15 != 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | fn signbit32(x: f32) bool { | ||
| 24 | const bits = @bitCast(u32, x); | ||
| 25 | return bits >> 31 != 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | fn signbit64(x: f64) bool { | ||
| 29 | const bits = @bitCast(u64, x); | ||
| 30 | return bits >> 63 != 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | fn signbit80(x: f80) bool { | ||
| 34 | const bits = @bitCast(u80, x); | ||
| 35 | return bits >> 79 != 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | fn signbit128(x: f128) bool { | ||
| 39 | const bits = @bitCast(u128, x); | ||
| 40 | return bits >> 127 != 0; | ||
| 41 | } | 10 | } |
| 42 | 11 | ||
| 43 | test "math.signbit" { | 12 | test "math.signbit" { |
| 44 | try expect(signbit(@as(f16, 4.0)) == signbit16(4.0)); | 13 | inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| { |
| 45 | try expect(signbit(@as(f32, 4.0)) == signbit32(4.0)); | 14 | try expect(!signbit(@as(T, 0.0))); |
| 46 | try expect(signbit(@as(f64, 4.0)) == signbit64(4.0)); | 15 | try expect(!signbit(@as(T, 1.0))); |
| 47 | try expect(signbit(@as(f128, 4.0)) == signbit128(4.0)); | 16 | try expect(signbit(@as(T, -2.0))); |
| 48 | } | 17 | try expect(signbit(@as(T, -0.0))); |
| 49 | 18 | try expect(!signbit(math.inf(T))); | |
| 50 | test "math.signbit16" { | 19 | try expect(signbit(-math.inf(T))); |
| 51 | try expect(!signbit16(4.0)); | 20 | try expect(!signbit(math.nan(T))); |
| 52 | try expect(signbit16(-3.0)); | 21 | try expect(signbit(-math.nan(T))); |
| 53 | } | 22 | } |
| 54 | |||
| 55 | test "math.signbit32" { | ||
| 56 | try expect(!signbit32(4.0)); | ||
| 57 | try expect(signbit32(-3.0)); | ||
| 58 | } | ||
| 59 | |||
| 60 | test "math.signbit64" { | ||
| 61 | try expect(!signbit64(4.0)); | ||
| 62 | try expect(signbit64(-3.0)); | ||
| 63 | } | ||
| 64 | |||
| 65 | test "math.signbit128" { | ||
| 66 | try expect(!signbit128(4.0)); | ||
| 67 | try expect(signbit128(-3.0)); | ||
| 68 | } | 23 | } |