How to calculate the square root of a number in Ruby?

I'm a passionate software developer.
Search for a command to run...

I'm a passionate software developer.
No comments yet. Be the first to comment.
Components Introduction In order to truly understand props in React, we first need to be clear on what components are. A component is a reusable piece of the user interface that encapsulates structure, styling, and behavior. Instead of repeating the ...

Introduction Context By default, Codespace is using the GITHUB_TOKEN env var (the “Codespaces token”), which can’t create repos. It’s necessary to authenticate gh with a token/account that has the right scopes, or create the repo on the web first. Ob...

Solution Here it is: git commit --allow-empty-message -m "" Explanation To create a Git commit without a message, or with an empty message, you can use the --allow-empty-message flag with the git commit command. git commit --allow-empty-message -m "...

Steps Cleanup phase This phase’s goal is to removes existing containers, volumes, networks and images Stop and remove volumes docker compose down --volumes --remove-orphans The previous command stops and removes containers, networks, and default volu...

Steps Stop the service It’s not mandatory, but before uninstalling, it's a good practice to stop any running Docker services. sudo systemctl stop docker Remove docker packages It’s used to completely and forcefully remove a list of specified packag...

Before we proceed, it's crucial to note that calculating the square root of a number from scratch is indeed possible, although it's not the focus of this post.
Instead, we'll explore how to efficiently calculate the square root using the sqrt method, which is available in both the Math module and the Integer class.
To calculate the square root, you have two options:
Integer.sqrt(number)
Math.sqrt(number)
Integer class
Integer.sqrt(number)
Math module
Math.sqrt(number)
Both Integer.sqrt() and Math.sqrt() require a non-negative number as a parameter. It's essential to be aware that the Math module treats the parameter as a float type, while the Integer class considers it an integer type.
If the parameter passed to Integer.sqrt() is not an integer (e.g., a float or a string), it will be converted to an integer before the square root is calculated. Similarly, for the Math.sqrt() method, the parameter is converted to a float first.
However, when dealing with string parameters, complications arise. If the parameter is a string, a TypeError will be raised.
It's crucial to handle such scenarios to avoid unexpected errors in your code.
A TypeError is returned if the parameter value is a String.
irb(main):006:0> Integer.sqrt('4')
Traceback (most recent call last):
6: from /usr/local/bin/irb:23:in `<main>'
5: from /usr/local/bin/irb:23:in `load'
4: from /var/lib/gems/2.5.0/gems/irb-1.1.0/exe/irb:11:in `<top (required)>'
3: from (irb):5
2: from (irb):6:in `rescue in irb_binding'
1: from (irb):6:in `sqrt'
TypeError (no implicit conversion of String into Integer)
irb(main):007:0> Math.sqrt('4.0')
Traceback (most recent call last):
6: from /usr/local/bin/irb:23:in `<main>'
5: from /usr/local/bin/irb:23:in `load'
4: from /var/lib/gems/2.5.0/gems/irb-1.1.0/exe/irb:11:in `<top (required)>'
3: from (irb):6
2: from (irb):7:in `rescue in irb_binding'
1: from (irb):7:in `sqrt'
TypeError (can't convert String into Float)
A Math::DomainError is returned if a negative value is used as a parameter.
irb(main):001:0> Integer.sqrt(-2)
Traceback (most recent call last):
5: from /usr/local/bin/irb:23:in `<main>'
4: from /usr/local/bin/irb:23:in `load'
3: from /var/lib/gems/2.5.0/gems/irb-1.1.0/exe/irb:11:in `<top (required)>'
2: from (irb):1
1: from (irb):1:in `sqrt'
Math::DomainError (Numerical argument is out of domain - "isqrt")
irb(main):002:0> Math.sqrt(-2)
Traceback (most recent call last):
6: from /usr/local/bin/irb:23:in `<main>'
5: from /usr/local/bin/irb:23:in `load'
4: from /var/lib/gems/2.5.0/gems/irb-1.1.0/exe/irb:11:in `<top (required)>'
3: from (irb):1
2: from (irb):2:in `rescue in irb_binding'
1: from (irb):2:in `sqrt'
Math::DomainError (Numerical argument is out of domain - "sqrt")
An ArgumentError is returned if the given number of parameters is different than 1, since it is expected 1 parameter.
irb(main):010:0> Integer.sqrt
Traceback (most recent call last):
6: from /usr/local/bin/irb:23:in `<main>'
5: from /usr/local/bin/irb:23:in `load'
4: from /var/lib/gems/2.5.0/gems/irb-1.1.0/exe/irb:11:in `<top (required)>'
3: from (irb):9
2: from (irb):10:in `rescue in irb_binding'
1: from (irb):10:in `sqrt'
ArgumentError (wrong number of arguments (given 0, expected 1))
irb(main):011:0> Math.sqrt
Traceback (most recent call last):
6: from /usr/local/bin/irb:23:in `<main>'
5: from /usr/local/bin/irb:23:in `load'
4: from /var/lib/gems/2.5.0/gems/irb-1.1.0/exe/irb:11:in `<top (required)>'
3: from (irb):10
2: from (irb):11:in `rescue in irb_binding'
1: from (irb):11:in `sqrt'
ArgumentError (wrong number of arguments (given 0, expected 1))
The Integer.sqrt() method returns the integer square root of the non-negative integer parameter.
For example, the largest non-negative integer square root of 5 is 2. This method specifically returns the largest integer number.
irb(main):001:0> Integer.sqrt(1)
=> 1
irb(main):002:0> Integer.sqrt(2)
=> 1
irb(main):003:0> Integer.sqrt(3)
=> 1
irb(main):004:0> Integer.sqrt(4)
=> 2
irb(main):005:0> Integer.sqrt(5)
=> 2
The square root of 2 is 1.4142135623730951, but since the Integer.sqrt() method specifically deals with integers, it returns the largest integer, which is 1.
On the other hand, the Math.sqrt() method is capable of returning a float value.
irb(main):006:0> Math.sqrt(1)
=> 1.0
irb(main):007:0> Math.sqrt(2)
=> 1.4142135623730951
irb(main):008:0> Math.sqrt(3)
=> 1.7320508075688772
irb(main):009:0> Math.sqrt(4)
=> 2.0
Here, Math.sqrt() provides the accurate square root, including decimal values.
In summary, when precision with decimal values is required, the Math.sqrt() method is the preferable choice.
If you specifically need the largest integer result, Integer.sqrt() is suitable. Feel free to reach out if you have any questions or further inquiries.

Thank you for reading this article.
If you have any questions, thoughts, suggestions, or corrections, please share them with us.
We appreciate your feedback and look forward to hearing from you.
Feel free to suggest topics for future blog articles. Until next time!