Thread: how to avoid rounding?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    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?

    Thanks in advance
    Last edited by Eldente; 01-27-2011 at 01:59 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    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.
    Last edited by nimitzhunter; 01-27-2011 at 02:09 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    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. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Eldente View Post
    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.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoid duplicate input issue
    By Eyesight in forum C Programming
    Replies: 6
    Last Post: 12-03-2010, 10:58 AM
  2. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  3. gcc inline assembler - rounding mode
    By mrsirpoopsalot in forum C Programming
    Replies: 10
    Last Post: 02-08-2010, 07:27 PM
  4. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  5. How to avoid a flickring in XOR_PUT
    By planet_abhi in forum Game Programming
    Replies: 3
    Last Post: 10-02-2003, 02:49 PM