floor or ceil for integer division

Discussion in 'C++' started by PengYu.UT@gmail.com, Oct 23, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Steve Pope Guest

    <> wrote:

    >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
     
    Steve Pope, Oct 23, 2006
    #2
    1. Advertising

  3. Ye Dafeng Guest

    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?
     
    Ye Dafeng, Oct 23, 2006
    #3
  4. Ye Dafeng Guest

    Ye Dafeng wrote:
    > 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!
     
    Ye Dafeng, Oct 23, 2006
    #4
  5. Guest

    On Oct 22, 9:53 pm, (Steve Pope) wrote:
    > <> wrote:
    > >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, 2006
    #5
  6. Ye Dafeng Guest

    wrote:
    >
    > On Oct 22, 9:53 pm, (Steve Pope) wrote:
    >> <> wrote:
    >>> 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;
     
    Ye Dafeng, Oct 23, 2006
    #6
  7. Steve Pope Guest

    <> wrote:

    >On Oct 22, 9:53 pm, (Steve Pope) wrote:


    >> <> wrote:


    >> >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
     
    Steve Pope, Oct 23, 2006
    #7
  8. Jim Langston Guest

    <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Oct 23, 2006
    #8
  9. Salt_Peter Guest

    wrote:
    > On Oct 22, 9:53 pm, (Steve Pope) wrote:
    > > <> wrote:
    > > >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.
     
    Salt_Peter, Oct 23, 2006
    #9
  10. 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
     
    Andrey Tarasevich, Oct 23, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SpaceCowboy
    Replies:
    6
    Views:
    1,130
    Josef Garvi
    Aug 15, 2003
  2. Christoph Zwerschke

    Command line option -Q (floor division)

    Christoph Zwerschke, Mar 23, 2006, in forum: Python
    Replies:
    2
    Views:
    303
    Georg Brandl
    Mar 29, 2006
  3. zlotawy

    ceil and floor

    zlotawy, Sep 15, 2007, in forum: VHDL
    Replies:
    4
    Views:
    724
  4. john.swilting

    use POSIX qw(ceil floor)

    john.swilting, Apr 21, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    185
    john.swilting
    Apr 26, 2007
  5. RobG
    Replies:
    6
    Views:
    245
Loading...

Share This Page