跳转到内容
标准库Zig 0.17.1官方原文
源码std/math/pow.zig

第 6 行

std.math.pow.expect

别名

第 8 行

别名目标 std.testing.expect

std.math.pow.isOddInteger

函数

第 183 行

fn isOddInteger(x: f64) bool

参数

x: f64

用法示例

test isOddInteger {
    try expect(isOddInteger(@floatFromInt(math.maxInt(i64) * 2)) == false);
    try expect(isOddInteger(@floatFromInt(math.maxInt(i64) * 2 + 1)) == false);
    try expect(isOddInteger(1 << 53) == false);
    try expect(isOddInteger(12.0) == false);
    try expect(isOddInteger(15.0) == true);
}

std.math.pow.math

别名

第 7 行

别名目标 std.math

std.math.pow.pow

函数

第 33 行

返回 x 的 y 次幂(x^y)。

特殊情况:

  • 对任意 x,pow(x, +-0) = 1
  • 对任意 y,pow(1, y) = 1
  • 对任意 x,pow(x, 1) = x
  • 对任意 y != 0,pow(nan, y) = nan
  • 对任意 x != 1,pow(x, nan) = nan
  • 当 y 为小于 0 的奇整数时,pow(+-0, y) = +-inf
  • pow(+-0, -inf) = +inf
  • pow(+-0, +inf) = +0
  • 当有限的 y < 0 且不是奇整数时,pow(+-0, y) = +inf
  • 当有限的 y 为大于 0 的奇整数时,pow(+-0, y) = +-0
  • 当有限的 y > 0 且不是奇整数时,pow(+-0, y) = +0
  • pow(-1, +-inf) = 1
  • 当 |x| > 1 时,pow(x, +inf) = +inf
  • 当 |x| > 1 时,pow(x, -inf) = +0
  • 当 |x| < 1 时,pow(x, +inf) = +0
  • 当 |x| < 1 时,pow(x, -inf) = +inf
  • 当 y > 0 时,pow(+inf, y) = +inf
  • 当 y < 0 时,pow(+inf, y) = +0
  • pow(-inf, y) = pow(-0, -y)
  • 当有限的 x < 0 且 y 为有限非整数时,pow(x, y) = nan
pub fn pow(comptime T: type, x: T, y: T) T

参数

T: type
x: T
y: T

用法示例

test pow {
    const epsilon = 0.000001;

    try expect(math.approxEqAbs(f32, pow(f32, 0.0, 3.3), 0.0, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 0.8923, 3.3), 0.686572, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 0.2, 3.3), 0.004936, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 1.5, 3.3), 3.811546, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 37.45, 3.3), 155736.703125, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 89.123, 3.3), 2722489.5, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, -1.0, 1e10), 1.0, epsilon));

    try expect(math.approxEqAbs(f64, pow(f64, 0.0, 3.3), 0.0, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 0.8923, 3.3), 0.686572, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 0.2, 3.3), 0.004936, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 1.5, 3.3), 3.811546, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 37.45, 3.3), 155736.7160616, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 89.123, 3.3), 2722490.231436, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, -1.0, 1e20), 1.0, epsilon));
}

std.math.pow.std

别名

第 6 行

别名目标 std

源码

std/math/pow.zig

// 移植自采用 BSD-3 许可证的 Go。
// https://golang.org/LICENSE
//
// https://golang.org/src/math/pow.go

const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;

