Java Math hypot() 方法
定义和用法
hypot()
方法返回直角三角形的斜边长度,这相当于二维点 (x, y) 与原点 (0, 0) 之间的距离。
此方法返回的值等于 Math.sqrt(x * x + y * y)
,但它进行了优化,以防止在加法和乘法等中间操作中引起的溢出和下溢。
实例
获取二维点 (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));
语法
public static double hypot(double x, double y)
参数
参数 | 描述 |
---|---|
x | 必需。点的 x 坐标。 |
y | 必需。点的 y 坐标。 |
技术细节
返回: | double 值,表示点 (x, y) 与原点 (0, 0) 之间的距离。 |
---|---|
Java 版本: | 1.5+ |