On Thu, Jul 12, 2012 at 1:13 AM, John M. [email protected] 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 A., 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