What is hypot() in Ruby?

The hypot() function in Ruby returns the hypotenuse of a right-angled triangle from its length and base, which are sent as parameters.

The mathematical formula to calculate the hypotenuse from length and base is as follows:

hypotenuse = sqrt( length^2 + base^2 )

Figure 1 shows the visual representation of the hypot() function.

Figure 1: Visual representation of hypot() function

This function requires the Math module.

Syntax

hypot(length, base)

Parameters

This function requires length and base as parameters.

Return value

This function returns the hypotenuse of the right-angled triangle from its length and base, which are sent as parameters.

Example

# the hypotenuse of right angled triangle
# length=10
# base=10
print "The hypotenuse of triangle with length = 10 and base = 10: ",Math.hypot(10,10)," \n"
# length=4
# base=6
print "The hypotenuse of triangle with length = 4 and base = 6: ",Math.hypot(4,6)," \n"
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources