In Dev-C++, how can I make a rounding to nearest integer?

Discussion in 'C++' started by Mr. Ken, May 25, 2006.

  1. Mr. Ken

    Mr. Ken Guest

    Here are the funny results I got from Dev-C++, what I need is rounding to
    nearest integer.
    How can I do that in Dev-C++?


    a = -1 -1 -1 -1 1 1 2 2 2 1
    b = round(a)
    -0.729627 -0.9540721 -0.2123411 -0.078923 0.321015 0.9876552 1.123422
    1.632136 1.234538 0.765442
    c = int(a)
    0 0 0 0 0 0 1 1 1 0

    My required data.
    c =
    -1 -1 0 0 0 1 1 2 1 1



    Thank you.
     
    Mr. Ken, May 25, 2006
    #1
    1. Advertising

  2. Mr. Ken

    Jim Langston Guest

    "Mr. Ken" <Mr. Ken@asdf> wrote in message
    news:44756dac$...
    > Here are the funny results I got from Dev-C++, what I need is rounding to
    > nearest integer.
    > How can I do that in Dev-C++?
    >
    >
    > a = -1 -1 -1 -1 1 1 2 2 2 1
    > b = round(a)
    > -0.729627 -0.9540721 -0.2123411 -0.078923 0.321015 0.9876552 1.123422
    > 1.632136 1.234538 0.765442
    > c = int(a)
    > 0 0 0 0 0 0 1 1 1 0
    >
    > My required data.
    > c =
    > -1 -1 0 0 0 1 1 2 1 1


    I think you are just trying to round
    -0.729627 -0.9540721 -0.2123411 -0.078923 0.321015 0.9876552 1.123422
    1.632136 1.234538 0.765442

    to the nearest int instead of truncating them, correct?

    Simply add (or subtract) .5

    c = static_cast<int>( a < 0 ? a - 0.5 : a + 0.5 );

    static_cast<int>() is the c++ way to do (int) casting.

    the
    a < 0 : a - 0.5 : a + 0.5
    is the ternary statement. it's similar (but not the same) as
    if ( a < 0 )
    return a - 0.5;
    else
    return a + 0.5;
     
    Jim Langston, May 25, 2006
    #2
    1. Advertising

  3. Mr. Ken

    Tomás Guest

    RoundOff

    Jim Langston posted:


    > Simply add (or subtract) .5
    >
    > c = static_cast<int>( a < 0 ? a - 0.5 : a + 0.5 );
    >
    > static_cast<int>() is the c++ way to do (int) casting.
    >
    > the
    > a < 0 : a - 0.5 : a + 0.5
    > is the ternary statement. it's similar (but not the same) as
    > if ( a < 0 )
    > return a - 0.5;
    > else
    > return a + 0.5;




    template<class Integral,class Floating>
    inline Integral RoundOff(Floating const d)
    {
    return static_cast<Integral>(
    (d < Floating(0)) ? (d - Floating(.5)) : (d + Floating(.5))
    );

    /* static_cast only used to supress compiler truncation warning */
    }

    int main()
    {
    float b = 34.23;

    long k = RoundOff<long>(b);
    }

    -Tomás
     
    Tomás, May 25, 2006
    #3
    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. Greg Young [MVP]

    Re: rounding values to the nearest 5 or 10

    Greg Young [MVP], Apr 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    483
    Greg Young [MVP]
    Apr 27, 2006
  2. chaz
    Replies:
    0
    Views:
    398
  3. =?ISO-8859-1?Q?G=F6ran_Andersson?=

    Re: rounding values to the nearest 5 or 10

    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Apr 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,287
    Milsnips
    Apr 28, 2006
  4. Shea Martin
    Replies:
    7
    Views:
    3,595
    P.J. Plauger
    Oct 30, 2003
  5. Derek Basch
    Replies:
    8
    Views:
    429
    Fredrik Lundh
    Feb 28, 2006
Loading...

Share This Page