Java Math hypot() 方法
示例
获取二维点 (x, y) 到原点 (0, 0) 的距离
System.out.println(Math.hypot(3, 4));
System.out.println(Math.hypot(1, 1));
System.out.println(Math.hypot(1, 10));
定义和用法
hypot()
方法返回直角三角形斜边的长度,这等同于二维点 (x, y) 到原点 (0, 0) 的距离。
此方法返回一个等于 Math.sqrt(x * x + y * y)
的值,但它经过优化,可以防止在中间操作(如加法和乘法)中导致的溢出和下溢。
语法
public static double hypot(double x, double y)
参数值
参数 | 描述 |
---|---|
x | 必需。点的 x 坐标。 |
y | 必需。点的 y 坐标。 |
技术详情
返回 | 一个 double 值,表示点 (x, y) 到原点 (0, 0) 的距离。 |
---|---|
Java 版本 | 1.5+ |
❮ Math 方法