Two Questions: Custom rounding, and checking for whole numbers

Hi, I have taken computer programming at my school for half a semester now. I have browsed these forums for awhile and got help from other peoples posts, but I could not find ones that fit me that well.
I recently got a project in math, that when I looked at it, would be easily done with writing a program for it, except, I have a couple problems. The project has to do with raising dear to experiment with exponential equations. It is actually quite fun.
In said program, I want to round numbers from X.001 - X.5 down, and X.501 to X.999 up. I do not see how I could substitute a number in for X to work in any and every case, and, the rounding rules are a bit custom because the X.5 rounds down.
For the rounding I was looking at some posts and it said to add .5 and if it is above the next whole number it rounds up, if it is not, it rounds down. The only problem I have is that when I have a .5, and add .5, it goes to one and rounds to one, but, I need it to round down it zero. Instead of adding .5, could I just add .499999 or something like that?
I want to encase this in an if statement that looks something like:

if(X != whole number)
{
~round stuffs
}

If someone could write it for me, or preferably help me through the steps, I would be very happy.

Thanks in advance,
Sam

Edit: A couple more questions I just thought of. Is endl; or '\n' more proficient since they both do the same thing.
And, is including -using namespace std- more or less proficient than doing std::cout for all of those lines? Or does it not really matter?
Last edited on
And, is including -using namespace std- more or less proficient than doing std::cout for all of those lines? Or does it not really matter?

Just put using std::cout; at the top of your code.

Here's a generic example on how you could round numbers in this way:
1
2
3
4
5
6
double x = 12.4325;
x = (int)(x +.4999); //typecasted to int to truncate the variable
cout << x;
x = 12.5;
x = (int)(x+.4999);
cout << x;


Try it for different values of x and it should do what you want. It would be harder though to round to something other than a whole number.
That was not exactly what I was looking for. I was looking for a snippet of code that would:
1. Take the value of a variable
But, the value of the variable is not set. The program gets the value of the variable from a previous step.
The value of the variable changes every time I execute a function within my main.
2. Used custom rounding rules listed above
closed account (4Gb4jE8b)
No one's here for doing your homework.

But to get an inputted value use

1
2
3
double x; //or float if you prefer, all depends on precision you want
cout << "Enter a number: ";
cin >> x;


and then run it through whatever rounding rules you need.

'\n' is the ascii form of endl, where is endl technically is std::endl, they do the same thing, i've seen nowhere it actually matters down to an OS level, it just depends on what you're used to and if you're using namespace std (or specify std::endl) for speed of typing purposes.

in math.h there are also functions for rounding such as ceil() and floor() and round() which you may consider using.
Last edited on
I am not asking you to do my homework, I am asking for someone to help me with something that I have no idea how to do. If I have to, I can do my homework by hand but I love programming and thought it would be fun to write a program to do it.

But how do I ask for user input, then round it up or down. I cannot use, or I do not know how to use:

if( x (is between) x.000001 and x.5)
{
round down
}

if( x (is between) x.500000001 and x+1)
{
round down
}

I have no idea how to do that, and I have no idea how to get user input for X and round it down or up with a small bit of code.
Article: Rounding Algorithms
http://www.cplusplus.com/forum/articles/3638/
That helps with all those algorithms except for one thing, or I am just missing it. I am quite new to computer programming and c++, and don't quite get some things.
I don't see how I could say:
if( x (is between) x.000001 and x.5)
{
(use algorithm)
}

if( x (is between) x.500000001 and (the next whole number))
{
(use algorithm)
}

