std: use float builtins instead of std.math

This commit is contained in:
Andrew Kelley 2022-04-27 19:35:28 -07:00
parent c26f7550f0
commit 0ffe82e624
6 changed files with 29 additions and 29 deletions

View File

@ -1655,7 +1655,7 @@ fn parseInternal(
if (numberToken.is_integer)
return try std.fmt.parseInt(T, numberToken.slice(tokens.slice, tokens.i - 1), 10);
const float = try std.fmt.parseFloat(f128, numberToken.slice(tokens.slice, tokens.i - 1));
if (std.math.round(float) != float) return error.InvalidNumber;
if (@round(float) != float) return error.InvalidNumber;
if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;
return @floatToInt(T, float);
},

View File

@ -38,14 +38,14 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
}
// small x: normal case
if (ix < 0x41100000) {
return Complex(f32).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));
return Complex(f32).init(math.cosh(x) * @cos(y), math.sinh(x) * @sin(y));
}
// |x|>= 9, so cosh(x) ~= exp(|x|)
if (ix < 0x42b17218) {
// x < 88.7: exp(|x|) won't overflow
const h = @exp(@fabs(x)) * 0.5;
return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y));
return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y));
}
// x < 192.7: scale to avoid overflow
else if (ix < 0x4340b1e7) {
@ -56,7 +56,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
// x >= 192.7: result always overflows
else {
const h = 0x1p127 * x;
return Complex(f32).init(h * h * math.cos(y), h * math.sin(y));
return Complex(f32).init(h * h * @cos(y), h * @sin(y));
}
}
@ -79,7 +79,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (iy >= 0x7f800000) {
return Complex(f32).init(x * x, x * (y - y));
}
return Complex(f32).init((x * x) * math.cos(y), x * math.sin(y));
return Complex(f32).init((x * x) * @cos(y), x * @sin(y));
}
return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
@ -106,14 +106,14 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
}
// small x: normal case
if (ix < 0x40360000) {
return Complex(f64).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y));
return Complex(f64).init(math.cosh(x) * @cos(y), math.sinh(x) * @sin(y));
}
// |x|>= 22, so cosh(x) ~= exp(|x|)
if (ix < 0x40862e42) {
// x < 710: exp(|x|) won't overflow
const h = @exp(@fabs(x)) * 0.5;
return Complex(f64).init(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y));
return Complex(f64).init(h * @cos(y), math.copysign(f64, h, x) * @sin(y));
}
// x < 1455: scale to avoid overflow
else if (ix < 0x4096bbaa) {
@ -124,7 +124,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
// x >= 1455: result always overflows
else {
const h = 0x1p1023;
return Complex(f64).init(h * h * math.cos(y), h * math.sin(y));
return Complex(f64).init(h * h * @cos(y), h * @sin(y));
}
}
@ -147,7 +147,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
if (iy >= 0x7ff00000) {
return Complex(f64).init(x * x, x * (y - y));
}
return Complex(f64).init(x * x * math.cos(y), x * math.sin(y));
return Complex(f64).init(x * x * @cos(y), x * @sin(y));
}
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));

View File

@ -39,7 +39,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
const hx = @bitCast(u32, x);
// cexp(0 + iy) = cos(y) + isin(y)
if ((hx & 0x7fffffff) == 0) {
return Complex(f32).init(math.cos(y), math.sin(y));
return Complex(f32).init(@cos(y), @sin(y));
}
if (hy >= 0x7f800000) {
@ -64,7 +64,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
// - x = nan
else {
const exp_x = @exp(x);
return Complex(f32).init(exp_x * math.cos(y), exp_x * math.sin(y));
return Complex(f32).init(exp_x * @cos(y), exp_x * @sin(y));
}
}
@ -90,7 +90,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
// cexp(0 + iy) = cos(y) + isin(y)
if ((hx & 0x7fffffff) | lx == 0) {
return Complex(f64).init(math.cos(y), math.sin(y));
return Complex(f64).init(@cos(y), @sin(y));
}
if (hy >= 0x7ff00000) {
@ -115,7 +115,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
// - x = nan
else {
const exp_x = @exp(x);
return Complex(f64).init(exp_x * math.cos(y), exp_x * math.sin(y));
return Complex(f64).init(exp_x * @cos(y), exp_x * @sin(y));
}
}

