Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4349
| From | Josef 'Jupp' Schugt <jupp@gmx.de> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Math cube root |
| Date | 2011-05-12 07:25 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <op.vvdbgayy6p3xzb@pen2> (permalink) |
| References | <52af0ed0252adbd3445f639fbff6282f@ruby-forum.com> <dbc7278d5a23fce616550d07283c1da1@ruby-forum.com> |
On Wed, 11 May 2011 21:45:42 +0200, Sergey Avseyev <sergey.avseyev@gmail.com> 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
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Math cube root Sergey Avseyev <sergey.avseyev@gmail.com> - 2011-05-11 14:45 -0500
Re: Math cube root Jeremy Bopp <jeremy@bopp.net> - 2011-05-11 15:06 -0500
Re: Math cube root Josef 'Jupp' Schugt <jupp@gmx.de> - 2011-05-12 07:25 -0500
Re: Math cube root serialhex <serialhex@gmail.com> - 2011-05-12 08:54 -0500
Re: Math cube root Colin Bartlett <colinb2r@googlemail.com> - 2011-05-12 10:26 -0500
Re: Math cube root Josh Cheek <josh.cheek@gmail.com> - 2011-05-12 13:30 -0500
Re: Math cube root serialhex <serialhex@gmail.com> - 2011-05-12 13:46 -0500
Re: Math cube root Rob Biedenharn <Rob@AgileConsultingLLC.com> - 2011-05-12 13:53 -0500
Re: Math cube root jzakiya <jzakiya@gmail.com> - 2011-05-12 13:20 -0700
Re: Math cube root serialhex <serialhex@gmail.com> - 2011-05-12 15:36 -0500
Re: Math cube root Martin DeMello <martindemello@gmail.com> - 2011-05-14 13:12 -0500
csiph-web