红宝石 |数学 hypot()函数
当 l 和 b 作为参数给出时,Ruby 中的hypot()函数返回sqrt(l^2 + b^2) 。它最终返回边为l和b的直角三角形的斜边。
Syntax: Math.hypot(l, b)
Parameter: The function takes two mandatory parameter l and b which specifies the length and the base.
Return Value: The function returns the hypotenuse of a right-angled triangle with sides l and b.
示例 1 :
# Ruby program for hypot() function
# Assigning values
l1 = 3
b1 = 4
l2 = 12
b2 = 14
# Prints the hypot() value
puts Math.hypot(l1, b1)
puts Math.hypot(l2, b2)
输出:
5.0
18.439088914585774
示例 2 :
# Ruby program for hypot() function
# Assigning values
l1 = 10
b1 = 5
l2 = 11
b2 = 20
# Prints the hypot() value
puts Math.hypot(l1, b1)
puts Math.hypot(l2, b2)
输出:
11.180339887498949
22.825424421026653
参考:https://devdocs.io/ruby~2.5/math#method-c-hypot