From: Josef 'Jupp' Schugt Date: 2011-05-12T21:25:30+09:00 Subject: Re: Math cube root On Wed, 11 May 2011 21:45:42 +0200, Sergey Avseyev wrote: > How can you explain this: > > $ irb > 1.9.2p180 (main):001:0> 1000 ** (1.0/3) > 9.999999999999998 > 1.9.2p180 (main):002:0> Math.sqrt(100) > 10.0 Floating-point numbers have a finite precision. As a result, the outcome of a numerical (computer) calculation usually differs from the outcome of the mathematical calculation. Assume you only can operate with integers and want to compute the square root of 133. You may then end up with either 11 (11² = 121) or 12 (12² = 144) while the actual result is approximately 11.5 (11.5² = 121 + 11 + 0.25 = 132.25; more precisely 11.5325625947). You may like to use formatted output of numbers that suppresses digits beyond the actual precision: jupp@pen2:~ $ irb irb(main):001:0> "%.15f" % 1000 ** (1.0/3) => "9.999999999999998" irb(main):002:0> "%.14f" % 1000 ** (1.0/3) => "10.00000000000000" The above example turns the numerical value into a string displaying a fractional part with 15 and 14 digits, respectively. Assuming that IEEE 754 double precision floating point numbers (i.e. those used by Ruby) have a precision of a little less than 16 (decimal) digits it is safe to assume that the complexity of operation you perform results in a value that is precise to a little less than 15 digits - which means that you can assume 14 digits to be correct. By chance it MAY be precise to more digits as it is the case for Math.sqrt(100) - but that is nothing you can rely on unless you learn some gory details of numerical mathematics. HTH