bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Mar-31 10:30 UTC
strange behavior of while loop
hai all, I am very new to Ruby and when I am doing some small program I found the strange output for my program. Please let me know why this is happening.... num = 10.1 while num < 10.5 puts "num = " + num.to_s num = num + 0.1 end output that I am getting is : num = 10.1 num = 10.2 num = 10.3 num = 10.4 num = 10.5 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> num = 10.1 > while num < 10.5 > puts "num = " + num.to_s > num = num + 0.1 > end > > output that I am getting is : > num = 10.1 > num = 10.2 > num = 10.3 > num = 10.4 > num = 10.5Floating point numbers are not represented exactly. Google for "floating point accuracy" and I''m sure you''ll find lots of information about this. If you need exactly tenths, then use integers at ten times the size and divide by ten when displaying: num = 101 while num < 105 puts "num = #{num/10.0}" num += 1 end num = 10.1 num = 10.2 num = 10.3 num = 10.4 Note you need to divide by 10.0 to get a float as the result as otherwise Ruby would do integer division. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could also use BigDecimal instead of float: num = BigDecimal("10.1") while num < BigDecimal("10.5") do puts "num = #{num.to_s}" num += BigDecimal("0.1") end On Mar 31, 7:41 am, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> bramu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > num = 10.1 > > while num < 10.5 > > puts "num = " + num.to_s > > num = num + 0.1 > > end > > > output that I am getting is : > > num = 10.1 > > num = 10.2 > > num = 10.3 > > num = 10.4 > > num = 10.5 > > Floating point numbers are not represented exactly. Google for > "floating point accuracy" and I''m sure you''ll find lots of information > about this. If you need exactly tenths, then use integers at ten times > the size and divide by ten when displaying: > > num = 101 > while num < 105 > puts "num = #{num/10.0}" > num += 1 > end > > num = 10.1 > num = 10.2 > num = 10.3 > num = 10.4 > > Note you need to divide by 10.0 to get a float as the result as > otherwise Ruby would do integer division. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Float rounding errors >> num = 10.1 => 10.1 >> num += 0.1 => 10.2 >> num += 0.1 => 10.3 >> num += 0.1 => 10.4 >> num += 0.1 => 10.5 >> num < 10.5 => true >> num => 10.5 http://www.regdeveloper.co.uk/2006/08/12/floating_point_approximation/ http://en.wikipedia.org/wiki/Floating_point Julian. On 31/03/2008, at 9:30 PM, bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > hai all, > > I am very new to Ruby and when I am doing some small program I > found the strange output for my program. Please let me know why this > is happening.... > > num = 10.1 > > while num < 10.5 > puts "num = " + num.to_s > num = num + 0.1 > end > > output that I am getting is : > > num = 10.1 > num = 10.2 > num = 10.3 > num = 10.4 > num = 10.5 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This is even more fun ;-) >> (10.1 * 1000).to_i => 10100 >> ((10.1 + 0.1) * 1000).to_i => 10200 >> ((10.1 + 0.1 + 0.1) * 1000).to_i => 10299 Julian. On 31/03/2008, at 9:30 PM, bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > hai all, > > I am very new to Ruby and when I am doing some small program I > found the strange output for my program. Please let me know why this > is happening.... > > num = 10.1 > > while num < 10.5 > puts "num = " + num.to_s > num = num + 0.1 > end > > output that I am getting is : > > num = 10.1 > num = 10.2 > num = 10.3 > num = 10.4 > num = 10.5 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 31 Mar 2008, at 14:20, Julian Leviston wrote:> > > http://www.regdeveloper.co.uk/2006/08/12/floating_point_approximation/ > http://en.wikipedia.org/wiki/Floating_point >http://docs.sun.com/source/806-3568/ncg_goldberg.html is one of the classics on this subject. Fred> Julian. > > > On 31/03/2008, at 9:30 PM, bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > >> >> hai all, >> >> I am very new to Ruby and when I am doing some small program I >> found the strange output for my program. Please let me know why this >> is happening.... >> >> num = 10.1 >> >> while num < 10.5 >> puts "num = " + num.to_s >> num = num + 0.1 >> end >> >> output that I am getting is : >> >> num = 10.1 >> num = 10.2 >> num = 10.3 >> num = 10.4 >> num = 10.5 >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---