C code, converting float into integer

Click For Summary

Discussion Overview

The discussion revolves around a C programming issue related to converting a float value into an integer within a function. Participants explore the behavior of the `ceilf` function, integer division, and casting, while addressing the output of a grid scanning algorithm. The scope includes technical explanations and debugging strategies.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant expresses the need to convert a float value to an integer using the `ceilf` function but encounters issues with the output being all zeros.
  • Another participant points out the misuse of the floating-point format specifier (%f) for an integer value, suggesting a change to %d.
  • A participant notes that changing the calculation of `r` to a different formula results in unexpected output, specifically `r` being printed as 1 multiple times.
  • Concerns are raised about the inclusion of necessary libraries and enabling compiler warnings to catch potential issues.
  • Participants discuss the implications of integer division in C, specifically how using integers in division affects the result, leading to incorrect calculations.
  • There is a suggestion to use casting to convert float to int, with examples provided, but confusion remains about the correct syntax and usage.
  • One participant finds success by changing the divisor from 10 to 10.0 to ensure floating-point division occurs, which resolves the issue of `r` being incorrectly calculated.

Areas of Agreement / Disagreement

Participants generally agree on the importance of using floating-point division and the need for proper casting, but there is no consensus on the best approach to resolve the initial issue with `ceilf` and the output of the function.

Contextual Notes

There are unresolved questions regarding the use of the `ceilf` function, the correct casting syntax, and the implications of integer division in the context of the calculations being performed.

Who May Find This Useful

Readers interested in C programming, particularly those dealing with type conversions, debugging, and mathematical computations in code.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K