Thread: double/float output is inaccurate.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    double/float output is inaccurate.

    This program is intended to read numbers into an array and to count how many positive, negative numbers there are and to also calculate the total for each category and calculate the average.

    Please use these numbers as input:
    -123.45
    -234.56
    576.1
    -9.345
    675.2
    100
    -10
    1654.45
    765.89
    0 (sentinel value) prints output once this is entered

    I hand calculated and used excel to verify that the negative total should be -377.3550 and the negative average should be -94.33875

    BUT when i run my program my out puts are -377.35499477 -94.33874869

    Can someone tell me what is causing the error?

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	float input[11];								
    	int elements = 0;								
    	double sum_positive = 0;						
    	double sum_negative = 0;						
    	double sum_overall = 0;							
    	int positivecount = 0;							
    	int negativecount = 0;							
    	double positive_ave = 0;						
    	double negative_ave = 0;						
    	double overall_ave = 0;							
    	int counter = 0;								
    
    
    	puts("Enter a set of numbers of no more than 10 numbers:");		
    
    
    	for(;;)
    	{
    		scanf("%f", &input[elements]);								
    
    
    		if(input[elements] == 0)					
    		{
    		break;														
    		}
    		
    		elements++;													
    
    
    		if (elements > 10)
    		{
    		puts("Error! You have entered more than ten numbers\n");	
    		break;
    		}
    	}
    	for (counter = 0; counter <= elements; counter++)
    	{
    		if(input[counter] > 0)
    		{
    			sum_positive += input[counter];							
    			++positivecount;										
    		}
    		if(input[counter] < 0)
    		{
    			sum_negative += input[counter];							
    			++negativecount;										
    		}
    	}
    
    
    	
    
    
    	positive_ave = sum_positive / (double) positivecount;			
    	negative_ave = sum_negative / (double) negativecount;			
    	sum_overall = sum_positive + sum_negative;						
    	overall_ave = sum_overall / elements;							
    
    
    	if(elements <= 10)												
    	{
    
    
    		puts("Input Read:");
    
    
    		for(counter = 0; counter <= elements; counter++)
    		{
    			if(input[counter] != 0)
    			{
    			printf("%10.4f\n",input[counter]);						
    			}
    		}
    
    
    		puts("\nStatistics:");
    		puts("Desc\t\tNumber\t\t   Total:\t   Average:");
    		printf("Positive\t%6d\t\t%10.4f\t%10.4f\n", positivecount, sum_positive, positive_ave);
    		printf("Negative\t%6d\t\t%14.8f\t%14.8f\n", negativecount, sum_negative, negative_ave);
    		printf("Overall\t\t%6d\t\t%10.4f\t%10.4f\n", elements, sum_overall, overall_ave);
    
    
    	}
    
    
    
    
    	return 0;
    }
    THANKS IN ADVANCE

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    double/float output is inaccurate.
    Yep. They sure are.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's probably nothing wrong. Floats (and doubles) are simply not entirely accurate. You should only print them out to some reasonable number of decimal places depending on your input. Maybe 3 decimal places in your case.

    You might want to make your input array double (and use %lf in scanf).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Can someone tell me what is causing the error?
    It's not an error as such. It's a consequence of the fact that doubles and floats are stored using a finite number of bits, so that there is a limit to how precise a number can be.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    23
    so is the lesson i should take from this is to never use floats and doubles in the same program? use one or the other?

    because i changed all my doubles to floats and now i get the right answer.

    I'm sure there is a way to use both but I am pressed for time so I cannot really look into it atm.

    Thanks for your help(s)

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The lesson is that since floating point values (whether float, double, long double, whatever) have:
    1) a binary representation, so decimal numbers are not always accurately represented
    2) limited precision

    In general, you should use double unless there's a reason to use float like you want to save space.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by slee8251 View Post
    so is the lesson i should take from this is to never use floats and doubles in the same program? use one or the other?
    Write down the precise value of pi on a piece of paper.

    When you've completed that task you will understand the lesson.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by slee8251 View Post
    so is the lesson i should take from this is to never use floats and doubles in the same program? use one or the other?

    because i changed all my doubles to floats and now i get the right answer.
    No, that is definitely not the lesson to learn here, in fact it's nonsense.
    I don't think you understand the problem at all.

    Read the link stahta01 posted.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion and inaccurate results
    By divineleft in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2006, 03:11 PM
  2. float vs Double
    By sughandh in forum C Programming
    Replies: 3
    Last Post: 07-16-2006, 04:26 AM
  3. double to float
    By jsbeckton in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 11:29 PM
  4. float; double; int
    By Lyanette in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2003, 12:04 PM
  5. Float,double,int
    By Bill 101 in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 02:09 PM