# C code, converting float into integer

Hi guys, i am using the C language and have created the following function:

void counter (double c) {
FILE *fp;
char output[]="output.xls";
int n,p=0;
int r=0;
int i,j;
float x=0;
fp=fopen(output,"w");
fprintf(fp,"rmax\tnumber of particles within rmax\r\r\n");
for(n=1;n<=10;n++) {
x=pow(c,n/10);
r=ceilf(x); /*here i need to convert the float x into integer r, since in the for statements below, r must be an integer */
for(i=-r;i<=r;i++) {
for(j=-r;j<=r;j++) {
if(grid[N/2+i][N/2+j]) {
if(sqrt(i*i+j*j)<=r) p++;
}
}

}
fprintf(fp,"%f\t%d\r\n",r, p);
p=0;

}
fclose(fp);

}

Basically as the comment in the code above says...
I need to convert my r value to be of type int that takes a value of the integer part of float x.
I thought the ceil function would do the trick, but it doesnt, my file just prints all zeros for r.
Any help would be appreciated thanks.

## Answers and Replies

Related Programming and Computer Science News on Phys.org
D H
Staff Emeritus
Science Advisor
The problem is you using a floating point format (%f) for an integer.

The problem is you using a floating point format (%f) for an integer.
thanks changed that to %d but its still no use, basically my for function is scanning a grid in integer steps and grid function must take integer values as arguments

actually that above statement is not true. The following works perfectly:
void counter (double c) {

FILE *fp;
char output[]="output.xls";

int n,p=0;
int i,j;
float r=0;
fp=fopen(output,"w");
fprintf(fp,"rmax\tnumber of particles within rmax\r\r\n");
for(n=1;n<=10;n++) {
r=c*n/10; /*need r=pow(c,n/10) instead*/

for(i=-r;i<=r;i++) {
for(j=-r;j<=r;j++) {
if(grid[N/2+i][N/2+j]) {
if(sqrt(i*i+j*j)<=r) p++;
}
}

}
fprintf(fp,"%f\t%d\r\n",r, p);
p=0;

}
fclose(fp);

}

however when i change r to be some other calculation (such as pow(c,n/10)) then i only get r=1 printed nine times in the file

Last edited:
D H
Staff Emeritus
Science Advisor
Are you compiling with all warnings enabled? (Always enable all warnings.)

If you were to do this I strongly suspect the compiler is going to complain about the function "ceilf".

Why are you using ceilf, period? What is wrong with a cast?

Are you compiling with all warnings enabled? (Always enable all warnings.)

If you were to do this I strongly suspect the compiler is going to complain about the function "ceilf".

Why are you using ceilf, period? What is wrong with a cast?
scrap my first post, please read the second one and by the way when counter is called, the c variable is 80.62782 (changes depending on other parts of program).

D H
Staff Emeritus
Science Advisor
Where are you telling the machine about pow and ceilf?

Turn all warnings on.

And use your debugger.

Where are you telling the machine about pow and ceilf?

Turn all warnings on.

And use your debugger.
i have included math library and gl glut library in program, so when i compile, these are included. All warnings are on

D H
Staff Emeritus
Science Advisor
Where is your #include <math.h> ?

And why are you using ceilf?
Issue #1: It works on floats, not doubles.
Issue #2: It returns a float, not an int.
Issue #3: What is wrong with a cast?

D H
Staff Emeritus
Science Advisor
What compiler are you using?

What compiler are you using?
I am quite new to programming, i am using linux server connected to our campus. It has glut installed etc.
Also math library is included right above main() (beginning of the whole code)
Counter is just a single function in the whole code.

how can I use cast? eg would it be
r=pow(c,n/10);
r=cast(r);

Do i need to include a library for this? i tried this and it didnt work

D H
Staff Emeritus
Science Advisor
In the current implementation, you should be seeing r having a value of 1 for the first 9 iterations. It should be c for the last iteration.

Both the n and the 10 in n/10 are integers. The compiler is using integer division.

Change that 10 to 10.0.

D H
Staff Emeritus
Science Advisor
how can I use cast? eg would it be
r=pow(c,n/10);
r=cast(r);

Do i need to include a library for this? i tried this and it didnt work
cast isn't a function.

Code:
double x;
int i;
i = x;
That "i=x" is a cast.

harborsparrow
Gold Member
r = (int) x;

DH you genius, i just had to do 10 to 10.0 but why? i spent four hours on this piece of sh**

Mark44
Mentor
In the current implementation, you should be seeing r having a value of 1 for the first 9 iterations. It should be c for the last iteration.

Both the n and the 10 in n/10 are integers. The compiler is using integer division.

Change that 10 to 10.0.
To elaborate on what D H said, when n is 1, 2, 3, ..., 9, n/10 evaluates to 0, so you are raising c to the power 0, which gives you 1. Whenever you have a division expression with both operands being integral types (int, short, long, char, and the unsigned or signed variants), integer division is performed. If at least one of the operands is a floating point type (float or double or long double), floating point division is performed.

For example, 5/2 == 2, while 5.0/2, 5/2.0, and 5.0/2.0 all evaluate to 2.5.

When n is 10, n/10 evaluates to 1 (not a surprise), so you are raising c to the power 1, which is c.

Thanks Mark44 for your explanation, very much appreciated. and thanks DH for keeping up with my mistakes.