Often times the ceil method is used for patterns that involve grouping. For example, if I have an array of objects and I want to group them in rows of 3, I might do this: (@objects.size / 3.0).ceil So if size returns 2, then the above expression returns 1. Here''s my question. In terms of arithmetic, I dont understand why 3.0 must be used and not just 3. If I was to calculate 2/3.0 and 2/3 in a calculator, it returns same result, after all, since .0 is meaningless. Its like 3.00 or 3.0000 it all means the same. So why does ceil return different results with 2/3.0 and 2/3? thanks for response -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/nln7GDpfo7sJ. 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-US.
Am 12.07.2012 07:13 schrieb "John Merlino" <stoicism1-YDxpq3io04c@public.gmane.org>:> > Often times the ceil method is used for patterns that involve grouping.For example, if I have an array of objects and I want to group them in rows of 3, I might do this:> > (@objects.size / 3.0).ceil > > So if size returns 2, then the above expression returns 1. > > Here''s my question. In terms of arithmetic, I dont understand why 3.0must be used and not just 3. If I was to calculate 2/3.0 and 2/3 in a calculator, it returns same result, after all, since .0 is meaningless. Its like 3.00 or 3.0000 it all means the same. So why does ceil return different results with 2/3.0 and 2/3? Just try 2/3 without the ceil... as far as I remember dividing int by int returns an int, so you Have to use 2/3.0 to tell ruby that you want to Have a float, dass at least this is True for C, C++ and C#.> > thanks for response-- 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-US.
Let''s ask irb: 1.9.2-p320 :002 > 3/2 => 1 1.9.2-p320 :003 > 3.0/2 => 1.5 Walter On Jul 12, 2012, at 1:13 AM, John Merlino wrote:> Often times the ceil method is used for patterns that involve grouping. For example, if I have an array of objects and I want to group them in rows of 3, I might do this: > > (@objects.size / 3.0).ceil > > So if size returns 2, then the above expression returns 1. > > Here''s my question. In terms of arithmetic, I dont understand why 3.0 must be used and not just 3. If I was to calculate 2/3.0 and 2/3 in a calculator, it returns same result, after all, since .0 is meaningless. Its like 3.00 or 3.0000 it all means the same. So why does ceil return different results with 2/3.0 and 2/3? > > thanks for response > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/nln7GDpfo7sJ. > 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-US.-- 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-US.
On Thu, Jul 12, 2012 at 1:13 AM, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> (@objects.size / 3.0).ceil > > So if size returns 2, then the above expression returns 1. > > Here''s my question. In terms of arithmetic, I dont understand why 3.0 must > be used and not just 3.To clarify (and expand, thus re-muddying) the preceding answers somewhat: If a mathematical expression is written with only integers, most languages will assume that you want an integer answer. How it will deal with the remainder may vary, such as rounding, truncation, or "banker''s rounding". (IIRC VB or some such thing uses that. The difference from normal rounding is that normally halves round up, while in banker''s rounding, halves round to whichever way gives an even number.) So, you use .0 to turn one of those integers into a floating point number. You can achieve the same thing with a multiplication by 1.0. You''ll see this used where the numbers are variables rather than literals, especially in languages that make you declare a variable''s type, and distinguish between integers and floating point numbers. Either way, in a complicated expression, you may need to be careful about *which* literal you tack .0 onto, or *when* you multiply by 1.0. Getting that wrong may wind up with some deeper parts of the expression yielding integer results, when you really wanted a float. -Dave -- Dave Aronson, Cleared/Remote Ruby on Rails Freelancer (NoVa/DC/Remote); see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.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-US.
Yeah that''s pretty much the key point - that return type of invoking the / method of integer and passing it a decimal is a decimal type cast. On Jul 12, 12:45 pm, Dave Aronson <googlegroups2d...-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Thu, Jul 12, 2012 at 1:13 AM, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote: > > (@objects.size / 3.0).ceil > > > So if size returns 2, then the above expression returns 1. > > > Here''s my question. In terms of arithmetic, I dont understand why 3.0 must > > be used and not just 3. > > To clarify (and expand, thus re-muddying) the preceding answers somewhat: > > If a mathematical expression is written with only integers, most > languages will assume that you want an integer answer. How it will > deal with the remainder may vary, such as rounding, truncation, or > "banker''s rounding". (IIRC VB or some such thing uses that. The > difference from normal rounding is that normally halves round up, > while in banker''s rounding, halves round to whichever way gives an > even number.) > > So, you use .0 to turn one of those integers into a floating point number. > > You can achieve the same thing with a multiplication by 1.0. You''ll > see this used where the numbers are variables rather than literals, > especially in languages that make you declare a variable''s type, and > distinguish between integers and floating point numbers. > > Either way, in a complicated expression, you may need to be careful > about *which* literal you tack .0 onto, or *when* you multiply by 1.0. > Getting that wrong may wind up with some deeper parts of the > expression yielding integer results, when you really wanted a float. > > -Dave > > -- > Dave Aronson, Cleared/Remote Ruby on Rails Freelancer > (NoVa/DC/Remote); seewww.DaveAronson.com, and blogs atwww.Codosaur.us,www.Dare2XL.com,www.RecruitingRants.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-US.