Rounding functions in microsoft visual C/C++

Discussion in 'C Programming' started by Raf, Aug 27, 2007.

  1. Raf

    Raf Guest

    Hello everybody !!

    I'm newbye in programming with Microsoft Visual C/C++ software (MSVC).
    I'm nowadays porting a library initialy developped on Linux and
    written in C++ without object oriented capabilities; I'm trying to
    compile it on MSVC.

    My current problem is that the initial code is using a rounding to the
    nearest integer function named "rint" only defined on the GNU C
    library (in the GNU math.h file). And I really can't find an
    equivalent of this function on the MSVC documentation. I've tried with
    the Round function (which, according to the documentation, needs to
    use the System namespace), but the namespace System is unknown. Am I
    doing a bad use of the Round function and/or the System namespace ?
    Does anyone know basic rounding functions on MSVC ??

    Any help would be welcome !!
    Have a nice week ....
     
    Raf, Aug 27, 2007
    #1
    1. Advertising

  2. "Raf" <> wrote in message
    news:...
    > Hello everybody !!
    >
    > I'm newbye in programming with Microsoft Visual C/C++ software (MSVC).
    > I'm nowadays porting a library initialy developped on Linux and
    > written in C++ without object oriented capabilities; I'm trying to
    > compile it on MSVC.
    >
    > My current problem is that the initial code is using a rounding to the
    > nearest integer function named "rint" only defined on the GNU C
    > library (in the GNU math.h file). And I really can't find an
    > equivalent of this function on the MSVC documentation. I've tried with
    > the Round function (which, according to the documentation, needs to
    > use the System namespace), but the namespace System is unknown. Am I
    > doing a bad use of the Round function and/or the System namespace ?
    > Does anyone know basic rounding functions on MSVC ??
    >
    > Any help would be welcome !!
    > Have a nice week ....
    >


    Just write the function.

    double rint(double x)
    {
    return floor(x + 0.5);
    }

    or however rint works. You might want to check that x is within the range of
    a long, int, or even size_t, you might have different requirements for
    negative numbers or 0.5 exactly. You might want to return as a int or long
    rather than a double holding an integer.

    namespace are C++ ism's. Add "using namespace xxx" and double colons until
    it works, if coding in C++.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Aug 27, 2007
    #2
    1. Advertising

  3. Raf

    Raf Guest

    Ok, thanks Malcom and Abdo !
    I will try rigth now to check how I can have the "rint" capabilities
    with "floor" and "ceil" functions .
    I'll keep you posted !!

    And thanks too for the indication of the Microsoft Visual C++
    discussion ...
     
    Raf, Aug 27, 2007
    #3
  4. "Raf" <> wrote in message
    news:...
    > Hello everybody !!
    >
    > I'm newbye in programming with Microsoft Visual C/C++ software (MSVC).
    > I'm nowadays porting a library initialy developped on Linux and
    > written in C++ without object oriented capabilities; I'm trying to
    > compile it on MSVC.
    >
    > My current problem is that the initial code is using a rounding to the
    > nearest integer function named "rint" only defined on the GNU C
    > library (in the GNU math.h file). And I really can't find an
    > equivalent of this function on the MSVC documentation. I've tried with
    > the Round function (which, according to the documentation, needs to
    > use the System namespace), but the namespace System is unknown. Am I
    > doing a bad use of the Round function and/or the System namespace ?
    > Does anyone know basic rounding functions on MSVC ??

    If your integers are always of the same sign then see the standard floor()
    and ciel() (depending on your integers sign)

    Also for specific Microsoft Visual C++ question try:
    microsoft.public.vc.langauge
    You'll get more answers there as this is an OT question here.

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Aug 27, 2007
    #4
  5. [comp.lang.c] Abdo Haji-Ali <_use_com_instead> wrote:

    > If your integers are always of the same sign then see the standard floor()
    > and ciel() (depending on your integers sign)


    That's ceil().

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
     
    Christopher Benson-Manica, Aug 27, 2007
    #5
  6. Raf

    user923005 Guest

    On Aug 27, 5:00 am, "Malcolm McLean" <> wrote:
    > "Raf" <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > Hello everybody !!

    >
    > > I'm newbye in programming with Microsoft Visual C/C++ software (MSVC).
    > > I'm nowadays porting a library initialy developped on Linux and
    > > written in C++ without object oriented capabilities; I'm trying to
    > > compile it on MSVC.

    >
    > > My current problem is that the initial code is using a rounding to the
    > > nearest integer function named "rint" only defined on the GNU C
    > > library (in the GNU math.h file). And I really can't find an
    > > equivalent of this function on the MSVC documentation. I've tried with
    > > the Round function (which, according to the documentation, needs to
    > > use the System namespace), but the namespace System is unknown. Am I
    > > doing a bad use of the Round function and/or the System namespace ?
    > > Does anyone know basic rounding functions on MSVC ??

    >
    > > Any help would be welcome !!
    > > Have a nice week ....

    >
    > Just write the function.
    >
    > double rint(double x)
    > {
    > return floor(x + 0.5);
    >
    > }
    >
    > or however rint works. You might want to check that x is within the range of
    > a long, int, or even size_t, you might have different requirements for
    > negative numbers or 0.5 exactly. You might want to return as a int or long
    > rather than a double holding an integer.


    If you want it to work with the other half of the possible inputs, you
    might try:

    return floor(x < 0 ? x - 0.5 : x + 0.5);

    instead.

    The snippets collection has rounding macros in it (in case you want to
    round to something other than the nearest integer).

    > namespace are C++ ism's. Add "using namespace xxx" and double colons until
    > it works, if coding in C++.
    > --
    > Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -
    >
    > - Show quoted text -
     
    user923005, Aug 27, 2007
    #6
  7. >> > My current problem is that the initial code is using a rounding to the
    >> > nearest integer function named "rint" only defined on the GNU C
    >> > library (in the GNU math.h file). And I really can't find an
    >> > equivalent of this function on the MSVC documentation. I've tried with
    >> > the Round function (which, according to the documentation, needs to
    >> > use the System namespace), but the namespace System is unknown. Am I
    >> > doing a bad use of the Round function and/or the System namespace ?
    >> > Does anyone know basic rounding functions on MSVC ??

    >>
    >> > Any help would be welcome !!
    >> > Have a nice week ....

    >>
    >> Just write the function.
    >>
    >> double rint(double x)
    >> {
    >> return floor(x + 0.5);
    >>
    >> }
    >>
    >> or however rint works. You might want to check that x is within the range of
    >> a long, int, or even size_t, you might have different requirements for
    >> negative numbers or 0.5 exactly. You might want to return as a int or long
    >> rather than a double holding an integer.

    >
    >If you want it to work with the other half of the possible inputs, you
    >might try:


    You can try it, but it's WRONG. Use the original version above.
    It works fine for positive and negative arguments that do not cause
    overflow, except possibly for the "exactly halfway between" case,
    which is not specified by "round to nearest integer".

    >return floor(x < 0 ? x - 0.5 : x + 0.5);


    Examples:
    Test: rint(1.9)
    original version: rint(1.9) -> floor(2.4) -> 2.0
    wrong version: rint(1.9) -> floor(2.4) -> 2.0

    Test: rint(2.1)
    original version: rint(2.1) -> floor(2.6) -> 2.0
    wrong version: rint(2.1) -> floor(2.6) -> 2.0

    Test: rint(-1.9)
    original version: rint(-1.9) -> floor(-1.4) -> -2.0
    wrong version: rint(-1.9) -> floor(-2.4) -> -3.0 (wrong answer)

    Test: rint(-2.1)
    original version: rint(-2.1) -> floor(-1.6) -> -2.0
    wrong version: rint(-2.1) -> floor(-2.6) -> -3.0 (wrong answer)
     
    Gordon Burditt, Aug 28, 2007
    #7
  8. Raf

    user923005 Guest

    On Aug 27, 4:42 pm, (Gordon Burditt) wrote:
    > >> > My current problem is that the initial code is using a rounding to the
    > >> > nearest integer function named "rint" only defined on the GNU C
    > >> > library (in the GNU math.h file). And I really can't find an
    > >> > equivalent of this function on the MSVC documentation. I've tried with
    > >> > the Round function (which, according to the documentation, needs to
    > >> > use the System namespace), but the namespace System is unknown. Am I
    > >> > doing a bad use of the Round function and/or the System namespace ?
    > >> > Does anyone know basic rounding functions on MSVC ??

    >
    > >> > Any help would be welcome !!
    > >> > Have a nice week ....

    >
    > >> Just write the function.

    >
    > >> double rint(double x)
    > >> {
    > >> return floor(x + 0.5);

    >
    > >> }

    >
    > >> or however rint works. You might want to check that x is within the range of
    > >> a long, int, or even size_t, you might have different requirements for
    > >> negative numbers or 0.5 exactly. You might want to return as a int or long
    > >> rather than a double holding an integer.

    >
    > >If you want it to work with the other half of the possible inputs, you
    > >might try:

    >
    > You can try it, but it's WRONG. Use the original version above.
    > It works fine for positive and negative arguments that do not cause
    > overflow, except possibly for the "exactly halfway between" case,
    > which is not specified by "round to nearest integer".
    >
    > >return floor(x < 0 ? x - 0.5 : x + 0.5);

    >
    > Examples:
    > Test: rint(1.9)
    > original version: rint(1.9) -> floor(2.4) -> 2.0
    > wrong version: rint(1.9) -> floor(2.4) -> 2.0
    >
    > Test: rint(2.1)
    > original version: rint(2.1) -> floor(2.6) -> 2.0
    > wrong version: rint(2.1) -> floor(2.6) -> 2.0
    >
    > Test: rint(-1.9)
    > original version: rint(-1.9) -> floor(-1.4) -> -2.0
    > wrong version: rint(-1.9) -> floor(-2.4) -> -3.0 (wrong answer)
    >
    > Test: rint(-2.1)
    > original version: rint(-2.1) -> floor(-1.6) -> -2.0
    > wrong version: rint(-2.1) -> floor(-2.6) -> -3.0 (wrong answer)- Hide quoted text -
    >
    > - Show quoted text -


    You are right, and mea culpa. I was thinking of the FAQ version that
    goes:

    14.6: How do I round numbers?

    A: The simplest and most straightforward way is with code like

    (int)(x + 0.5)

    This technique won't work properly for negative numbers,
    though (for which you could use something like
    (int)(x < 0 ? x - 0.5 : x + 0.5)).

    but floor() takes care of the square wave shape through the origin.
     
    user923005, Aug 28, 2007
    #8
  9. Raf

    Raf Guest

    hello everybody !
    Here's what I've finally implemented.


    double rint(double x)
    {
    //middle value point test
    if (ceil(x+0.5) == floor(x+0.5))
    {
    int a = (int)ceil(x);
    if (a%2 == 0)
    {return ceil(x);}
    else
    {return floor(x);}
    }

    else return floor(x+0.5);
    }

    This function allows to round a double to the nearest integer, and ,in
    the case of middle value points (as 0.5, -1.5, 34.5 ...), to round to
    the nearest even integer.
    This is how the GNU C "rint" functions works in most cases .
    Thanks a lot for your help and comments ...
    Have a nice end of week !!
     
    Raf, Aug 29, 2007
    #9
    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.

Share This Page