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

第 1 行

std.math.hypot.emulateFma

函数

第 54 行

inline fn emulateFma(comptime T: type) bool

参数

T: type

std.math.hypot.expect

别名

第 3 行

别名目标 std.testing.expect

std.math.hypot.floatEps

别名

第 8 行

别名目标 std.math.floatEps

std.math.hypot.floatMax

别名

第 10 行

别名目标 std.math.floatMax

std.math.hypot.floatMin

别名

第 9 行

别名目标 std.math.floatMin

std.math.hypot.floatTrueMin

别名

第 11 行

别名目标 std.math.floatTrueMin

std.math.hypot.hypot

函数

第 23 行

返回 sqrt(x * x + y * y),避免不必要的上溢和下溢。

特殊情况:

x y hypot
+-inf any +inf
any +-inf +inf
nan fin nan
fin nan nan
pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y)

用法示例

test hypot {
    try expect(hypot(0.3, 0.4) == 0.5);
}

std.math.hypot.hypot_test_cases

常量

第 84 行

std.math.hypot.hypotFused

函数

第 63 行

inline fn hypotFused(comptime F: type, x: F, y: F) F

参数

F: type
x: F
y: F

std.math.hypot.hypotUnfused

函数

第 71 行

inline fn hypotUnfused(comptime F: type, x: F, y: F) F

参数

F: type
x: F
y: F

std.math.hypot.inf

别名

第 6 行

别名目标 std.math.inf

std.math.hypot.isInf

别名

第 5 行

别名目标 std.math.isInf

std.math.hypot.isNan

别名

第 4 行

别名目标 std.math.isNan

std.math.hypot.math

别名

第 2 行

别名目标 std.math

std.math.hypot.nan

别名

第 7 行

别名目标 std.math.nan

std.math.hypot.std

别名

第 1 行

别名目标 std

源码

std/math/hypot.zig

const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;
const isNan = math.isNan;
const isInf = math.isInf;
const inf = math.inf;
const nan = math.nan;
const floatEps = math.floatEps;
const floatMin = math.floatMin;
const floatMax = math.floatMax;
const floatTrueMin = math.floatTrueMin;

/// 返回 sqrt(x * x + y * y),避免不必要的上溢和下溢。
///
/// 特殊情况:
///
/// |   x   |   y   | hypot |
/// |-------|-------|-------|
/// | +-inf |  any  | +inf  |
/// |  any  | +-inf | +inf  |
/// |  nan  |  fin  |  nan  |
/// |  fin  |  nan  |  nan  |
pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y) {
    const T = @TypeOf(x, y);
    switch (@typeInfo(T)) {
        .float => {},
        .comptime_float => return @sqrt(x * x + y * y),
        else => @compileError("hypot not implemented for " ++ @typeName(T)),
    }
    const lower = @sqrt(floatMin(T));
    const upper = @sqrt(floatMax(T) / 2);
    const scale = floatTrueMin(T) * upper;
    const hypfn = if (emulateFma(T)) hypotUnfused else hypotFused;
    var major: T = x;
    var minor: T = y;
    if (isInf(major) or isInf(minor)) return inf(T);
    if (isNan(major) or isNan(minor)) return nan(T);
    if (T == f16) return @floatCast(@sqrt(@mulAdd(f32, x, x, @as(f32, y) * y)));
    if (T == f32) return @floatCast(@sqrt(@mulAdd(f64, x, x, @as(f64, y) * y)));
    major = @abs(major);
    minor = @abs(minor);
    if (minor > major) {
        const tempo = major;
        major = minor;
        minor = tempo;
    }
    if (minor == 0.0) return major;
    if (major - minor == major) return major;
    if (major > upper) return hypfn(T, major * scale, minor * scale) / scale;
    if (minor < lower) return hypfn(T, major / scale, minor / scale) * scale;
    return hypfn(T, major, minor);
}

inline fn emulateFma(comptime T: type) bool {
    // 如果 @mulAdd 降低为软件实现,
    // 应使用 hypotUnfused 替代 hypotFused。
    // 此处作有依据的估计,但理想情况下,
    // 应在编译时准确检测
    // 是否会采用该回退。
    return (T == f128 or T == f80);
}

inline fn hypotFused(comptime F: type, x: F, y: F) F {
    const r = @sqrt(@mulAdd(F, x, x, y * y));
    const rr = r * r;
    const xx = x * x;
    const z = @mulAdd(F, -y, y, rr - xx) + @mulAdd(F, r, r, -rr) - @mulAdd(F, x, x, -xx);
    return r - z / (2 * r);
}

inline fn hypotUnfused(comptime F: type, x: F, y: F) F {
    const r = @sqrt(x * x + y * y);
    if (r <= 2 * y) { // 30 度或更陡
        const dx = r - y;
        const z = x * (2 * dx - x) + (dx - 2 * (x - y)) * dx;
        return r - z / (2 * r);
    } else { // 小于 30 度
        const dy = r - x;
        const z = 2 * dy * (x - 2 * y) + (4 * dy - y) * y + dy * dy;
        return r - z / (2 * r);
    }
}

const hypot_test_cases = .{
    .{ 0.0, -1.2, 1.2 },
    .{ 0.2, -0.34, 0.3944616584663203993612799816649560759946493601889826495362 },
    .{ 0.8923, 2.636890, 2.7837722899152509525110650481670176852603253522923737962880 },
    .{ 1.5, 5.25, 5.4600824169603887033229768686452745953332522619323580787836 },
    .{ 37.45, 159.835, 164.16372840856167640478217141034363907565754072954443805164 },
    .{ 89.123, 382.028905, 392.28687638576315875933966414927490685367196874260165618371 },
    .{ 123123.234375, 529428.707813, 543556.88524707706887251269205923830745438413088753096759371 },
};

test hypot {
    try expect(hypot(0.3, 0.4) == 0.5);
}

test "hypot.correct" {
    inline for (.{ f16, f32, f64, f128 }) |T| {
        inline for (hypot_test_cases) |v| {
            const a: T, const b: T, const c: T = v;
            try expect(math.approxEqRel(T, hypot(a, b), c, @sqrt(floatEps(T))));
        }
    }
}

test "hypot.precise" {
    inline for (.{ f16, f32, f64 }) |T| { // f128 似乎相差 5 ulp
        inline for (hypot_test_cases) |v| {
            const a: T, const b: T, const c: T = v;
            try expect(math.approxEqRel(T, hypot(a, b), c, floatEps(T)));
        }
    }
}

test "hypot.special" {
    @setEvalBranchQuota(2000);
    inline for (.{ f16, f32, f64, f128 }) |T| {
        try expect(math.isNan(hypot(nan(T), 0.0)));
        try expect(math.isNan(hypot(0.0, nan(T))));

        try expect(math.isPositiveInf(hypot(inf(T), 0.0)));
        try expect(math.isPositiveInf(hypot(0.0, inf(T))));
        try expect(math.isPositiveInf(hypot(inf(T), nan(T))));
        try expect(math.isPositiveInf(hypot(nan(T), inf(T))));

        try expect(math.isPositiveInf(hypot(-inf(T), 0.0)));
        try expect(math.isPositiveInf(hypot(0.0, -inf(T))));
        try expect(math.isPositiveInf(hypot(-inf(T), nan(T))));
        try expect(math.isPositiveInf(hypot(nan(T), -inf(T))));
    }
}