Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4290

Re: Math cube root

From Jeremy Bopp <jeremy@bopp.net>
Newsgroups comp.lang.ruby
Subject Re: Math cube root
Date 2011-05-11 15:06 -0500
Organization Service de news de lacave.net
Message-ID <4DCAEC4C.6080900@bopp.net> (permalink)
References <52af0ed0252adbd3445f639fbff6282f@ruby-forum.com> <dbc7278d5a23fce616550d07283c1da1@ruby-forum.com>

Show all headers | View raw


On 5/11/2011 14:45, 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

You're using floating point arithmetic which is always inexact.  The
1.0/3 part cannot be represented with infinite precision, so it's
basically rounded at a certain point.  The result is then used for the
rest of the operation, which may compound the inaccuracy introduced by
the initial rounding.

If you must use floating point operations, be prepared to accept results
that are only *close* to what you expect, where close is largely
dependent on the operations being performed.

http://en.wikipedia.org/wiki/Floating_point

-Jeremy

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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