till.vollmer-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2006-Sep-04 21:32 UTC
Maybe a Ruby question: Strange math!
irb(main):004:0> (19.9 * 100).to_i => 1989 irb(main):005:0> (19.9 * 100) => 1990.0 irb(main):006:0> 1990.0.to_i => 1990 Can someone explain this? Regards Till Vollmer --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Floating point number aren''t completely accurate. http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems On 04/09/06, till.vollmer-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <till.vollmer-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > > irb(main):004:0> (19.9 * 100).to_i > => 1989 > irb(main):005:0> (19.9 * 100) > => 1990.0 > irb(main):006:0> 1990.0.to_i > => 1990 > > Can someone explain this? > Regards > Till Vollmer > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
crazy stuff indeed. I understand (vaguely) the issue with FP numbers not being accurate, but I can''t wrap my head around this. I can''t seem to find any real discussion of ways to deal with this. Is the only workable solution to this issue to work exclusively with Integers? Eric --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
etlund wrote:> crazy stuff indeed. > > I understand (vaguely) the issue with FP numbers not being accurate, > but I can''t wrap my head around this. > > I can''t seem to find any real discussion of ways to deal with this. Is > the only workable solution to this issue to work exclusively with > Integers? > > EricYeah, for the simplicity you can use one integer for the integer part and one integer for the decimal part. Maybe there is a class around here on internet for this. Good luck for all of us -- 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 -~----------~----~----~----~------~----~------~--~---
etlund wrote:> crazy stuff indeed. > > I understand (vaguely) the issue with FP numbers not being accurate, > but I can''t wrap my head around this. > > I can''t seem to find any real discussion of ways to deal with this. Is > the only workable solution to this issue to work exclusively with > Integers? > > EricFloating point numbers can only approximate some integers. In your case, the final product of 19.9 * 100 is probably something like 1989.99999999999999999999998, which just displays as 1990.0 in most cases. Now, .to_i truncates off the decimal part pretty ruthlessly. To be safe, you should probably use a function like .round instead of .to_i unless you are absolutely certain you can do that safely. .round will round the number to the nearest integer, which is really what you want. _Kevin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/5/06, etlund <etlund-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > crazy stuff indeed. > > I understand (vaguely) the issue with FP numbers not being accurate, > but I can''t wrap my head around this. > > I can''t seem to find any real discussion of ways to deal with this. Is > the only workable solution to this issue to work exclusively with > Integers?Yup. It''s standard to deal with integers. If you look at the banking system then everything there is in whole numbers. On the computer level $55 is always represented as 5500. However that method may not work for your needs. BigDecimal seems to do a bunch. Never used it thought. 001:0> require ''bigdecimal'' 002:0> require ''bigdecimal/util'' 003:0> (19.9.to_d * 100).to_i => 1990 http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html -- -------------- Jon Gretar Borgthorsson http://www.jongretar.net/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 4, 2006, at 5:32 PM, till.vollmer-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> > irb(main):004:0> (19.9 * 100).to_i > => 1989 > irb(main):005:0> (19.9 * 100) > => 1990.0 > irb(main):006:0> 1990.0.to_i > => 1990 > > Can someone explain this?to_i truncates, so irb(main):001:0> 99.9.to_i => 99 You are probably expecting rounding rather than truncation: irb(main):002:0> 99.9.round => 100 Cheers, Bob> Regards > Till Vollmer > > > >---- Bob Hutchison -- blogs at <http://www.recursive.ca/ hutch/> Recursive Design Inc. -- <http://www.recursive.ca/> Raconteur -- <http://www.raconteur.info/> xampl for Ruby -- <http://rubyforge.org/projects/xampl/> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
_Kevin wrote:> etlund wrote: >> crazy stuff indeed. >> >> I understand (vaguely) the issue with FP numbers not being accurate, >> but I can''t wrap my head around this. >> >> I can''t seem to find any real discussion of ways to deal with this. Is >> the only workable solution to this issue to work exclusively with >> Integers? >> >> Eric > > Floating point numbers can only approximate some integers. > In your case, the final product of 19.9 * 100 is probably something > like > > 1989.99999999999999999999998, which just displays as 1990.0 in most > cases. > > Now, .to_i truncates off the decimal part pretty ruthlessly. > > To be safe, you should probably use a function like .round instead of > .to_i unless you are absolutely certain you can do that safely. > > .round will round the number to the nearest integer, which is really > what you want. > > _KevinI think holding the floating data by two distinct integers is the way to go. In a practical sense, a class can also make complexity invisible. -- 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 -~----------~----~----~----~------~----~------~--~---