What I specifically do not get how to do is substitute a value in for X, for it to work after I say 'is between'.
If anyone has any answers to this, I would greatly appreciate it.
if x is between y and z, not including x==y or x==z
if(y<x&&x<z)
if x is between y and z, including x==y or x==z
if(y<=x&&x<=z)
Thats not what I am exactly looking for. Every time I run the function, I get a new value for X, so I cannot necessarily say that. What I am going to do, is actually post the section of code that I need to use this on so people can see what I mean.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void upyear()
{
	ab = ab + yb;
	ad = ad + yd;
	yb = fb;
	yd = fd;
	if(ab >= ad)
	{
		fb = ad/2;
		fd = ad/2;
	}
	if(ab < ad)
	{
		if(2*ab >= ad)
		{
			fb = ad/2;
			fd = ad/2;
		}
		if(2*ab < ad)
		{
			fb = ad;
			fd = ad;
		}
	}
	cout << "Year " << year << ":\n";
	cout << "Adult Bucks - " << ab << '\n';
	cout << "Adult Does - " << ad << '\n';
	cout << "Yearling Bucks - " << yb << '\n';
	cout << "Yearling Does - " << yd << '\n';
	cout << "Fawn Bucks - " << fb << '\n';
	cout << "Fawn Does - " << fd << '\n';
	population = ab + ad + yb + yd + fb + fd;
	cout << "Population - " << population << "\n\n";

	cout << "Year " << year << " with survival rates:\n";
	cout << "Adult Bucks - " << ab*0.95 << '\n';
	cout << "Adult Does - " << ad*0.95 << '\n';
	cout << "Yearling Bucks - " << yb*0.85 << '\n';
	cout << "Yearling Does - " << yd*0.85 << '\n';
	cout << "Fawn Bucks - " << fb*0.7 << '\n';
	cout << "Fawn Does - " << fd*0.7 << '\n';
	population = (ab*0.95) + (ad*0.95) + (yb*0.85) + (yd*0.85) + (fb*0.7) + (fd*0.7);
        // ^ This line is what I need help with ^
	cout << "Population - " << population << "\n\n";

	year = year + 1;
	cin.ignore();
}


With the last bit of coding where it starts with:
cout << "Year " << year << " with survival rates:\n";
You can see that I am multiplying those numbers by decimals. And with the assignment, I need to round numbers (ab, ad, yb, yd, fb, fd) that have a decimal equivalent to
X.00000 to X.5 - Round down
X.50001 to X.999999 - Round up

In earlier posts, I have seen how to round and I can use that. But, I do not know how to say 'X' is different for every occasion that I use it.
Thanks in advance.
Last edited on
edit: nevermind
Last edited on
Bump?
If no one thinks this is possible, just post that so I don't spam these forums with bumps and bumps. Bump?
I'm not sure what exactly you are having trouble with.

If you want to perform a calculation multiple times with different inputs, that is what function is for:

 
#include <cmath> 
1
2
3
4
double hypotenuse( double x, double y )
  {
  return std::sqrt( (x * x) + (y * y) );
  }
1
2
3
4
5
6
7
8
9
double x = 3;
double y = 4;
double z = hypotenuse( x, y );
cout << "(" << x << ", " << y << ", " << z << ")\n";

x = 9;
y = 12;
z = hypotenuse( x, y );
cout << "(" << x << ", " << y << ", " << z << ")\n";

Etc.
what exactly is the question? you asked

I don't see how I could say:
if( x (is between) x.000001 and x.5)
{
(use algorithm)
}

if( x (is between) x.500000001 and (the next whole number))
{
(use algorithm)
}

What I specifically do not get how to do is substitute a value in for X, for it to work after I say 'is between'.


and rocketboy9000 answered with

if x is between y and z, not including x==y or x==z
if(y<x&&x<z)
if x is between y and z, including x==y or x==z
if(y<=x&&x<=z)


which seemed like a valid answer to me, I don't think anyone knows what you are asking.

I'm not sure what you need either, but it seems like you need to get odd roundings at odd spots or something, you could make a small function somewhere that just does the rounding and sets those variables and call the function where you need it.


oh, wait... are you asking to mask off the bottom part just to check for that? You could floor the number and subtract it that from itself..

basically if x has a point value 17.1341 and you just need the .1341, you could do this

( x-floor(x) ) which would be (17.1341 - 17) to equal just the .1341 part of the number

Is that what you are looking for?
What I mean is I have no idea what the value of X is, the program is going to run and get it for me.
Heres an example: X=15.35
if( 15.35(is between) 15.500000001 and 15.5)
{
(use algorithm)
}

if( 15.35 (is between) 15.500000001 and (the next whole number))
{
(use algorithm)
}

And another: X=20.76

if( 20.76(is between) 20.500000001 and 20.5)
{
(use algorithm)
}

if( 20.76 (is between) 20.500000001 and (the next whole number))
{
(use algorithm)
}

I guess once I look at it, I did not explain it as thoroughly as I could.
Here is a function to perform .5 rounding:
1
2
3
4
5
int round5(double x){
	double f=x-floor(x);
	if(f>0.5)return floor(x)+1;
	else return floor(x);
}
Last edited on
That bit of code.. really helped. I took the idea of where you took x and subtracted floor(x) and came up with:
1
2
3
4
5
6
7
8
9
10
11
12
void round()
{
	f = (ceil(x))-x;
	if(f >= 0.5)
	{
		x = floor(x);
	}
	if(f < 0.5)
	{
		x = ceil(x);
	}
}

When I get X, it does exactly what I wanted. Thank you all for helping me, and, especially you rocketboy9000.
What? no props? I said the same thing... well, sorta... pooh
LOL.