/// 返回 x 的 y 次幂(x^y)。
///
/// 特殊情况:
///  - 对任意 x,pow(x, +-0) = 1
///  - 对任意 y,pow(1, y) = 1
///  - 对任意 x,pow(x, 1) = x
///  - 对任意 y != 0,pow(nan, y) = nan
///  - 对任意 x != 1,pow(x, nan) = nan
///  - 当 y 为小于 0 的奇整数时,pow(+-0, y) = +-inf
///  - pow(+-0, -inf) = +inf
///  - pow(+-0, +inf) = +0
///  - 当有限的 y < 0 且不是奇整数时,pow(+-0, y) = +inf
///  - 当有限的 y 为大于 0 的奇整数时,pow(+-0, y) = +-0
///  - 当有限的 y > 0 且不是奇整数时,pow(+-0, y) = +0
///  - pow(-1, +-inf) = 1
///  - 当 |x| > 1 时,pow(x, +inf) = +inf
///  - 当 |x| > 1 时,pow(x, -inf) = +0
///  - 当 |x| < 1 时,pow(x, +inf) = +0
///  - 当 |x| < 1 时,pow(x, -inf) = +inf
///  - 当 y > 0 时,pow(+inf, y) = +inf
///  - 当 y < 0 时,pow(+inf, y) = +0
///  - pow(-inf, y) = pow(-0, -y)
///  - 当有限的 x < 0 且 y 为有限非整数时,pow(x, y) = nan
pub fn pow(comptime T: type, x: T, y: T) T {
    if (@typeInfo(T) == .int) {
        return math.powi(T, x, y) catch unreachable;
    }

    if (T != f32 and T != f64) {
        @compileError("pow not implemented for " ++ @typeName(T));
    }

    // pow(x, +-0) = 1,对任意 x 成立。
    // pow(1, y) = 1,对任意 y 成立。
    if (y == 0 or x == 1) {
        return 1;
    }

    // pow(nan, y) = nan    对于任意 y != 0
    // pow(x, nan) = nan    对于任意 x != 1
    if (math.isNan(x) or math.isNan(y)) {
        @branchHint(.unlikely);
        return math.nan(T);
    }

    // 对任意 x,pow(x, 1) = x
    if (y == 1) {
        return x;
    }

    if (x == 0) {
        if (y < 0) {
            // 当 y 为奇整数时,pow(+-0, y) = +-inf
            if (isOddInteger(y)) {
                return math.copysign(math.inf(T), x);
            }
            // 当 y 为偶整数时,pow(+-0, y) = +inf
            else {
                return math.inf(T);
            }
        } else {
            if (isOddInteger(y)) {
                return x;
            } else {
                return 0;
            }
        }
    }

    if (math.isInf(y)) {
        // pow(-1, inf) = 1
        if (x == -1) {
            return 1.0;
        }
        // pow(x, +inf) = +0    当 |x| < 1 时
        // pow(x, -inf) = +0    当 |x| > 1 时
        else if ((@abs(x) < 1) == math.isPositiveInf(y)) {
            return 0;
        }
        // 当 |x| < 1 时,pow(x, -inf) = +inf
        // 当 |x| > 1 时,pow(x, +inf) = +inf
        else {
            return math.inf(T);
        }
    }

    if (math.isInf(x)) {
        if (math.isNegativeInf(x)) {
            return pow(T, 1 / x, -y);
        }
        // pow(+inf, y) = +0    for y < 0
        else if (y < 0) {
            return 0;
        }
        // 当 y > 0 时,pow(+inf, y) = +0
        else if (y > 0) {
            return math.inf(T);
        }
    }

    // sqrt 特殊情况
    if (y == 0.5) {
        return @sqrt(x);
    }

    if (y == -0.5) {
        return 1 / @sqrt(x);
    }

    const r1 = math.modf(@abs(y));
    var yi = r1.ipart;
    var yf = r1.fpart;

    if (yf != 0 and x < 0) {
        return math.nan(T);
    }
    if (yi >= 1 << (@typeInfo(T).float.bits - 1)) {
        // yi 是较大的偶数,因此结果始终为正,且 x 的符号无关紧要
        //
        return @exp(y * @log(@abs(x)));
    }

    // a = a1 * 2^ae
    var a1: T = 1.0;
    var ae: i32 = 0;

    // a *= x^yf
    if (yf != 0) {
        if (yf > 0.5) {
            yf -= 1;
            yi += 1;
        }
        a1 = @exp(yf * @log(x));
    }

    // a *= x^yi
    const r2 = math.frexp(x);
    var xe = r2.exponent;
    var x1 = r2.significand;

    var i = @as(@Int(.signed, @typeInfo(T).float.bits), @intFromFloat(yi));
    while (i != 0) : (i >>= 1) {
        const overflow_shift = math.floatExponentBits(T) + 1;
        if (xe < -(1 << overflow_shift) or (1 << overflow_shift) < xe) {
            // 在下方左移使 xe 溢出前截获它。
            // 因为 i != 0,它至少还有一位为 1,因此 ae 至少还会
            // 在一次迭代中累加 xe;ae += xe 是 ae 的下界。
            // ae 的下界超过浮点数指数的范围,
            // 因此最终调用 Ldexp 会产生下溢/上溢,即 0/Inf。
            ae += xe;
            break;
        }
        if (i & 1 == 1) {
            a1 *= x1;
            ae += xe;
        }
        x1 *= x1;
        xe <<= 1;
        if (x1 < 0.5) {
            x1 += x1;
            xe -= 1;
        }
    }

    // a *= a1 * 2^ae
    if (y < 0) {
        a1 = 1 / a1;
        ae = -ae;
    }

    return math.scalbn(a1, ae);
}

