Open In App

Ruby | Math hypot() function

Improve
Improve
Like Article
Like
Save
Share
Report

The hypot() function in Ruby returns sqrt(l^2 + b^2) when l and b are given as parameters. It eventually returns the hypotenuse of a right-angled triangle with sides l and 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.

Example 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)


Output:

5.0
18.439088914585774

Example 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)


Output:

11.180339887498949
22.825424421026653

Reference: https://devdocs.io/ruby~2.5/math#method-c-hypot


Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads