Forum: Ruby newbie question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6dc99607cfc06eed4636695a0d16531e?d=identicon&s=25 Will (Guest)
on 2008-10-08 00:29
(Received via mailing list)
Greetings all, I think this is generally more a programming question
than a
ruby explicit one, however, I am wondering why this code yields 23,
0.999999999999979 instead of 23, 1.

Any help would be great!

i = 0
while i <= 50
  a = 37*i / 50
  c = a + 0.0
  b = (37*i/50.0 - c ) *50
  if b < 1.5 && b > 0.5
    puts i
    puts b
  end
  i = i+1
end


thanks much
Will
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2008-10-08 00:47
(Received via mailing list)
Rounding errors in floating point arithmetic.

Farrel
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-10-08 01:10
(Received via mailing list)
Will wrote:
> Greetings all, I think this is generally more a programming question than a
> ruby explicit one, however, I am wondering why this code yields 23,
> 0.999999999999979 instead of 23, 1.
>

A common question. Check out
http://en.wikipedia.org/wiki/Floating_point.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-10-08 03:10
(Received via mailing list)
On Tue, Oct 7, 2008 at 5:27 PM, Will <wrsh07@gmail.com> wrote:
>  b = (37*i/50.0 - c ) *50
>  if b < 1.5 && b > 0.5
>    puts i
>    puts b
>  end
>  i = i+1
> end

require 'bigdecimal'

#put a bigdecimal in the numerator or denominator...
((BigDecimal("37") * 23 / 50 - 17) * 50).to_f
#=> 1.0

Todd
This topic is locked and can not be replied to.