View File

@ -45,8 +45,8 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
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,
@cos(z.im) * exp_x * scale1 * scale2,
@sin(z.im) * exp_x * scale1 * scale2,
);
}
@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32));
return Complex(f64).init(
math.cos(z.im) * exp_x * scale1 * scale2,
math.sin(z.im) * exp_x * scale1 * scale2,
@cos(z.im) * exp_x * scale1 * scale2,
@sin(z.im) * exp_x * scale1 * scale2,
);
}

View File

@ -38,14 +38,14 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
}
// small x: normal case
if (ix < 0x41100000) {
return Complex(f32).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));
return Complex(f32).init(math.sinh(x) * @cos(y), math.cosh(x) * @sin(y));
}
// |x|>= 9, so cosh(x) ~= exp(|x|)
if (ix < 0x42b17218) {
// x < 88.7: exp(|x|) won't overflow
const h = @exp(@fabs(x)) * 0.5;
return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y));
return Complex(f32).init(math.copysign(f32, h, x) * @cos(y), h * @sin(y));
}
// x < 192.7: scale to avoid overflow
else if (ix < 0x4340b1e7) {
@ -56,7 +56,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
// x >= 192.7: result always overflows
else {
const h = 0x1p127 * x;
return Complex(f32).init(h * math.cos(y), h * h * math.sin(y));
return Complex(f32).init(h * @cos(y), h * h * @sin(y));
}
}
@ -79,7 +79,7 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
if (iy >= 0x7f800000) {
return Complex(f32).init(x * x, x * (y - y));
}
return Complex(f32).init(x * math.cos(y), math.inf(f32) * math.sin(y));
return Complex(f32).init(x * @cos(y), math.inf(f32) * @sin(y));
}
return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y));
@ -105,14 +105,14 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
}
// small x: normal case
if (ix < 0x40360000) {
return Complex(f64).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y));
return Complex(f64).init(math.sinh(x) * @cos(y), math.cosh(x) * @sin(y));
}
// |x|>= 22, so cosh(x) ~= exp(|x|)
if (ix < 0x40862e42) {
// x < 710: exp(|x|) won't overflow
const h = @exp(@fabs(x)) * 0.5;
return Complex(f64).init(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y));
return Complex(f64).init(math.copysign(f64, h, x) * @cos(y), h * @sin(y));
}
// x < 1455: scale to avoid overflow
else if (ix < 0x4096bbaa) {
@ -123,7 +123,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
// x >= 1455: result always overflows
else {
const h = 0x1p1023 * x;
return Complex(f64).init(h * math.cos(y), h * h * math.sin(y));
return Complex(f64).init(h * @cos(y), h * h * @sin(y));
}
}
@ -146,7 +146,7 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
if (iy >= 0x7ff00000) {
return Complex(f64).init(x * x, x * (y - y));
}
return Complex(f64).init(x * math.cos(y), math.inf(f64) * math.sin(y));
return Complex(f64).init(x * @cos(y), math.inf(f64) * @sin(y));
}
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));

View File

@ -33,7 +33,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
return Complex(f32).init(x, r);
}
const xx = @bitCast(f32, hx - 0x40000000);
const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y);
const r = if (math.isInf(y)) y else @sin(y) * @cos(y);
return Complex(f32).init(xx, math.copysign(f32, 0, r));
}
@ -45,7 +45,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
// x >= 11
if (ix >= 0x41300000) {
const exp_mx = @exp(-@fabs(x));
return Complex(f32).init(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);
return Complex(f32).init(math.copysign(f32, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
}
// Kahan's algorithm
@ -76,7 +76,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
}
const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx);
const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y);
const r = if (math.isInf(y)) y else @sin(y) * @cos(y);
return Complex(f64).init(xx, math.copysign(f64, 0, r));
}
@ -88,7 +88,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
// x >= 22
if (ix >= 0x40360000) {
const exp_mx = @exp(-@fabs(x));
return Complex(f64).init(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx);
return Complex(f64).init(math.copysign(f64, 1, x), 4 * @sin(y) * @cos(y) * exp_mx * exp_mx);
}
// Kahan's algorithm