473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Oct 8 '07 #1
5 11251

"matthias s" <postamt__add__kieferfrost__dodd__dewrote in message
news:%2***************@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.
Oct 8 '07 #2
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
Oct 8 '07 #3
>
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.
Oct 8 '07 #4
Jon Slaughter <Jo***********@Hotmail.comwrote:

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 - <sk***@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
Oct 8 '07 #5
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
Oct 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
4
by: nick | last post by:
I'm having a really weird problem. Take a look at this screenshot. http://users.aber.ac.uk/njb4/dotnetproblem1.jpg Notice the 4 watches at the bottom, _totalRecords, MyDataGrid.PageSize and...
12
by: Test User | last post by:
Hi all, I have learnt that if I want to round 0.5 to an integer the result should be 1, This is also the case if I do it in SQL server 2000, but if I do it in VB.NET the result will be 0. ...
3
by: Agnes | last post by:
my number is 100.1 , Now I only want to get 100 without any round up or round down function, Can anyone told me which function can do that ? Thanks
4
by: Ryan Liu | last post by:
Is there a good reason that Decimal does not have Ceiling() method, only Math class has? Thanks!
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
2
by: deanoooo812 | last post by:
I have an Access query (written in MS Access 2000 - thats all we've got - don't get me started on that topic...) for making pharmacy dispensing labels based on an extract from an automated dispensing...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.