473,783 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9202
"Mr. Ken" <Mr. Ken@asdf> wrote in message
news:44******** @news.starhub.n et.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(Floati ng const d)
{
return static_cast<Int egral>(
(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
2756
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
3206
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 discovered that they didn't put ming-utils pack). So I tried to download "raw" meterial from www.mingw.org: gcc-core binutils gcc-g++
21
14194
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 largish. I was under the assumption that /dev/random was "more random" than /dev/urandom, and that it would block if it ran out of entropy until it got more. Why am I seeing so many zeroes in my output? #include <stdio.h> #include <fcntl.h>...
0
1482
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 one of those machines, our "main dev." When I pull up the website in the VS IDE on that one machine, everything is fine. On any of the other machines, however, opening the website opens the site, then fills the Error List with just over a...
4
2985
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 <stdio.h> #include <string.h> int main(int argc, char **argv) {
2
11291
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 basic compiling on this new Dev C++, when i try to print something like: #include<iostream.h> using namespace std; int main () { cout << "Hello to the world" << endl; system("PAUSE") return 0;
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.