473,941 Members | 12,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

divide , maths and ceiling

Can someone tell me why the following statement gives me the result of 0 :

Math.Ceiling( 1 / 12);

Well actually the above is simplefied from :

private const int LocationsPerPag e = 12;

(int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
LocationsPerPag e);
Jul 26 '06 #1
6 15833
Because 1 / 12 is integer math, and evaluates to the integer zero (i.e. the
whole number of 12s in 1 is zero - makes it easy to do full division via x /
y and x % y [remainder]). Ceiling of zero is zero.

Conversely, 1F / 12 will evaluate as a float to 0.083333... and then Ceiling
is one.

Marc
Jul 26 '06 #2
Jon Vaughan wrote:
Can someone tell me why the following statement gives me the result of 0 :

Math.Ceiling( 1 / 12);

Well actually the above is simplefied from :

private const int LocationsPerPag e = 12;

(int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
LocationsPerPag e);
Sincce both operatnds of your division are integral types, the result of the
divide is already zero before you pass it to the Ceiling() function. Cast the
result to float and you will see your expected result.
HTH,
-rick-
Jul 26 '06 #3
On Wed, 26 Jul 2006 10:29:35 GMT, "Jon Vaughan"
<jo**********@h otmail.comwrote :
>Can someone tell me why the following statement gives me the result of 0 :

Math.Ceiling ( 1 / 12);

Well actually the above is simplefied from :

private const int LocationsPerPag e = 12;

(int)System.Ma th.Ceiling(dsLo cations.Tables[0].Rows.Count /
LocationsPerPa ge);

1/12 is 0, this is an integer division. Ceiling(0) is just 0.
Actually I had trouble reproducing your example, because the C#
compiler complains of ambiguity in the calls to Math.Ceiling()
Console.WriteLi ne(Math.Ceiling (1 / 12); // Won't compile.

int result = 1 / 12;
Console.WriteLi ne(Math.Ceiling ((double)result )); // Prints 0
Console.WriteLi ne(Math.Ceiling (1.0 / 12.0)); // Prints 1

Jul 26 '06 #4
Hi.

Math.Ceiling needs Double or Decimal
Instead of
System.Math.Cei ling(dsLocation s.Tables[0].Rows.Count /
LocationsPerPag e)
try:
System.Math.Cei ling((Double)ds Locations.Table s[0].Rows.Count /
(Double)Locatio nsPerPage)

It should help ...
to************* ***@gazeta.pl
Jon Vaughan napisal(a):
Can someone tell me why the following statement gives me the result of 0 :

Math.Ceiling( 1 / 12);

Well actually the above is simplefied from :

private const int LocationsPerPag e = 12;

(int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
LocationsPerPag e);
Jul 26 '06 #5
A different type of solution; another way (probably more efficient as no
FP-math needed) of doing this would be:

int locations = dsLocations.Tab les[0].Rows.Count; // total count
int pages = locations / LocationsPerPag e; // full pages
if(locations % LocationsPerPag e 0) pages++; // trailing rows on last page

Marc
Jul 26 '06 #6

Jon Vaughan wrote:
Can someone tell me why the following statement gives me the result of 0 :

Math.Ceiling( 1 / 12);

Well actually the above is simplefied from :

private const int LocationsPerPag e = 12;

(int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
LocationsPerPag e);
You are dividing to integers, so the result is an (int). Normally, a
conversion to int will just chop of anything after the decimal point.
So (int1 / int2) is the same as (int)(int1 / int2). You need to convert
both values to a floating point (e.g. a double) to make the
mathematical operation a non-integer one.

you should try:
(int)System.Mat h.Ceiling((doub le)dsLocations. Tables[0].Rows.Count /
(double)Locatio nsPerPage);

Which should give you the result you are looking for in this case:
Ceiling(1/12) == 1

Cheers,
Vincent.

Jul 27 '06 #7

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

Similar topics

20
15929
by: Daisy | last post by:
Something simple, I'm sure. My code: System.Windows.Forms.MessageBox.Show(((Int32)Math.Ceiling(1966 / 100)).ToString()); I'm expecting 20, but getting 19. What am I doing wrong? Thanks --
4
3665
by: Ryan Liu | last post by:
Is there a good reason that Decimal does not have Ceiling() method, only Math class has? Thanks!
2
10134
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 machine extract. In this query, I use an expression to calculate the total tablet requirement from a combination of the table value of Sum of DailyQty * DaysSupply. DaysSupply is a parameter that is entered by the user when running the query....
5
11306
by: matthias s | last post by:
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);
15
3915
by: Alasdair | last post by:
I need to apply the ceiling function to arbitrary sized (long) integers. However, division automatically returns the type of its operands, so that, for example: math.ceil(7/4) returns 1. I can use float, as in: math.ceil(7/float(4)), except that for very large integers float causes an unacceptable loss of precision. What is the best way of finding a ceiling of a quotient of arbitrary sized integers? Thanks,
1
3978
by: Greenwood | last post by:
Hi Im trying to Math.Ceiling with the following code, where fr is a array of doubles and max is an int, it give me the following error "Error The call is ambiguous between the following methods or properties: 'System.Math.Ceiling(decimal)' and 'System.Math.Ceiling(double)'" for (int i = 0; i < fr.Length; i++) { if (fr < max) { max = (int)Math.Ceiling(i); }...
0
10135
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9968
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11531
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4909
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.