473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.

May 25 '06 #1
2 9174
"Mr. Ken" <Mr. Ken@asdf> wrote in message
news:44********@news.starhub.net.sg...
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;


May 25 '06 #2
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
May 25 '06 #3

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

Similar topics

4
by: Harald Mossige | last post by:
Dev-Cpp "dont se" MinGW. (Win2K) I have downloaded and run: devlup4991nomingw_setup.exe and MinGW-3.1.0-1.exe in those dirrectory:
4
by: Jakub \Quetz\ Lambrych | last post by:
I would like to know if anybody tried to install Mingw 3.4.2 on Dev-Cpp 4.9.9.0? The problem is that after installing official Dev-packs (from sf.net) compilator doesn't want to work (i...
21
by: Ron Peterson | last post by:
In the following piece of code, which simply generates a sequence of (random) octal codes, I'm surprised by the apparent non-randomness of /dev/random. It's not noticeable unless RAND_LENGTH is...
0
by: Gregory Gadow | last post by:
We have a number of development machines in our IT department, all running the same version of VS 2005 sp 1. Our company website and several compiled components were all written in VB.Net 2.0 using...
4
by: waltbrad | last post by:
What can I do to get Dev-Cpp to compile C code? Whenever I try compiling C code the compiler complains about the headers. For instance the code:(I do name the code with extension.c) #include...
2
by: renagade629 | last post by:
Can anybody help me understand what i'm doing wrong or what I'm missing? Is there anyother good and commendable C++ program I can use (free) from the internet like Dev C++? I'm having trouble doing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.