understanding Math.Ceiling

  • Thread starter Thread starter matthias s
  • Start date Start date
M

matthias s

Hi there,

I believed that Math.Ceiling is like Math.Round except that it always rounds
up to the next

but this

double d = Math.Ceiling(29/15);


will get me "1" where I'd expect it to return "2". Am I on the wrong track?
Could somebody be so kind an shed some light?

Thanks in advance!

Matthias
 
You have to convert your inputs to decimals in order for it to work:

double d = Math.Ceiling(Convert.ToDecimal(29) / Convert.ToDecimal(15));
 
matthias s said:
Hi there,

I believed that Math.Ceiling is like Math.Round except that it always
rounds up to the next

but this

double d = Math.Ceiling(29/15);


will get me "1" where I'd expect it to return "2". Am I on the wrong
track? Could somebody be so kind an shed some light?
29/15 is 1 in integer math.

Essentially its performing Floor(29/15) first or another way is [29/15] or
int(29/15).

The compiler has no way of knowing that you want to divide floating point
numbers. I find that annoying cause it can creap in bugs if your not
careful.

You can do 29.0/15.0 for 29.0F/15.0F. The first is double precision I
believe while the second is single. Else it defaults to int and performs
integer arithmetic.

You are correct though, Ceiling rounds up.
 
(top-posting re-ordered for clarity)

...
Troye said:
You have to convert your inputs to decimals in order for it to work:

double d = Math.Ceiling(Convert.ToDecimal(29) / Convert.ToDecimal(15));

Or, as Jon Slaughter suggested,
double d = Math.Ceiling( 29.0 / 15.0 );
or
double d = Math.Ceiling( 29D / 15D );


As long as either number is a double literal, the division will be done with
double arithmetic. It's best that every literal intended as a double be a
double literal.
 
matthias said:
I believed that Math.Ceiling is like Math.Round except that it always rounds
up to the next

but this

double d = Math.Ceiling(29/15);

will get me "1" where I'd expect it to return "2". Am I on the wrong track?
Could somebody be so kind an shed some light?

Others have already explained how you can do a floating point
division instead of an integer division.

But note that you should never have to do a conversion
from integer to floating point.

If it is literal constants you can just write:

double d = 2;

If it is floating point variables your approach will work:

double d = Math.Ceiling(a/b);

If it is integer variables you do not need Math.Ceiling:

double d = (a + b - 1) / b;

Arne
 
If it is integer variables you do not need Math.Ceiling:

double d = (a + b - 1) / b;

um,. if the lhs is done using all integer arithmetic then

(a + b - 1)/b = a/b + 1 - 1/b

but 1/b = 0

so your left with a/b + 1 which is the ceiling of course(since a/b is floor)
but no reason to subtract 1.

i.e.

a/b by itself is integer division which is equivalent to floor(a/b) if a and
b are treated as floating point(or even not since floor(floor(x)) =
floor(x). Since floor(x) + 1 = ceil(x) you can easily move between the two.

Point is, is that its unecessary to subtract 1/b as it does absolutely
nothing in integer arithmetic.

So rather,

int d = a / b + 1;

Not that your wrong but just wanted to clear up that issue.
 
Jon Slaughter said:
um,. if the lhs is done using all integer arithmetic then

(a + b - 1)/b = a/b + 1 - 1/b

You're applying algebra to split the whole thing up, which doesn't take
rounding into account. The rounding is only applied once in Arne's
post, instead of three times with your dissection.
but 1/b = 0

so your left with a/b + 1 which is the ceiling of course(since a/b is floor)
but no reason to subtract 1.

Yes there is. Take a=8, b=4. The ceiling of a/b is 2, but if you use
(a+b)/b you end up with 3. Take (a+b-1)/b you get 11/4 which is rounded
down to 2, the correct answer.
Point is, is that its unecessary to subtract 1/b as it does absolutely
nothing in integer arithmetic.
So rather,

int d = a / b + 1;

Again, that's wrong - when b is exactly divisible by a, the answer is
incorrect.
Not that your wrong but just wanted to clear up that issue.

Arne was absolutely correct, and you are wrong to apply algebra and
then integer division to each part and assume it's the same answer as
applying integer division to the original expression.
 
Jon said:
um,. if the lhs is done using all integer arithmetic then

(a + b - 1)/b = a/b + 1 - 1/b

but 1/b = 0

so your left with a/b + 1 which is the ceiling of course(since a/b is floor)
but no reason to subtract 1.

i.e.

a/b by itself is integer division which is equivalent to floor(a/b) if a and
b are treated as floating point(or even not since floor(floor(x)) =
floor(x). Since floor(x) + 1 = ceil(x) you can easily move between the two.

Point is, is that its unecessary to subtract 1/b as it does absolutely
nothing in integer arithmetic.

So rather,

int d = a / b + 1;

Not that your wrong but just wanted to clear up that issue.

Try put a = b into the two formulas:

(a + b - 1) / b

a / b + 1

Do you get the same ?

No !

So ...

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top