100
101
102
103
104
105
106
107
  //--------------------------------------------------------------------------
  // Common rounding: round half up
  // Bias: +Infinity
  template <typename FloatType>
  FloatType roundhalfup( const FloatType& value )
    {
    return std::floor( value +0.5 );
    }
    http://www.cplusplus.com/forum/articles/3638/#msg15550

Of course, you are perfectly within your rights to do things the hard way, with precision errors, if you really want to...

Still hoping this helps.
.... I was typing something and there is this button on my mouse that just closes the page I was using. So I'm just going to sum up what I typed earlier.
Cranium:
When I read yours, I did not know that it was just the point value I needed, when it actually turned out to be exactly what I needed.
When rocketboy put the code on the page, it just clicked in my head.
Duoas:
I am a beginner in c++, and I do not understand half of what that says, namely, the template and FloatType lines.
How I did it, just makes sense to me, which is why I am choosing to do it that way.

Thanks to everyone who contributed in helping me solve this.

I am going to post my code on here, so you guys can see what I was doing. And, it is the largest successful program I wrote, sort of.

It was given to us in my Pre Calculus class to model growing a deer herd to demonstrate exponential functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
using namespace std;

float ab, ad, yb, yd, fb, fd, x, f;
int year = 1;
int abht, adht, nabht, nadht, fbht, fdht, nfbht, nfdht, check, check1, check2, check3, fcheck, population, spopulation;

void getinfo();
void upyear();
void huntingtags();
void round();

