0

So I've got the following code in C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
     printf("Please enter the heights of 3 players\n");
     float average;
     float height;
     for (int i=0; i<3; i++)
     {
         scanf("%f", &height);
         average+=height;
     }
     average = (float)((average + 2.06)/4);
     if (average<1.900 || average>2.100) printf("Illegal heights average, your average %.3f isn’t approved\n",average);
     else printf("Your heights average is %.3f, welcome to space jam tournament!!\n",average); 
    return 0;
}

Now, for most tests, it works just fine, however one test continually fails:

enter image description here

I'm completely baffled by it. Maybe it has something to do with the float variables? Would be glad for someone to help me out with this. Thanks ahead :)

Blabla
  • 121
  • 5
  • Apart from that, I'm no IEEE754 specialist, but calculating this by hand yields exactly 1.9, so by rounding errors, your `average` variable might be just a little less than your 1.9 literal. – PhilMasteG Nov 04 '22 at 15:56
  • Please post code, data, and results as text, not screenshots. http://idownvotedbecau.se/imageofcode – Barmar Nov 04 '22 at 16:11
  • due to your use of `float`s, this actually sums to 1.89999997615814208984375 or `0x1.e66666p+0` in hex format. this is obviously less than 1.9 hence your test is failing – Sam Mason Nov 04 '22 at 17:13

0 Answers0