fn isOddInteger(x: f64) bool {
    if (@abs(x) >= 1 << 53) {
        // 来自 https://golang.org/src/math/pow.go
        // 1 << 53 是 float64 格式能精确表示的最大整数。
        // 此范围之外的任何数都会在小数点之前被截断,因此始终
        // 是偶数。
        // 如果没有此检查,且 x 超出 i64 范围,下方的 @intFromFloat(r.ipart) 转换将触发 panic。
        return false;
    }
    const r = math.modf(x);
    return r.fpart == 0.0 and @as(i64, @intFromFloat(r.ipart)) & 1 == 1;
}

test isOddInteger {
    try expect(isOddInteger(@floatFromInt(math.maxInt(i64) * 2)) == false);
    try expect(isOddInteger(@floatFromInt(math.maxInt(i64) * 2 + 1)) == false);
    try expect(isOddInteger(1 << 53) == false);
    try expect(isOddInteger(12.0) == false);
    try expect(isOddInteger(15.0) == true);
}

test pow {
    const epsilon = 0.000001;

    try expect(math.approxEqAbs(f32, pow(f32, 0.0, 3.3), 0.0, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 0.8923, 3.3), 0.686572, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 0.2, 3.3), 0.004936, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 1.5, 3.3), 3.811546, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 37.45, 3.3), 155736.703125, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, 89.123, 3.3), 2722489.5, epsilon));
    try expect(math.approxEqAbs(f32, pow(f32, -1.0, 1e10), 1.0, epsilon));

    try expect(math.approxEqAbs(f64, pow(f64, 0.0, 3.3), 0.0, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 0.8923, 3.3), 0.686572, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 0.2, 3.3), 0.004936, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 1.5, 3.3), 3.811546, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 37.45, 3.3), 155736.7160616, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, 89.123, 3.3), 2722490.231436, epsilon));
    try expect(math.approxEqAbs(f64, pow(f64, -1.0, 1e20), 1.0, epsilon));
}

