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(
16551655 if (numberToken.is_integer)
16561656 return try std.fmt.parseInt(T, numberToken.slice(tokens.slice, tokens.i - 1), 10);
16571657 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;
16591659 if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;
16601660 return @floatToInt(T, float);
16611661 },
lib/std/math/complex/cosh.zig+8-8
......@@ -38,14 +38,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
3838 }
3939 // small x: normal case
4040 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));
4242 }
4343
4444 // |x|>= 9, so cosh(x) ~= exp(|x|)
4545 if (ix < 0x42b17218) {
4646 // x < 88.7: exp(|x|) won't overflow
4747 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));
4949 }
5050 // x < 192.7: scale to avoid overflow
5151 else if (ix < 0x4340b1e7) {
......@@ -56,7 +56,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
5656 // x >= 192.7: result always overflows
5757 else {
5858 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));
6060 }
6161 }
6262
......@@ -79,7 +79,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
7979 if (iy >= 0x7f800000) {
8080 return Complex(f32).init(x * x, x * (y - y));
8181 }
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));
8383 }
8484
8585 return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
......@@ -106,14 +106,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
106106 }
107107 // small x: normal case
108108 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));
110110 }
111111
112112 // |x|>= 22, so cosh(x) ~= exp(|x|)
113113 if (ix < 0x40862e42) {
114114 // x < 710: exp(|x|) won't overflow
115115 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));
117117 }
118118 // x < 1455: scale to avoid overflow
119119 else if (ix < 0x4096bbaa) {
......@@ -124,7 +124,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
124124 // x >= 1455: result always overflows
125125 else {
126126 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));
128128 }
129129 }
130130
......@@ -147,7 +147,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
147147 if (iy >= 0x7ff00000) {
148148 return Complex(f64).init(x * x, x * (y - y));
149149 }
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));
151151 }
152152
153153 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) {
3939 const hx = @bitCast(u32, x);
4040 // cexp(0 + iy) = cos(y) + isin(y)
4141 if ((hx & 0x7fffffff) == 0) {
42 return Complex(f32).init(math.cos(y), math.sin(y));
42 return Complex(f32).init(@cos(y), @sin(y));
4343 }
4444
4545 if (hy >= 0x7f800000) {
......@@ -64,7 +64,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
6464 // - x = nan
6565 else {
6666 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));
6868 }
6969}
7070
......@@ -90,7 +90,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
9090
9191 // cexp(0 + iy) = cos(y) + isin(y)
9292 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));
9494 }
9595
9696 if (hy >= 0x7ff00000) {
......@@ -115,7 +115,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
115115 // - x = nan
116116 else {
117117 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));
119119 }
120120}
121121
lib/std/math/complex/ldexp.zig+4-4
......@@ -45,8 +45,8 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
4545 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
4646
4747 return Complex(f32).init(
48 math.cos(z.im) * exp_x * scale1 * scale2,
49 math.sin(z.im) * exp_x * scale1 * scale2,
48 @cos(z.im) * exp_x * scale1 * scale2,
49 @sin(z.im) * exp_x * scale1 * scale2,
5050 );
5151}
5252
......@@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
7878 const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32));
7979
8080 return Complex(f64).init(
81 math.cos(z.im) * exp_x * scale1 * scale2,
82 math.sin(z.im) * exp_x * scale1 * scale2,
81 @cos(z.im) * exp_x * scale1 * scale2,
82 @sin(z.im) * exp_x * scale1 * scale2,
8383 );
8484}
lib/std/math/complex/sinh.zig+8-8
......@@ -38,14 +38,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
3838 }
3939 // small x: normal case
4040 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));
4242 }
4343
4444 // |x|>= 9, so cosh(x) ~= exp(|x|)
4545 if (ix < 0x42b17218) {
4646 // x < 88.7: exp(|x|) won't overflow
4747 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));
4949 }
5050 // x < 192.7: scale to avoid overflow
5151 else if (ix < 0x4340b1e7) {
......@@ -56,7 +56,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
5656 // x >= 192.7: result always overflows
5757 else {
5858 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));
6060 }
6161 }
6262
......@@ -79,7 +79,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
7979 if (iy >= 0x7f800000) {
8080 return Complex(f32).init(x * x, x * (y - y));
8181 }
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));
8383 }
8484
8585 return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
......@@ -105,14 +105,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
105105 }
106106 // small x: normal case
107107 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));
109109 }
110110
111111 // |x|>= 22, so cosh(x) ~= exp(|x|)
112112 if (ix < 0x40862e42) {
113113 // x < 710: exp(|x|) won't overflow
114114 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));
116116 }
117117 // x < 1455: scale to avoid overflow
118118 else if (ix < 0x4096bbaa) {
......@@ -123,7 +123,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
123123 // x >= 1455: result always overflows
124124 else {
125125 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));
127127 }
128128 }
129129
......@@ -146,7 +146,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
146146 if (iy >= 0x7ff00000) {
147147 return Complex(f64).init(x * x, x * (y - y));
148148 }
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));
150150 }
151151
152152 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) {
3333 return Complex(f32).init(x, r);
3434 }
3535 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);
3737 return Complex(f32).init(xx, math.copysign(f32, 0, r));
3838 }
3939
......@@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
4545 // x >= 11
4646 if (ix >= 0x41300000) {
4747 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);
4949 }
5050
5151 // Kahan's algorithm
......@@ -76,7 +76,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
7676 }
7777
7878 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);
8080 return Complex(f64).init(xx, math.copysign(f64, 0, r));
8181 }
8282
......@@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
8888 // x >= 22
8989 if (ix >= 0x40360000) {
9090 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);
9292 }
9393
9494 // Kahan's algorithm