TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

understanding Math.Ceiling

C#(C Sharp)

 
 
Thread Tools Display Modes
Unread 08-10-2007, 02:09 PM   #1
matthias s
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


 
Unread 08-10-2007, 02:09 PM   #2
Troye Stonich
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
>



 
Unread 08-10-2007, 03:14 PM   #3
Jon Slaughter
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.


 
Unread 08-10-2007, 04:18 PM   #4
Lew
Guest
 
Posts: n/a
Re: understanding Math.Ceiling

(top-posting re-ordered 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
 
Unread 08-10-2007, 06:17 PM   #5
Arne Vajh°j
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
 
Unread 08-10-2007, 09:16 PM   #6
Jon Slaughter
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.


 
Unread 08-10-2007, 10:15 PM   #7
Jon Skeet [C# MVP]
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+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 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
 
Unread 08-10-2007, 11:06 PM   #8
Arne Vajh°j
Guest
 
Posts: n/a
Re: understanding Math.Ceiling

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
 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math libarary Jon Slaughter C#(C Sharp) 7 14-09-2007 10:56 PM
Math libarary Jon Slaughter C#(C Sharp) 5 14-09-2007 09:47 PM
Math libarary Jon Slaughter C#(C Sharp) 2 14-09-2007 09:10 PM
Math Library raju C#(C Sharp) 2 05-09-2007 09:19 AM
Numeration of math equation Marco Microsoft Office 0 28-08-2007 09:41 AM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 12:13 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional