# Thread: double/float output is inaccurate.

1. ## 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)
{

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;
}```

2. double/float output is inaccurate.
Yep. They sure are.

Quzah.

3. 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).

4. 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.

5. 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.

6. 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.

7. Originally Posted by slee8251
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.

8. Originally Posted by slee8251
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.