1

I made a program that accepts up to and including 21 inputs of grade data and outputs the number of marks, the max and min, the standard deviation, and the letter grades. Thus, I made an array of size 21. Unfortunately, all of the loops in my program continue until array[i] != '\0', and I didn't know that 0 is equivalent to null, so it was originally quite broken for when people entered 0. To fix this problem, I said that if you enter 0, the array at that particular index instead is assigned 0.000001. This solved most of my problems, with the exception of my maxMin function, which for some reason still prints 0.000001 as the min mark. This function is seen below...

void maxMin() {
  float min = array[0];
  float max = array[0];
  for (int j = 1; array[j] != '\0'; j++) {
    if (min > array[j]) {
      min = array[j];
    }
    if (max < array[j]) {
      max = array[j];
    }
  }
  printf("The highest mark is: %f \n", max);
  if (min == 0.000001) {
    printf("The lowest mark is: 0 \n");
  }
  else {
    printf("The lowest mark is: %f \n", min);
  }
}

How can I get this function to print 0 as the min mark?

DrJessop
  • 462
  • 6
  • 26
  • 1
    `array[j]` is a `float` so it doesn't make much logical sense to compare to `'\0'` (a `char`). Are you not getting a complier warning? I would change to pass in the size of the array instead of relying on it ending with 0. This way 0 can be a valid value. – MFisherKDX Oct 22 '17 at 23:12
  • Just keep track of how many values have been inputted and pass that along to any function that needs it. – Retired Ninja Oct 22 '17 at 23:13
  • This is all fine and dandy, but why doesn't min evaluate to 0.000001? – DrJessop Oct 22 '17 at 23:15
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Retired Ninja Oct 22 '17 at 23:23
  • I don't see any calculations here that would cause floating point accuracy to be an issue. Floats behave exactly like you'd expect if all you ever do with them is copy and compare them. – aschepler Oct 22 '17 at 23:37

2 Answers2

1

The problem is this statement:

if (min == 0.000001) {

0.000001 will be treated as double. The decimal number 0.000001 is represented differently as a single-precision float and as a double-precision double.

You need to put suffix f with float literal:

if (min == 0.000001f) {

When you put f suffix, it tells the compiler that this is a float.

H.S.
  • 11,654
  • 2
  • 15
  • 32
  • You can compare a double and a float. The issue might be precision in this particular comparison. – nicomp Oct 22 '17 at 23:32
  • This floating point suffix (or absence thereof) is not the problem. Equality should be less than or equal. And it might be possible to use `%.2f` in the format string to avoid needing a special case at all. – Jonathan Leffler Oct 22 '17 at 23:33
  • @JonathanLeffler Can you please elaborate this a little bit more - "Equality should be less than or equal." As per my understanding, here, the number `0.000001` is represented differently as a single-precision (float) and as a double-precision (double). Because of promotion rules, `min` will be promoted to `double` and the result is quite a bit different from the `double` representing `0.000001`, so the comparison (==) fails. – H.S. Oct 22 '17 at 23:57
  • For my purposes, this works perfectly. Thank you very much. – DrJessop Oct 25 '17 at 20:00
0

As another answer stated:

if (min == 0.000001) 

is the source of the problem.

However, adding the 'F' suffix is not the whole answer.

The whole answer is that comparing float or double values will (usually) fail because many/most numbers cannot be exactly represented in float or double

However, the posted code has some other problems, like the comparison of a float to a char when trying to determine if at end of numbers. Strongly suggest revising the process of inputting the 21 numbers so the code keeps track of how many numbers were input and pass that count to the minmax() function.

user3629249
  • 16,402
  • 1
  • 16
  • 17