473,467 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

floor or ceil for integer division

Hi,

The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way to round it to 4 for this case?

Thanks,
Peng

Oct 23 '06 #1
9 43509
Pe*******@gmail.com <Pe*******@gmail.comwrote:
>The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way to round it to 4 for this case?
int a,b,c;
c = (int) ((double)(a/b) + 0.4999999);

See also rint(), nearbyint() in <cmath>.

Steve
Oct 23 '06 #2
Pe*******@gmail.com wrote:
Hi,

The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way to round it to 4 for this case?

Thanks,
Peng
I donot understand what you want to say, 10/3=3.33333, so the integer
you need is 3, is that wrong?
Oct 23 '06 #3
Ye Dafeng wrote:
Pe*******@gmail.com wrote:
>Hi,

The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way to round it to 4 for this case?

Thanks,
Peng

I donot understand what you want to say, 10/3=3.33333, so the integer
you need is 3, is that wrong?

hoops, i misunderstand your meaning, sorry!
Oct 23 '06 #4


On Oct 22, 9:53 pm, spop...@speedymail.org (Steve Pope) wrote:
PengYu...@gmail.com <PengYu...@gmail.comwrote:
The usually integer division willroundthe result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way toroundit to 4 for this case?int a,b,c;
c = (int) ((double)(a/b) + 0.4999999);
Will this introduce errors if a/b=0.50000000000001?
>
See also rint(), nearbyint() in <cmath>.
How can I guarantee the conversion between float and int won't
introduce any errors?

Thanks,
Peng

Oct 23 '06 #5
Pe*******@gmail.com wrote:
>
On Oct 22, 9:53 pm, spop...@speedymail.org (Steve Pope) wrote:
>PengYu...@gmail.com <PengYu...@gmail.comwrote:
>>The usually integer division willroundthe result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way toroundit to 4 for this case?int a,b,c;
c = (int) ((double)(a/b) + 0.4999999);
Will this introduce errors if a/b=0.50000000000001?
>See also rint(), nearbyint() in <cmath>.
How can I guarantee the conversion between float and int won't
introduce any errors?

Thanks,
Peng
how about this method:

if((a%b) != 0)
c = (int)(a/b) + 1;
Oct 23 '06 #6
Pe*******@gmail.com <Pe*******@gmail.comwrote:
>On Oct 22, 9:53 pm, spop...@speedymail.org (Steve Pope) wrote:
>PengYu...@gmail.com <PengYu...@gmail.comwrote:
>The usually integer division willroundthe result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way toroundit to 4 for this
case?int a,b,c;
>c = (int) ((double)(a/b) + 0.4999999);
Will this introduce errors if a/b=0.50000000000001?
Well, you only asked that 10/3 round up to 4.
>See also rint(), nearbyint() in <cmath>.
>How can I guarantee the conversion between float and int won't
introduce any errors?
These give you either the nearest value, or they round down.
If you need more exact behavior use something like rint(),
and refer to the man pages for it.

Steve
Oct 23 '06 #7
<Pe*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,

The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
I'm wondering if there is any easy way to round it to 4 for this case?

Thanks,
Peng
If you don't want to go with the converstion to float, then look at the
remainder.

int Operator = 10;
int Devisor = 3;
int Result = Operator / Devisor;

if ( Operator % Devisor != 0 )
Result++;

This will round up.

if ( Operator % Devisor >= Devisor / 2 )
Result++;
This will round to the closest in cases where the devisor is devisible by 2.

Neither of these handle negatives correctly.
Oct 23 '06 #8
Pe*******@gmail.com wrote:
On Oct 22, 9:53 pm, spop...@speedymail.org (Steve Pope) wrote:
PengYu...@gmail.com <PengYu...@gmail.comwrote:
>The usually integer division willroundthe result to the biggest
>integet smaller than the float version division.For example, 10/3 = 3.
>I'm wondering if there is any easy way toroundit to 4 for this case?int a,b,c;
c = (int) ((double)(a/b) + 0.4999999);
Will this introduce errors if a/b=0.50000000000001?

