Java如何计算斜边的长度?
斜边是定义为计算直角三角形的斜边长度的数学函数。它旨在避免由于在计算机上执行的精度受限计算而引起的错误。
Math.hypot(doublex,doubley)返回sqrt(x2+y2),没有中间溢出或下溢。结果将与此计算相同Math.sqrt(Math.pow(x,2)+Math.pow(y,2))
package org.nhooo.example.math;
public class HypotExample {
public static void main(String[] args) {
double number1 = 3.0d;
double number2 = 5.0d;
// 计算总价值的平方根
// 数字1 ^ 2 +数字2 ^ 2
double sqr = Math.hypot(number1, number2);
System.out.println("Total value = " + (Math.pow(number1, 2) + Math.pow(number2, 2)));
System.out.println("Square root = " + sqr);
}
}