Hii All I use ruby 1.8.7 I run irb and type: a = (0.29 * 100).to_i the result is 28 why?????? could u explain to me.. Please???? THank you -- Senior Rails Developer Anton Effendi - Wu You Duan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Scary! irb(main):019:0> (0.57 * 100).to_i => 56 irb(main):020:0> (0.58 * 100).to_i => 57 This is totally out of my understanding.... Will look into core... seems like some bug... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
(0.29*100).to_s.to_i will return what you expect. On Mar 31, 10:27 am, Jimish Jobanputra <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Scary! > > irb(main):019:0> (0.57 * 100).to_i > => 56 > irb(main):020:0> (0.58 * 100).to_i > => 57 > > This is totally out of my understanding.... Will look into core... seems > like some bug... > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Nm... this explains it: http://rubypond.com/blog/when-ruby-floating-just-isn-t-good-enough -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
See this may be u get some idea.. http://rubypond.com/blog/when-ruby-floating-just-isn-t-good-enough -Shyam On Wed, Mar 31, 2010 at 11:48 AM, anton effendi <wuyouduan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hii All > > I use ruby 1.8.7 > I run irb and type: > > a = (0.29 * 100).to_i > > the result is 28 > > > why?????? > > could u explain to me.. Please???? > > > THank you > > > -- > > Senior Rails Developer > Anton Effendi - Wu You Duan > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Quoting anton effendi <wuyouduan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Hii All > > I use ruby 1.8.7 > I run irb and type: > > a = (0.29 * 100).to_i > > the result is 28Here''s why: irb(main):008:0> ''%0.16f'' % (0.29 * 100) => "28.9999999999999964" Most decimal fractions cannot be represented exactly in binary. If you need them to behave in certain ways, e.g. using real numbers to represent US dollars and cents, take care and learn to use the following functions as needed. irb(main):002:0> (0.29 * 100).to_i => 28 irb(main):003:0> (0.29 * 100).round => 29 irb(main):004:0> (0.29 * 100).floor => 28 irb(main):005:0> (0.29 * 100).ceil => 29 irb(main):009:0> (-0.29 * 100) => -29.0 irb(main):010:0> (-0.29 * 100).to_i => -28 irb(main):011:0> (-0.29 * 100).floor => -29 irb(main):012:0> (-0.29 * 100).round => -29 irb(main):013:0> (-0.29 * 100).ceil => -28 IIRC, .to_i rounds towards zero, .floor rounds down, .ceil rounds up, and .round adds 1/2 and rounds towards zero. HTH, Jeffrey P.S. if you are in the financial field, the SEC has rules on how to do arithmetic in US dollars and cents. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you all I found why that happen... On Wed, Mar 31, 2010 at 11:39 PM, Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>wrote:> Quoting anton effendi <wuyouduan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > Hii All > > > > I use ruby 1.8.7 > > I run irb and type: > > > > a = (0.29 * 100).to_i > > > > the result is 28 > > Here''s why: > > irb(main):008:0> ''%0.16f'' % (0.29 * 100) > => "28.9999999999999964" > > Most decimal fractions cannot be represented exactly in binary. If you > need > them to behave in certain ways, e.g. using real numbers to represent US > dollars and cents, take care and learn to use the following functions as > needed. > > irb(main):002:0> (0.29 * 100).to_i > => 28 > irb(main):003:0> (0.29 * 100).round > => 29 > irb(main):004:0> (0.29 * 100).floor > => 28 > irb(main):005:0> (0.29 * 100).ceil > => 29 > irb(main):009:0> (-0.29 * 100) > => -29.0 > irb(main):010:0> (-0.29 * 100).to_i > => -28 > irb(main):011:0> (-0.29 * 100).floor > => -29 > irb(main):012:0> (-0.29 * 100).round > => -29 > irb(main):013:0> (-0.29 * 100).ceil > => -28 > > > IIRC, .to_i rounds towards zero, .floor rounds down, .ceil rounds up, and > .round adds 1/2 and rounds towards zero. > > HTH, > Jeffrey > > P.S. if you are in the financial field, the SEC has rules on how to do > arithmetic in US dollars and cents. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Senior Rails Developer Anton Effendi - Wu You Duan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.