authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-27 19:35:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-27 19:35:28-07:00
log0ffe82e624f9334473758349219fe1dbcf3edb3b
tree6a03c1219eb2b836f8b0d58cf18e6d52e99dac4c
parentc26f7550f037e44fea51983eeb3f0adbb34d1d38

std: use float builtins instead of std.math


6 files changed, 29 insertions(+), 29 deletions(-)

lib/std/json.zig+1-1
...@@ -1655,7 +1655,7 @@ fn parseInternal(...@@ -1655,7 +1655,7 @@ fn parseInternal(
1655 if (numberToken.is_integer)1655 if (numberToken.is_integer)
1656 return try std.fmt.parseInt(T, numberToken.slice(tokens.slice, tokens.i - 1), 10);1656 return try std.fmt.parseInt(T, numberToken.slice(tokens.slice, tokens.i - 1), 10);
1657 const float = try std.fmt.parseFloat(f128, numberToken.slice(tokens.slice, tokens.i - 1));1657 const float = try std.fmt.parseFloat(f128, numberToken.slice(tokens.slice, tokens.i - 1));
1658 if (std.math.round(float) != float) return error.InvalidNumber;1658 if (@round(float) != float) return error.InvalidNumber;
1659 if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;1659 if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;
1660 return @floatToInt(T, float);1660 return @floatToInt(T, float);
1661 },1661 },
lib/std/math/complex/cosh.zig+8-8
...@@ -38,14 +38,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) {...@@ -38,14 +38,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
38 }38 }
39 // small x: normal case39 // small x: normal case
40 if (ix < 0x41100000) {40 if (ix < 0x41100000) {
41 return Complex(f32).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));41 return Complex(f32).init(math.cosh(x) * @cos(y), math.sinh(x) * @sin(y));
42 }42 }
4343
44 // |x|>= 9, so cosh(x) ~= exp(|x|)44 // |x|>= 9, so cosh(x) ~= exp(|x|)
45 if (ix < 0x42b17218) {45 if (ix < 0x42b17218) {
46 // x < 88.7: exp(|x|) won't overflow46 // 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) * math.cos(y), h * math.sin(y));48 return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y));
49 }49 }
50 // x < 192.7: scale to avoid overflow50 // x < 192.7: scale to avoid overflow
51 else if (ix < 0x4340b1e7) {51 else if (ix < 0x4340b1e7) {
...@@ -56,7 +56,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {...@@ -56,7 +56,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
56 // x >= 192.7: result always overflows56 // x >= 192.7: result always overflows
57 else {57 else {
58 const h = 0x1p127 * x;58 const h = 0x1p127 * x;
59 return Complex(f32).init(h * h * math.cos(y), h * math.sin(y));59 return Complex(f32).init(h * h * @cos(y), h * @sin(y));
60 }60 }
61 }61 }
6262
...@@ -79,7 +79,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {...@@ -79,7 +79,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
79 if (iy >= 0x7f800000) {79 if (iy >= 0x7f800000) {
80 return Complex(f32).init(x * x, x * (y - y));80 return Complex(f32).init(x * x, x * (y - y));
81 }81 }
82 return Complex(f32).init((x * x) * math.cos(y), x * math.sin(y));82 return Complex(f32).init((x * x) * @cos(y), x * @sin(y));
83 }83 }
8484
85 return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));85 return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
...@@ -106,14 +106,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) {...@@ -106,14 +106,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
106 }106 }
107 // small x: normal case107 // small x: normal case
108 if (ix < 0x40360000) {108 if (ix < 0x40360000) {
109 return Complex(f64).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));109 return Complex(f64).init(math.cosh(x) * @cos(y), math.sinh(x) * @sin(y));
110 }110 }
111111
112 // |x|>= 22, so cosh(x) ~= exp(|x|)112 // |x|>= 22, so cosh(x) ~= exp(|x|)
113 if (ix < 0x40862e42) {113 if (ix < 0x40862e42) {
114 // x < 710: exp(|x|) won't overflow114 // 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 * math.cos(y), math.copysign(f64, h, x) * math.sin(y));116 return Complex(f64).init(h * @cos(y), math.copysign(f64, h, x) * @sin(y));
117 }117 }
118 // x < 1455: scale to avoid overflow118 // x < 1455: scale to avoid overflow
119 else if (ix < 0x4096bbaa) {119 else if (ix < 0x4096bbaa) {
...@@ -124,7 +124,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {...@@ -124,7 +124,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
124 // x >= 1455: result always overflows124 // x >= 1455: result always overflows
125 else {125 else {
126 const h = 0x1p1023;126 const h = 0x1p1023;
127 return Complex(f64).init(h * h * math.cos(y), h * math.sin(y));127 return Complex(f64).init(h * h * @cos(y), h * @sin(y));
128 }128 }
129 }129 }
130130
...@@ -147,7 +147,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {...@@ -147,7 +147,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
147 if (iy >= 0x7ff00000) {147 if (iy >= 0x7ff00000) {
148 return Complex(f64).init(x * x, x * (y - y));148 return Complex(f64).init(x * x, x * (y - y));
149 }149 }
150 return Complex(f64).init(x * x * math.cos(y), x * math.sin(y));150 return Complex(f64).init(x * x * @cos(y), x * @sin(y));
151 }151 }
152152
153 return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));153 return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
lib/std/math/complex/exp.zig+4-4
...@@ -39,7 +39,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {...@@ -39,7 +39,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
39 const hx = @bitCast(u32, x);39 const hx = @bitCast(u32, x);
40 // cexp(0 + iy) = cos(y) + isin(y)40 // cexp(0 + iy) = cos(y) + isin(y)
41 if ((hx & 0x7fffffff) == 0) {41 if ((hx & 0x7fffffff) == 0) {
42 return Complex(f32).init(math.cos(y), math.sin(y));42 return Complex(f32).init(@cos(y), @sin(y));
43 }43 }
4444
45 if (hy >= 0x7f800000) {45 if (hy >= 0x7f800000) {
...@@ -64,7 +64,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {...@@ -64,7 +64,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
64 // - x = nan64 // - x = nan
65 else {65 else {
66 const exp_x = @exp(x);66 const exp_x = @exp(x);
67 return Complex(f32).init(exp_x * math.cos(y), exp_x * math.sin(y));67 return Complex(f32).init(exp_x * @cos(y), exp_x * @sin(y));
68 }68 }
69}69}
7070
...@@ -90,7 +90,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {...@@ -90,7 +90,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
9090
91 // cexp(0 + iy) = cos(y) + isin(y)91 // cexp(0 + iy) = cos(y) + isin(y)
92 if ((hx & 0x7fffffff) | lx == 0) {92 if ((hx & 0x7fffffff) | lx == 0) {
93 return Complex(f64).init(math.cos(y), math.sin(y));93 return Complex(f64).init(@cos(y), @sin(y));
94 }94 }
9595
96 if (hy >= 0x7ff00000) {96 if (hy >= 0x7ff00000) {
...@@ -115,7 +115,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {...@@ -115,7 +115,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
115 // - x = nan115 // - x = nan
116 else {116 else {
117 const exp_x = @exp(x);117 const exp_x = @exp(x);
118 return Complex(f64).init(exp_x * math.cos(y), exp_x * math.sin(y));118 return Complex(f64).init(exp_x * @cos(y), exp_x * @sin(y));
119 }119 }
120}120}
121121
lib/std/math/complex/ldexp.zig+4-4
...@@ -45,8 +45,8 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {...@@ -45,8 +45,8 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
45 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);45 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
4646
47 return Complex(f32).init(47 return Complex(f32).init(
48 math.cos(z.im) * exp_x * scale1 * scale2,48 @cos(z.im) * exp_x * scale1 * scale2,
49 math.sin(z.im) * exp_x * scale1 * scale2,49 @sin(z.im) * exp_x * scale1 * scale2,
50 );50 );
51}51}
5252
...@@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {...@@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
78 const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32));78 const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32));
7979
80 return Complex(f64).init(80 return Complex(f64).init(
81 math.cos(z.im) * exp_x * scale1 * scale2,81 @cos(z.im) * exp_x * scale1 * scale2,
82 math.sin(z.im) * exp_x * scale1 * scale2,82 @sin(z.im) * exp_x * scale1 * scale2,
83 );83 );
84}84}
lib/std/math/complex/sinh.zig+8-8
...@@ -38,14 +38,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) {...@@ -38,14 +38,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
38 }38 }
39 // small x: normal case39 // small x: normal case
40 if (ix < 0x41100000) {40 if (ix < 0x41100000) {
41 return Complex(f32).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));41 return Complex(f32).init(math.sinh(x) * @cos(y), math.cosh(x) * @sin(y));
42 }42 }
4343
44 // |x|>= 9, so cosh(x) ~= exp(|x|)44 // |x|>= 9, so cosh(x) ~= exp(|x|)
45 if (ix < 0x42b17218) {45 if (ix < 0x42b17218) {
46 // x < 88.7: exp(|x|) won't overflow46 // 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) * math.cos(y), h * math.sin(y));48 return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y));
49 }49 }
50 // x < 192.7: scale to avoid overflow50 // x < 192.7: scale to avoid overflow
51 else if (ix < 0x4340b1e7) {51 else if (ix < 0x4340b1e7) {
...@@ -56,7 +56,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {...@@ -56,7 +56,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
56 // x >= 192.7: result always overflows56 // x >= 192.7: result always overflows
57 else {57 else {
58 const h = 0x1p127 * x;58 const h = 0x1p127 * x;
59 return Complex(f32).init(h * math.cos(y), h * h * math.sin(y));59 return Complex(f32).init(h * @cos(y), h * h * @sin(y));
60 }60 }
61 }61 }
6262
...@@ -79,7 +79,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {...@@ -79,7 +79,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
79 if (iy >= 0x7f800000) {79 if (iy >= 0x7f800000) {
80 return Complex(f32).init(x * x, x * (y - y));80 return Complex(f32).init(x * x, x * (y - y));
81 }81 }
82 return Complex(f32).init(x * math.cos(y), math.inf(f32) * math.sin(y));82 return Complex(f32).init(x * @cos(y), math.inf(f32) * @sin(y));
83 }83 }
8484
85 return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));85 return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
...@@ -105,14 +105,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) {...@@ -105,14 +105,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
105 }105 }
106 // small x: normal case106 // small x: normal case
107 if (ix < 0x40360000) {107 if (ix < 0x40360000) {
108 return Complex(f64).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));108 return Complex(f64).init(math.sinh(x) * @cos(y), math.cosh(x) * @sin(y));
109 }109 }
110110
111 // |x|>= 22, so cosh(x) ~= exp(|x|)111 // |x|>= 22, so cosh(x) ~= exp(|x|)
112 if (ix < 0x40862e42) {112 if (ix < 0x40862e42) {
113 // x < 710: exp(|x|) won't overflow113 // 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) * math.cos(y), h * math.sin(y));115 return Complex(f64).init(math.copysign(f64, h, x) * @cos(y), h * @sin(y));
116 }116 }
117 // x < 1455: scale to avoid overflow117 // x < 1455: scale to avoid overflow
118 else if (ix < 0x4096bbaa) {118 else if (ix < 0x4096bbaa) {
...@@ -123,7 +123,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {...@@ -123,7 +123,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
123 // x >= 1455: result always overflows123 // x >= 1455: result always overflows
124 else {124 else {
125 const h = 0x1p1023 * x;125 const h = 0x1p1023 * x;
126 return Complex(f64).init(h * math.cos(y), h * h * math.sin(y));126 return Complex(f64).init(h * @cos(y), h * h * @sin(y));
127 }127 }
128 }128 }
129129
...@@ -146,7 +146,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {...@@ -146,7 +146,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
146 if (iy >= 0x7ff00000) {146 if (iy >= 0x7ff00000) {
147 return Complex(f64).init(x * x, x * (y - y));147 return Complex(f64).init(x * x, x * (y - y));
148 }148 }
149 return Complex(f64).init(x * math.cos(y), math.inf(f64) * math.sin(y));149 return Complex(f64).init(x * @cos(y), math.inf(f64) * @sin(y));
150 }150 }
151151
152 return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));152 return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
lib/std/math/complex/tanh.zig+4-4
...@@ -33,7 +33,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {...@@ -33,7 +33,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
33 return Complex(f32).init(x, r);33 return Complex(f32).init(x, r);
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 math.sin(y) * math.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(f32, 0, r));
38 }38 }
3939
...@@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {...@@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
45 // x >= 1145 // 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 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);48 return Complex(f32).init(math.copysign(f32, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
49 }49 }
5050
51 // Kahan's algorithm51 // Kahan's algorithm
...@@ -76,7 +76,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {...@@ -76,7 +76,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
76 }76 }
7777
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 math.sin(y) * math.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(f64, 0, r));
81 }81 }
8282
...@@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {...@@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
88 // x >= 2288 // 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 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);91 return Complex(f64).init(math.copysign(f64, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
92 }92 }
9393
94 // Kahan's algorithm94 // Kahan's algorithm