• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help with a bit of code (C++)

Jaepheth

Platinum Member
I need to take a few integers, put them through a function, and then round UP to the next integer (0.000001 = 1)

I wrote the following test program:

Code:
#include <iostream>
#include <cmath>
#include <fstream>


int main(){
ofstream data;
data.open("test.csv");
int test=0;
	for(int i=10;i>0;i--){
		for(int j=10;j>0;j--){
			test=int(ceil(i/j)+.5);
			data<<i<<","<<j<<","<<test<<endl;
		}
	}
data.close();
}

which I opened in Excel and compared with the decimal values to get:

Code:
i   j  test  decimal
5	5	1	1
5	4	1	1.25
5	3	1	1.666666667
5	2	2	2.5
5	1	5	5
4	5	0	0.8
4	4	1	1
4	3	1	1.333333333
4	2	2	2
4	1	4	4
3	5	0	0.6
3	4	0	0.75
3	3	1	1
3	2	1	1.5
3	1	3	3
2	5	0	0.4
2	4	0	0.5
2	3	0	0.666666667
2	2	1	1
2	1	2	2
1	5	0	0.2
1	4	0	0.25
1	3	0	0.333333333
1	2	0	0.5
1	1	1	1

it seems to be always rounding down?

Does anyone see what I'm doing wrong?
(compiler in use: Cygnus' CygWin g++ 2.91.57)


PS. the real function is will be part of a rather rather large recursive algorithm, so clock cycles are a concern, which is why I'd rather not use something like:
(x/y%1)==0 ? return x/y : return x/y-(x/y%1);
... if that would even work.
 
int / int = int, correct? You'll need one of the components to be a double in order to obtain a decimal value.
 
Back
Top