# Thread: how to avoid rounding?

1. ## how to avoid rounding?

Hello, people. I need help.

I need to write a program in C that uses a function to calculate the sum of all fractions 1/(a^4) for "a" values from 1 to x(which is scanf'ed), with 6 digits after the comma. I wrote the code:

Code:
```#include <stdio.h>
float f;
int function(int x) {
int a=1;
float e;
while(a<=x)
{
e=1/(a*a*a*a);
f=f+e;
a++;
}
return(f);
}```
The code seems to work (it works if I remove "1/", it DOES sum up all a^4's), but in the aforementioned case the answer is always 1.000000. The program seems to automatically round all values of 1/a*a*a*a to zero for a>1. How do I avoid this?

2. You're asking your function to return an int, but you're actually trying to return a float. Your compiler should be screaming bloody murder. Listen to it, and heed it's warnings. You might want to use a double for extra precision, since 1/a^4 can get real small real fast.

3. you need float or double to do division without the answer being truncated. So, you could either declare "a" to be "float" or double, or cast a^4 to float or double. And "1" need to be "1.0" to avoid implicit cast.

4. Thanks for your quick answers, my compiler (Visual Studio '10) didn't scream at all, but changing it to float function(int x) did do the trick!

Thanks again!

5. Originally Posted by Eldente
Thanks for your quick answers, my compiler (Visual Studio '10) didn't scream at all, but changing it to float function(int x) did do the trick!

Thanks again!
It wouldn't give you the error because it does implicit conversion to int for you.