See also rint(), nearbyint() in <cmath>.
How can I guarantee the conversion between float and int won't
introduce any errors?

Thanks,
Peng
Your worried about an insignificant, tiny, non-measureable error whilst
you are attempting to introduce a significant error by suggesting that
10/3 should be 4? Thats a 20% error! What happens when a 20% error gets
propagated?
What are the requirements for that 3 to become a 4? and why?
have you looked at modulus? 10%3, which will result 1 as an adjustment?
Although i still can't understand why.

Do you realize that you'ld have to multiply that doubles's error as
displayed by 1.0 x 10^14 in order for it to impact a cast to an integer
value? In fact, since an integer has both a max and min limitation with
a relatively puny and microscopic valid range, that floating number has
an amazing, mind-boggling count of significant figures.
Here, observe what happens to an integer when it reaches its max
allowed value:

#include <iostream>
#include <limits>

int main()
{
int min = std::numeric_limits<int>::min();
int max = std::numeric_limits<int>::max();
std::cout << "range of an integer on this platform:";
std::cout << "\nmin: " << min;
std::cout << "\nmax: " << max;
std::cout << "\nrollover = " << ++max; // rollover

std::cout << std::endl;

return 0;
}

/* your mileage will vary... i'm running a 64 bit platform
range of an integer on this platform:
min: -2147483648
max: 2147483647
rollover = -2147483648 !!!
*/

Now observe the unadulterated double:

/*
range of a double on this platform:
min: 2.22507e-308
max: 1.79769e+308
*/
thats around 4e616 !!! care to compare a roundoff error of 1.0e-14 to
that range?

floating numbers are not whole numbers and they do typically roundof
the last digit by 0.5 or so. But lets take that into context here.

Oct 23 '06 #9
Pe*******@gmail.com wrote:
...
The usually integer division will round the result to the biggest
integet smaller than the float version division.For example, 10/3 = 3.
That's true for positive integers. If one of them is negative though,
the direction of rounding will normally be the opposite (i.e. integer
division normally rounds towards zero).
I'm wondering if there is any easy way to round it to 4 for this case?
Increase the dividend by 'divisor - 1' before performing the division:

(10 + 3 - 1) / 3 = 4

--
Best regards,
Andrey Tarasevich

Oct 23 '06 #10

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

Similar topics

7
by: Matthew Wilson | last post by:
Hi- I just discovered this: >>> -1 // 12 -1 >>> 1 // 12 0 >>>
2
by: Michael Cornelius | last post by:
As an old C programmer, I'm surprised by some results I'm getting with integer division. For example: >>> -1/1000 -1 >>> -9/2 -5 I expect the results of these expressions to be 0 and -4,...
19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
2
by: Darius Fatakia | last post by:
hi, i'm new to MS Visual Studio and C++ but not to C programming in general. i'm trying to divide two integers and get their actual quotient (eg 5/3 = 1.666667 etc). i thought i had type cast...
3
by: Sidney Cadot | last post by:
Hi all, As I understand it, the rounding direction of signed integer division in C is unspecified in C89/C94, and specified to be 'towards-zero' in C99. I need division 'towards -infinity'...
6
by: J.Marsch | last post by:
Suppose that I have an integer division problem, but I want the answer was a float. What's the cleanest syntax for that? Example: In this code, the variable z ends up == 0. And that is...
2
by: Ben | last post by:
Hi, I have an interesting example from my debugger. I have 2 variables: sourcewidthnet and targetwidthnet. Notice the results in the debugger. I'm going to be forced to use the int function of the...
1
by: Marge | last post by:
Create a java program that performs integer division. The program will ask the user for two integer numbers and will divide the dividend by the divisor.It will then output the results(quotient) and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.