QP School

Full Version: Getting accurate results when working with integers in Ruby
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
After reading the official Ruby 3.0.0 documentation I have discovered that there are 2 ways to get the square root of a number.
When working with small numbers, both options return the same results,
but when the numbers get bigger the results of Option 2 may be inaccurate as follows:

Option 1:


Quote:Num1 = Integer.sqrt(10**46)
puts Num1


This will put exactly 100000000000000000000000 on the screen.

Option 2:


Quote:Num1 = Math.sqrt(10**46).floor
puts Num1


This will put 99999999999999991611392 on the screen.