Ruby Tutorial Ruby References

Ruby - Math.hypot() Method



The Ruby Math.hypot() method returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2).

Syntax

Math.hypot(x, y)

Parameters

x Specify a value.
y Specify a value.

Return Value

Returns sqrt(x2 +y2).

Example:

In the example below, Math.hypot() method is used to calculate sqrt(x2 +y2).

puts "Math.hypot(3, 4) = #{Math.hypot(3, 4)}" 
puts "Math.hypot(5, 12) = #{Math.hypot(5, 12)}" 
puts "Math.hypot(8, 15) = #{Math.hypot(8, 15)}" 
puts "Math.hypot(-20, 15) = #{Math.hypot(-20, 15)}" 
puts "Math.hypot(20, -15) = #{Math.hypot(20, -15)}" 

The output of the above code will be:

Math.hypot(3, 4) = 5.0
Math.hypot(5, 12) = 13.0
Math.hypot(8, 15) = 17.0
Math.hypot(-20, 15) = 25.0
Math.hypot(20, -15) = 25.0

❮ Ruby Math Module

5