hypot
hypot(float $x, float $y): float
计算直角三角形斜边长(点 (x, y) 到原点距离)。数学上等价 sqrt(x²+y²),但内部先缩放再平方,中间值永不溢出——3e200/4e200 的极端对照是本条灵魂:naive 公式平方即 INF,hypot 仍得 5e200。
C C23
#include <math.h>
#include <stdio.h>
int main(void) {
// hypot(3):不溢出的 sqrt(x*x + y*y) 算法
printf("%.10f\n", hypot(3.0, 4.0));
// naive 公式会溢出:3e200 * 3e200 远超 double 上限(约 1.8e308)
double naive = sqrt(3e200 * 3e200 + 4e200 * 4e200);
printf("%d\n", isinf(naive) != 0 ? 1 : 0);
// hypot 内部先缩放,同一对极端值保持有限
printf("%.10f\n", hypot(3e200, 4e200) / 1e200);
printf("%d\n", isinf(hypot(3e200, 4e200)) != 0 ? 1 : 0);
return 0;
}
5.0000000000 1 5.0000000000 0
C++ C++26
#include <cmath>
#include <cstdio>
int main() {
// std::hypot:不溢出的 sqrt(x*x + y*y) 算法
std::printf("%.10f\n", std::hypot(3.0, 4.0));
// naive 公式会溢出:3e200 * 3e200 远超 double 上限(约 1.8e308)
double naive = std::sqrt(3e200 * 3e200 + 4e200 * 4e200);
std::printf("%d\n", std::isinf(naive) ? 1 : 0);
// hypot 内部先缩放,同一对极端值保持有限
std::printf("%.10f\n", std::hypot(3e200, 4e200) / 1e200);
std::printf("%d\n", std::isinf(std::hypot(3e200, 4e200)) ? 1 : 0);
return 0;
}
5.0000000000 1 5.0000000000 0
C# v11.0.100
// .NET 无 Math.Hypot:naive 公式在大输入下溢出,
// 稳健版先除以较大边缩放再平方
static double Hypot(double x, double y) {
x = Math.Abs(x);
y = Math.Abs(y);
double m = Math.Max(x, y);
if (m == 0.0) return 0.0;
double r = Math.Min(x, y) / m;
return m * Math.Sqrt(1.0 + r * r);
}
double naive = Math.Sqrt(3e200 * 3e200 + 4e200 * 4e200);
Console.WriteLine(Hypot(3, 4).ToString("F10"));
Console.WriteLine(double.IsInfinity(naive) ? 1 : 0);
Console.WriteLine((Hypot(3e200, 4e200) / 1e200).ToString("F10"));
Console.WriteLine(double.IsInfinity(Hypot(3e200, 4e200)) ? 1 : 0);
5.0000000000 1 5.0000000000 0
Go v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
// math.Hypot:不溢出的 sqrt(x*x + y*y) 算法
fmt.Printf("%.10f\n", math.Hypot(3, 4))
// naive 公式会溢出:3e200 * 3e200 远超 float64 上限(约 1.8e308)
//(放进变量让溢出发生在运行时而非编译期常量求值)
x, y := 3e200, 4e200
naive := math.Sqrt(x*x + y*y)
if math.IsInf(naive, 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
// hypot 内部先缩放,同一对极端值保持有限
fmt.Printf("%.10f\n", math.Hypot(x, y)/1e200)
if math.IsInf(math.Hypot(x, y), 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
5.0000000000 1 5.0000000000 0
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Math.hypot:不溢出的 sqrt(x*x + y*y) 算法
System.out.printf("%.10f%n", Math.hypot(3, 4));
// naive 公式会溢出:3e200 * 3e200 远超 double 上限(约 1.8e308)
double naive = Math.sqrt(3e200 * 3e200 + 4e200 * 4e200);
System.out.println(Double.isInfinite(naive) ? 1 : 0);
// hypot 内部先缩放,同一对极端值保持有限
System.out.printf("%.10f%n", Math.hypot(3e200, 4e200) / 1e200);
System.out.println(Double.isInfinite(Math.hypot(3e200, 4e200)) ? 1 : 0);
}
}
5.0000000000 1 5.0000000000 0
JavaScript v26.13.1
// Math.hypot:不溢出的 sqrt(x*x + y*y) 算法
console.log(Math.hypot(3, 4).toFixed(10));
// naive 公式会溢出:3e200 * 3e200 远超 double 上限(约 1.8e308)
const naive = Math.sqrt(3e200 * 3e200 + 4e200 * 4e200);
console.log(Number.isFinite(naive) ? 0 : 1);
// hypot 内部先缩放,同一对极端值保持有限
console.log((Math.hypot(3e200, 4e200) / 1e200).toFixed(10));
console.log(Number.isFinite(Math.hypot(3e200, 4e200)) ? 0 : 1);
5.0000000000 1 5.0000000000 0
PHP v8.6.0
// hypot():不溢出的 sqrt(x*x + y*y) 算法
printf("%.10f\n", hypot(3, 4));
// naive 公式会溢出:3e200 * 3e200 远超 float 上限(约 1.8e308)
$naive = sqrt(3e200 * 3e200 + 4e200 * 4e200);
printf("%d\n", is_infinite($naive) ? 1 : 0);
// hypot 内部先缩放,同一对极端值保持有限
printf("%.10f\n", hypot(3e200, 4e200) / 1e200);
printf("%d\n", is_infinite(hypot(3e200, 4e200)) ? 1 : 0);
5.0000000000 1 5.0000000000 0
Python v3.15.2
import math
# math.hypot:不溢出的 sqrt(x*x + y*y) 算法
print(f"{math.hypot(3, 4):.10f}")
# naive 公式会溢出:3e200 * 3e200 远超 float 上限(约 1.8e308)
naive = math.sqrt(3e200 * 3e200 + 4e200 * 4e200)
print(1 if math.isinf(naive) else 0)
# hypot 内部先缩放,同一对极端值保持有限
print(f"{math.hypot(3e200, 4e200) / 1e200:.10f}")
print(1 if math.isinf(math.hypot(3e200, 4e200)) else 0)
5.0000000000 1 5.0000000000 0
Rust v1.99.1
fn main() {
// f64::hypot:不溢出的 sqrt(x*x + y*y) 算法
println!("{:.10}", 3.0_f64.hypot(4.0));
// naive 公式会溢出:3e200 * 3e200 远超 f64::MAX(约 1.8e308)
let naive = (3e200_f64 * 3e200_f64 + 4e200_f64 * 4e200_f64).sqrt();
println!("{}", if naive.is_infinite() { 1 } else { 0 });
// hypot 内部先缩放,同一对极端值保持有限
let big = 3e200_f64.hypot(4e200);
println!("{:.10}", big / 1e200);
println!("{}", if big.is_infinite() { 1 } else { 0 });
}
5.0000000000 1 5.0000000000 0
TypeScript v26.13.1
// Math.hypot:不溢出的 sqrt(x*x + y*y) 算法
console.log(Math.hypot(3, 4).toFixed(10));
// naive 公式会溢出:3e200 * 3e200 远超 double 上限(约 1.8e308)
const naive: number = Math.sqrt(3e200 * 3e200 + 4e200 * 4e200);
console.log(Number.isFinite(naive) ? 0 : 1);
// hypot 内部先缩放,同一对极端值保持有限
console.log((Math.hypot(3e200, 4e200) / 1e200).toFixed(10));
console.log(Number.isFinite(Math.hypot(3e200, 4e200)) ? 0 : 1);
5.0000000000 1 5.0000000000 0
语言差异说明
PHP:hypot($x, $y) 内置;数学上等价 sqrt(x²+y²),但内部先按较大边缩放再平方,中间值永不溢出——本条灵魂是极端对照:naive 公式在 3e200/4e200 下平方即得 INF,hypot 仍精确返回 5e200(归一化除以 1e200 后输出 5.0000000000)。
C/C++:hypot(3)(POSIX 函数,非 ISO C;<math.h> 显式包含);INF 判定用 isinf(x) != 0——宏返回非零 int,勿写成 == 1。
Java:Math.hypot;C#:.NET 无内置 hypot——Math.Sqrt(x*x+y*y) 在 3e200/4e200 下溢出为 Infinity(本实测输出第 2 行的 1),示例手写稳健版(取 max/min 缩放)对照修复。
Python:math.hypot;JavaScript/TypeScript:Math.hypot;Go:math.Hypot;Rust:f64::hypot——10 门语言中唯 C# 需手写。
精度策略:极端值输出一律除以 1e200 归一化后 %.10f 限 10 位小数——跨平台/跨编译器 ULP 尾差(约 1e-16 相对量级)不影响前 10 位;INF 判定只依赖 isinf 系布尔派生,输出 1/0 环境无关。
3/4/5 常规值:各语言 hypot(3, 4) 恰为 5.0(双精度可精确表示),%.10f 下 10 门语言输出恒一致。
Go 细节:3e200*3e200 若写成常量表达式会在编译期报「constant overflows float64」,示例放入变量让溢出发生在运行时。
联动:sqrt 见 sqrt 条目;INF/NaN 判定族见 is_infinite / is_nan 条目。