int main()
{
	getinfo();
	cin.ignore();
	while(year <= 20)
	{
		while(population < 1000)
		{
			upyear();
		}
		while(population >= 1000)
		{
			huntingtags();
			upyear();
			if( population >= 1500)
			{
				cout << "Population exceeded 1500. Population was: " << population << ".\n";
				cin.ignore();
				return 0;
			}
		}
		if(year == 20 && 1500 < population > 1450)
		{
			cout << "Population: " << population << ".\n";
			cin.ignore();
			return 0;
		}
		if(year == 20 && population > 1450)
		{
			cout << "Population did not reach 1450. Population was: " << population << ".\n";
			cin.ignore();
			return 0;
		}
	}
	return 0;
}
void getinfo()
{
	cout << "How many adult bucks do you have?\n";
	cin >> ab;
	cin.ignore();
	system("cls");
	cout << "How many adult does do you have?\n";
	cin >> ad;
	cin.ignore();
	system("cls");
	cout << "How many yearling bucks do you have?\n";
	cin >> yb;
	cin.ignore();
	system("cls");
	cout << "How many yearling does do you have?\n";
	cin >> yd;
	cin.ignore();
	system("cls");
	cout << "How many fawn bucks do you have?\n";
	cin >> fb;
	cin.ignore();
	system("cls");
	cout << "How many fawn does do you have?\n";
	cin >> fd;
	cin.ignore();
	system("cls");
	cout << "You have " << ab << " adult bucks.\n";
	cout << "You have " << ad << " adult does.\n";
	cout << "You have " << yb << " yearling bucks.\n";
	cout << "You have " << yd << " yearling does.\n";
	cout << "You have " << fb << " fawn bucks.\n";
	cout << "You have " << fd << " fawn doe.\n";
	population = ab + ad + yb + yd + fb + fd;
	cout << "You have " << population << " total dear.\n";
}
void upyear()
{
	ab = ab + yb;
	ad = ad + yd;
	yb = fb;
	yd = fd;
	if(ab >= ad)
	{
		fb = ad/2;
		fd = ad/2;
	}
	if(ab < ad)
	{
		if(2*ab >= ad)
		{
			fb = ad/2;
			fd = ad/2;
		}
		if(2*ab < ad)
		{
			fb = ad;
			fd = ad;
		}
	}

	if(fb - fd ==  0 && fcheck == 0)
	{
		fd = ceil(fd);
		fb = floor(fb);
		fcheck = 1;
	}

	if(fb - fd ==  0 && fcheck == 1)
	{
		fb = ceil(fb);
		fd = floor(fd);
		fcheck = 0;
	}

	cout << "Year " << year << ":\n";
	cout << "Adult Bucks - " << ab << '\n';
	cout << "Adult Does - " << ad << '\n';
	cout << "Yearling Bucks - " << yb << '\n';
	cout << "Yearling Does - " << yd << '\n';
	cout << "Fawn Bucks - " << fb << '\n';
	cout << "Fawn Does - " << fd << '\n';
	population = ab + ad + yb + yd + fb + fd;
	cout << "Population - " << population << "\n\n";

	ab = ab*0.95;
	x = ab;
	round();
	ab = x;
	
	ad = ad*0.95;
	x = ad;
	round();
	ad = x;

	yb = yb*0.85;
	x = yb;
	round();
	yb = x;

	yd = yd*0.85;
	x = yd;
	round();
	yd = x;

	fb = fb*0.7;
	x = fb;
	round();
	fb = x;

	fd = fd*0.7;
	x = fd;
	round();
	fd = x;

	cout << "Year " << year << " with survival rates:\n";
	cout << "Adult Bucks - " << ab << '\n';
	cout << "Adult Does - " << ad << '\n';
	cout << "Yearling Bucks - " << yb << '\n';
	cout << "Yearling Does - " << yd << '\n';
	cout << "Fawn Bucks - " << fb << '\n';
	cout << "Fawn Does - " << fd << '\n';
	spopulation = ab + ad + yb + yd + fb + fd;
	cout << "Population - " << spopulation << "\n\n";

	year = year + 1;
}
void huntingtags()
{
	while(check == 0)
	{
		cout << "How many adult buck hunting tags do you want?\n";
		cin >> abht;
		cin.ignore();
		cout << "How many adult doe hunting tags do you want?\n";
		cin >> adht;
		cin.ignore();
		cout << "How many fawn buck hunting tags do you want?\n";
		cin >> fbht;
		cin.ignore();
		cout << "How many fawn doe hunting tags do you want?\n";
		cin >> fdht;
		cin.ignore();
		cout << '\n';
		check = 1;
	}
	while(check == 1 && check2 == 1)
	{
		while(check3 == 0)
		{
			check1 = 1;
			cout << "How many adult buck hunting tags do you want?\n";
			cin >> nabht;
			cin.ignore();
			cout << "How many adult doe hunting tags do you want?\n";
			cin >> nadht;
			cin.ignore();
			cout << "How many fawn buck hunting tags do you want?\n";
			cin >> nfbht;
			cin.ignore();
			cout << "How many fawn doe hunting tags do you want?\n";
			cin >> nfdht;
			cin.ignore();
			cout << '\n';
			if(nabht-abht >= 6)
			{
				cout << "\nYour new hunting tag amount cannot be 5 more or 5 less than the amount you\ndid last year.\nLast year you had: " << abht << " adult buck hunting tags.\n";
			}
			if(nadht-adht >= 6)
			{
				cout << "\nYour new hunting tag amount cannot be 5 more or 5 less than the amount you\ndid last year.\nLast year you had: " << adht << " adult doe hunting tags.\n";
			}
			if(nfbht-fbht >= 6)
			{
				cout << "\nYour new hunting tag amount cannot be 5 more or 5 less than the amount you\ndid last year.\nLast year you had: " << fbht << " fawn buck hunting tags.\n";
			}
			if(nfdht-fdht >= 6)
			{
				cout << "\nYour new hunting tag amount cannot be 5 more or 5 less than the amount you\ndid last year.\nLast year you had: " << fdht << " fawn doe hunting tags.\n\n";
			}
			if((nabht-abht <= 5 && nadht-adht <= 5 && nfbht-fbht <= 5 && nfdht-fdht <= 5) && (nabht-abht >= -5 && nadht-adht >= -5 && nfbht-fbht >= -5 && nfdht-fdht >= -5))
			{
				nadht = adht;
				nabht = abht;
				nfbht = fbht;
				nfdht = fdht;
				check = 2;
				check3 = 1;
			}
			if(!(nabht-abht <= 5 && nadht-adht <= 5 && nfbht-fbht <= 5 && nfdht-fdht <= 5) && !(nabht-abht >= -5 && nadht-adht >= -5 && nfbht-fbht >= -5 && nfdht-fdht >= -5))
			{
				cout << '\n';
			}

		}
	}
	if(check1 == 0)
	{
		ab = ab - abht;
		ad = ad - adht;
		fb = fb - fbht;
		fd = fd - fdht;

	}
	if(check1 == 1)
	{
		ab = ab - nabht;
		ad = ad - nadht;
		fb = fb - nfbht;
		fd = fd - nfdht;
	}
	if(check == 2)
	{
		check = 1;
	}
	check3 = 0;
	check2 = 1;
}
void round()
{
	f = (ceil(x))-x;
	if(f >= 0.5)
	{
		x = floor(x);
	}
	if(f < 0.5)
	{
		x = ceil(x);
	}
}

Last edited on
Topic archived. No new replies allowed.