From: Josh Cheek Date: 2013-03-31T17:13:47+09:00 Subject: Re: Ruby Gotchas presentation slides --047d7b3438003ec61d04d93415d9 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Mar 31, 2013 at 2:14 AM, Julian Leviston wrote: > On 31/03/2013, at 5:36 PM, Josh Cheek wrote: > > Might also add floating point number imprecision. > $ ruby -e 'p 7.01 - 7' > 0.009999999999999787 > > > Really? That's got nothing to do with Ruby! That's computers! :) > > Julian. > It's IEEE floating point math. It does have to do with Ruby, Ruby could have implemented literal numbers with decimal points using Rational or BigDecimal instead of Float. Both are in the stdlib (plus, there are gems like flt) require 'bigdecimal' require 'bigdecimal/util' ('7.01'.to_f - '7.00'.to_f) # => 0.009999999999999787 ('7.01'.to_r - '7.00'.to_r) # => (1/100) ('7.01'.to_d - '7.00'.to_d).to_digits # => "0.01" --047d7b3438003ec61d04d93415d9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Sun, Mar 31, 2013 at 2:14 AM, Julian Leviston <julian@coretech.net.au> wrote:

It's IEEE floating point math.


=
require 'bigdecimal'
require 'bigdecimal/util= 9;
('7.01'.to_f - '7.00'.to_f) =A0 =A0 =A0 =A0 = =A0 # =3D> 0.009999999999999787
('7.01'.to_r - '7.00'.to_r) =A0 =A0 =A0 =A0 =A0 # =3D&= gt; (1/100)
('7.01'.to_d - '7.00'.to_d).to_digits= # =3D> "0.01"

--047d7b3438003ec61d04d93415d9--