08102007, 02:09 PM  #1 
Guest
Posts: n/a

understanding Math.Ceiling
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 Sponsored Links 
08102007, 02:09 PM  #2 
Guest
Posts: n/a

Re: understanding Math.Ceiling
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" <postamt__add__kieferfrost__dodd__de> wrote in message news:%23A5YwxaCIHA.324@TK2MSFTNGP04.phx.gbl... > 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 > 
08102007, 03:14 PM  #3 
Guest
Posts: n/a

Re: understanding Math.Ceiling
"matthias s" <postamt__add__kieferfrost__dodd__de> wrote in message news:%23A5YwxaCIHA.324@TK2MSFTNGP04.phx.gbl... > 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. 
08102007, 04:18 PM  #4 
Guest
Posts: n/a

Re: understanding Math.Ceiling
(topposting reordered for clarity)
"matthias s" wrote... >> 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? Troye Stonich wrote: > 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.  Lew 
08102007, 06:17 PM  #5 
Guest
Posts: n/a

Re: understanding Math.Ceiling
matthias s wrote:
> 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 
08102007, 09:16 PM  #6 
Guest
Posts: n/a

Re: understanding Math.Ceiling
>
> 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. 
08102007, 10:15 PM  #7 
Guest
Posts: n/a

Re: understanding Math.Ceiling
Jon Slaughter <Jon_Slaughter*************> wrote:
> > > > 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 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+b1)/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 Skeet  <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too 
08102007, 11:06 PM  #8 
Guest
Posts: n/a

Re: understanding Math.Ceiling
Sponsored Links Jon Slaughter wrote:
>> 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. Try put a = b into the two formulas: (a + b  1) / b a / b + 1 Do you get the same ? No ! So ... Arne Sponsored Links 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Math libarary  Jon Slaughter  C#(C Sharp)  7  14092007 10:56 PM 
Math libarary  Jon Slaughter  C#(C Sharp)  5  14092007 09:47 PM 
Math libarary  Jon Slaughter  C#(C Sharp)  2  14092007 09:10 PM 
Math Library  raju  C#(C Sharp)  2  05092007 09:19 AM 
Numeration of math equation  Marco  Microsoft Office  0  28082007 09:41 AM 
< Home  Windows Help  MS Office Help  Hardware Support >
New To Site?  Need Help? 