Rounding functions in microsoft visual C/C++

R

Raf

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 ....
 
M

Malcolm McLean

Raf said:
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++.
 
R

Raf

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 ...
 
A

Abdo Haji-Ali

Raf said:
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
 
C

Christopher Benson-Manica

[comp.lang.c] Abdo Haji-Ali said:
If your integers are always of the same sign then see the standard floor()
and ciel() (depending on your integers sign)

That's ceil().
 
U

user923005

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).
 
G

Gordon Burditt

My current problem is that the initial code is using a rounding to the
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)
 
U

user923005

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".


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.
 
R

Raf

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 !!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top