test "special" {
    // 对任意 x,pow(x, +-0) = 1
    try expect(pow(f32, 4, 0.0) == 1.0);
    try expect(pow(f32, 7, -0.0) == 1.0);
    try expect(pow(f32, math.nan(f32), -0.0) == 1.0);
    // 任意 y 均有 pow(1, y) = 1
    try expect(pow(f32, 1.0, 4) == 1.0);
    try expect(pow(f32, 1.0, 7) == 1.0);
    try expect(pow(f32, 1.0, -math.inf(f32)) == 1.0);
    try expect(pow(f32, 1.0, math.nan(f32)) == 1.0);
    // 对任意 x,pow(x, 1) = x
    try expect(pow(f32, 45, 1.0) == 45);
    try expect(pow(f32, -45, 1.0) == -45);
    try expect(math.isPositiveZero(pow(f32, 0.0, 1.0)));
    try expect(math.isNegativeZero(pow(f32, -0.0, 1.0)));
    try expect(math.isPositiveInf(pow(f32, math.inf(f32), 1.0)));
    try expect(math.isNan(pow(f32, math.nan(f32), 1.0)));
    // 对任意 y != 0,pow(nan, y) = nan
    try expect(math.isNan(pow(f32, math.nan(f32), 5.0)));
    // 对任意 x != 1,pow(x, nan) = nan
    try expect(math.isNan(pow(f32, 5.0, math.nan(f32))));
    // pow(+-0, y)    = +-inf,当 y 为小于 0 的奇整数时
    try expect(math.isPositiveInf(pow(f32, 0.0, -1.0)));
    try expect(math.isNegativeInf(pow(f32, -0.0, -5.0)));
    // pow(+-0, -inf) = +inf
    try expect(math.isPositiveInf(pow(f32, 0.0, -math.inf(f32))));
    try expect(math.isPositiveInf(pow(f32, -0.0, -math.inf(f32))));
    // pow(+-0, +inf) = +0
    try expect(math.isPositiveZero(pow(f32, 0.0, math.inf(f32))));
    try expect(math.isPositiveZero(pow(f32, -0.0, math.inf(f32))));
    // pow(+-0, y)    = +inf,当有限 y < 0 且不是奇整数时
    try expect(math.isPositiveInf(pow(f32, 0.0, -2.0)));
    try expect(math.isPositiveInf(pow(f32, -0.0, -2.0)));
    try expect(math.isPositiveInf(pow(f32, 0.0, -5.2)));
    try expect(math.isPositiveInf(pow(f32, -0.0, -0.5)));
    // pow(+-0, y)    = +-0,当有限 y 为大于 0 的奇整数时
    try expect(math.isPositiveZero(pow(f32, 0.0, 3.0)));
    try expect(math.isNegativeZero(pow(f32, -0.0, 5.0)));
    // pow(+-0, y)    = +0,当有限 y > 0 且不是奇整数时
    try expect(math.isPositiveZero(pow(f32, 0.0, 2.0)));
    try expect(math.isPositiveZero(pow(f32, -0.0, 2.0)));
    try expect(math.isPositiveZero(pow(f32, 0.0, 5.2)));
    try expect(math.isPositiveZero(pow(f32, -0.0, 0.5)));
    // pow(-1, +-inf) = 1
    try expect(pow(f32, -1.0, math.inf(f32)) == 1.0);
    try expect(pow(f32, -1.0, -math.inf(f32)) == 1.0);
    // 对 |x| > 1,pow(x, +inf) = +inf
    try expect(math.isPositiveInf(pow(f32, 1.2, math.inf(f32))));
    try expect(math.isPositiveInf(pow(f32, -1.2, math.inf(f32))));
    // 当 |x| > 1 时,pow(x, -inf) = +0
    try expect(math.isPositiveZero(pow(f32, 1.2, -math.inf(f32))));
    try expect(math.isPositiveZero(pow(f32, -1.2, -math.inf(f32))));
    // pow(x, +inf)   = +0 for |x| < 1
    try expect(math.isPositiveZero(pow(f32, 0.2, math.inf(f32))));
    try expect(math.isPositiveZero(pow(f32, -0.2, math.inf(f32))));
    // |x| < 1 时,pow(x, -inf) = +inf
    try expect(math.isPositiveInf(pow(f32, 0.2, -math.inf(f32))));
    try expect(math.isPositiveInf(pow(f32, -0.2, -math.inf(f32))));
    // 当 y > 0 时,pow(+inf, y) = +inf
    try expect(math.isPositiveInf(pow(f32, math.inf(f32), 2.0)));
    try expect(math.isPositiveInf(pow(f32, math.inf(f32), 0.2)));
    // 当 y < 0 时,pow(+inf, y) = +0
    try expect(math.isPositiveZero(pow(f32, math.inf(f32), -2.0)));
    try expect(math.isPositiveZero(pow(f32, math.inf(f32), -0.2)));
    // 当 y 为大于 0 的奇整数时,pow(-inf, y) = -inf
    try expect(math.isNegativeInf(pow(f32, -math.inf(f32), 5.0)));
    // pow(-inf, +inf) = +inf
    try expect(math.isPositiveInf(pow(f32, -math.inf(f32), math.inf(f32))));
    // pow(-inf, -inf) = +0
    try expect(math.isPositiveZero(pow(f32, -math.inf(f32), -math.inf(f32))));
    // pow(-inf, y) = +inf,当有限值 y > 0 且不为奇整数时。
    try expect(math.isPositiveInf(pow(f32, -math.inf(f32), 4.0)));
    try expect(math.isPositiveInf(pow(f32, -math.inf(f32), 0.5)));
    // 当有限的 y 是小于 0 的奇整数时,pow(-inf, y) = -0
    try expect(math.isNegativeZero(pow(f32, -math.inf(f32), -3.0)));
    // pow(-inf, y) = +0,当有限值 y < 0 且不为奇整数时。
    try expect(math.isPositiveZero(pow(f32, -math.inf(f32), -2.0)));
    try expect(math.isPositiveZero(pow(f32, -math.inf(f32), -5.2)));
    // pow(x, y)      = nan,当有限 x < 0 且 y 为有限非整数时
    try expect(math.isNan(pow(f32, -1.0, 1.2)));
    try expect(math.isNan(pow(f32, -12.4, -78.5)));
}

test "overflow" {
    try expect(math.isPositiveInf(pow(f64, 2, 1 << 32)));
    try expect(pow(f64, 2, -(1 << 32)) == 0);
    try expect(math.isNegativeInf(pow(f64, -2, (1 << 32) + 1)));
    try expect(pow(f64, 0.5, 1 << 45) == 0);
    try expect(math.isPositiveInf(pow(f64, 0.5, -(1 << 45))));
    try expect(math.isPositiveInf(pow(f64, -2, 1 << 64)));
    try expect(pow(f64, 0.5, 1 << 64) == 0);
}