C code, converting float into integer

  • Thread starter jam12
  • Start date
  • #1
38
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.
 

Answers and Replies

  • #2
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
The problem is you using a floating point format (%f) for an integer.
 
  • #3
38
0
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:
  • #4
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
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?
 
  • #5
38
0
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).
 
  • #6
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
Where are you telling the machine about pow and ceilf?

Turn all warnings on.

And use your debugger.
 
  • #7
38
0
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
 
  • #8
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
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?
 
  • #9
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
What compiler are you using?
 
  • #10
38
0
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
38
0
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
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
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
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
684
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
harborsparrow
Gold Member
554
120
r = (int) x;
 
  • #15
38
0
DH you genius, i just had to do 10 to 10.0 but why? i spent four hours on this piece of sh**
 
  • #16
34,025
5,679
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
38
0
Thanks Mark44 for your explanation, very much appreciated. and thanks DH for keeping up with my mistakes.
 

Related Threads on C code, converting float into integer

Replies
9
Views
27K
Replies
4
Views
46K
Replies
7
Views
3K
Replies
1
Views
11K
  • Last Post
Replies
2
Views
3K
Replies
1
Views
3K
  • Last Post
Replies
4
Views
582
Replies
7
Views
1K
Replies
6
Views
5K
Replies
14
Views
2K
Top