C code, converting float into integer

In summary: 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.
  • #1
jam12
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.
 
Technology news on Phys.org
  • #2
The problem is you using a floating point format (%f) for an integer.
 
  • #3
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:
  • #4
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
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).
 
  • #6
Where are you telling the machine about pow and ceilf?

Turn all warnings on.

And use your debugger.
 
  • #7
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
 
  • #8
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
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.
 

1. How do I convert a float into an integer in C code?

To convert a float into an integer in C code, you can use the int() function. This function rounds down the float to the nearest whole number and returns an integer value. For example, if the float is 5.9, the int() function will return the integer 5.

2. Can I convert a float into an integer without losing precision?

No, when converting a float into an integer, you will inevitably lose precision. This is because an integer can only represent whole numbers, while a float can represent decimal numbers as well. Therefore, converting a float into an integer will result in rounding down and losing the decimal part of the number.

3. How can I convert a float into an integer without rounding down?

If you want to convert a float into an integer without rounding down, you can use the ceil() function. This function rounds the float up to the nearest whole number and returns an integer value. For example, if the float is 5.1, the ceil() function will return the integer 6.

4. What happens if I try to convert a float into an integer that is too large for the integer data type?

If the float is too large for the integer data type, the result of the conversion will be undefined. This is because an integer has a limited range of values that it can represent, while a float can have a much larger range. It is important to make sure that the float value is within the range of the integer data type before attempting to convert it.

5. Is there a way to check if a float can be converted into an integer without losing precision?

Yes, you can use the isfinite() function to check if a float value is within the range of the integer data type. This function returns true if the value is within the range and false if it is too large or too small. You can use this function to ensure that the float can be safely converted into an integer without losing precision.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
4
Views
614
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
8
Views
877
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top