...

Average

Xiang
01-22-2003, 03:07 PM
Dear sir,

I want to find the average of the four values as below. Why my average was displayed in 15.676126 instead of the correct average is 15.676125.

#include <stdio.h>
#include <conio.h>

void main()
{
int v1 = 4;
int v2 = 42;
float v3 = 16.7;
float v4 = 0.0045;
float average;

clrscr();
printf("The four number are:\n");
printf("%i %i %g %g\n\n", v1, v2, v3, v4);
printf("The average is:\n");

average = (v1 + v2 + v3 + v4)/4;
printf("%f", average);
getch();
}


Thanks,

Xiang

codefox
01-23-2003, 02:56 PM
Probably a floating-point round-off error. Use double for higher precision



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum