Ruby Tutorial Ruby References

Ruby - Math.sqrt() Method



The Ruby Math.sqrt() method returns the square root of the given number.

Syntax

Math.sqrt(x)

Parameters

x Specify the positive number.

Return Value

Returns the square root of the specified number.
If the x is negative, DomainError is thrown.

Example:

In the example below, Math.sqrt() method is used to find out the square root of the given number.

puts "Math.sqrt(9) = #{Math.sqrt(9)}"
puts "Math.sqrt(64) = #{Math.sqrt(64)}"
puts "Math.sqrt(100) = #{Math.sqrt(100)}"
puts "Math.sqrt(500) = #{Math.sqrt(500)}"

The output of the above code will be:

Math.sqrt(9) = 3.0
Math.sqrt(64) = 8.0
Math.sqrt(100) = 10.0
Math.sqrt(500) = 22.360679774997898

❮ Ruby Math Module

5