Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
math
sqrt
communitycreator

What is Math.sqrt() in Ruby?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The sqrt() function in Ruby returns the square root of a non-negative number.

Figure 1 shows the mathematical representation of the sqrt() function.

Figure 1: Mathematical representation of the sqrt() function

Syntax

sqrt(number)

Parameter

sqrt() requires a non-negative number as a parameter. If the number is negative, a DomainError will be thrown.

Return value

sqrt() returns the square root of the number sent as a parameter.

Example

print "sqrt(4):", Math.sqrt(4), "\n"
print "sqrt(0):", Math.sqrt(0), "\n"
print "sqrt(25):", Math.sqrt(25), "\n"
print "sqrt(4.5):", Math.sqrt(4.5), "\n"

RELATED TAGS

ruby
math
sqrt
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring