C code, converting float into integer

AI Thread Summary
The discussion revolves around a C function designed to calculate and output values to an Excel file. The user is encountering issues with converting a floating-point value to an integer, specifically when using the `pow` function. Initially, the user attempted to use `ceilf` to convert a float to an integer, but this resulted in incorrect outputs. It was suggested that using a cast would be more appropriate, as `ceilf` returns a float, not an int. The main problem identified was integer division in the expression `n/10`, which caused the result to always be zero for values of `n` from 1 to 9. Changing `10` to `10.0` allowed for floating-point division, yielding correct results. The user was advised to ensure that the math library is included and to enable all compiler warnings for better debugging. The conversation highlighted the importance of understanding data types and casting in C programming, ultimately leading to a resolution of the output issue.
jam12
Messages
37
Reaction score
0
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.
 
Technology news on Phys.org
The problem is you using a floating point format (%f) for an integer.
 
D H said:
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:
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?
 
D H said:
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).
 
Where are you telling the machine about pow and ceilf?

Turn all warnings on.

And use your debugger.
 
D H said:
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
 
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?
 
What compiler are you using?
 
  • #10
D H said:
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.
 
  • #11
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
 
  • #12
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.
 
  • #13
jam12 said:
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.
 
  • #14
r = (int) x;
 
  • #15
DH you genius, i just had to do 10 to 10.0 but why? i spent four hours on this piece of sh**
 
  • #16
D H said:
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.
 
  • #17
Thanks Mark44 for your explanation, very much appreciated. and thanks DH for keeping up with my mistakes.
 
Back
Top