(So far, 825 people got it right out of 2196 for a success rate of 37%)

What will be the output from the following application:
(Edited 2022-07-28 based on @jms_nh's comment below)

#include<stdio.h>
int main()
{
float Value=0.9;

if(Value < 0.9)
{
printf("A");
}
else
{
printf("B");
}

return 0;
}

Pick one:
A
B
Implementation-Dependent
None of the above

[ - ]
Comment by July 26, 2022

The original answer A (it has since been changed) is incorrect. Floating-point types in C are not guaranteed to have the standard IEEE-754 sizes (32-bit for float and 64-bit for double).

Although it's unlikely, there could be a C language implementation where float and double are both 32-bit, in which case no conversion occurs and the program would print "B".

(Also, I haven't counted bits, but those binary constants don't look right. IEEE-754 floats have 24-bit significands and doubles have 53-bit significands, so the truncated binary representation of 0.9 should be more than twice as long in the case of a 64-bit double. Just sayin'.)

---

Update: in fact, there is such an implementation.

MSP430 had GCC versions where float/double sizes were 32/32 bits and in more recent version they were 32/64 bits.

MSP430 GCC 5.3.0 https://godbolt.org/z/3ec3GKG3n

MSP430 GCC 4.5.3 https://godbolt.org/z/W9qzdf1Tn

---

Furthermore we haven't even started talking about floating-point rounding modes....

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n...

(my emphasis)

When a double is demoted to float, a long double is demoted to double or float, or a value being represented in greater precision and range than required by its semantic type (see 6.3.1.8) is explicitly converted (including to its own type), if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.

[ - ]
Comment by July 28, 2022

I guess the answer has been changed, because the answer is correct, but the explanation is still not correct:

Even the result in the "Most of the time:" case, the result is implementation defined, according to the standard quoted above (6.4.4.2 Floating constants):

For decimal floating constants, and ..., the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner.

So the binary respresentation could also be the next larger value, instead of the next smaller, or nearest representable value, as given in the explanation, in which case it would print B.
(I doubt that any implementation would do that, though)

[ - ]
Comment by July 28, 2022

I suspected A and got it.  My slightly modified version of your program shows why:

$cat test59.c #include <stdio.h> int main() { float Value=0.9; if (Value < 0.9) { printf("A"); } else { printf("B"); } printf("\nValue = %20.18f\n", Value); printf ("C uses %d bytes to store a float in memory\n", (int) sizeof(float)); return 0; }$gcc -Wall test59.c -o test59.o

\$ test59.o
A
Value = 0.899999976158142090
C uses 4 bytes to store a float in memory

There is always significant roundoff error representing floating point numbers with floats, esp. in only 4 bytes.    You must test, test,  test the code.    And then test after EVERY change to the  source code.  Floats not so good for algorithmic decision-making in embedded work, IMHO.    The smaller the word size in bits, the greater the roundoff error will be.

[ - ]
Comment by August 4, 2022

Excellent analysis and answer as always.

Thanks,

Dave

[ - ]
Comment by July 29, 2022

Can anybody explain to me how this code can output text Implementation-Dependent?

Thanks

[ - ]
Comment by October 15, 2022
f(Value < 0.9f)   // notice the 'f'
{
printf("A");
}
else
{
printf("B");
}

This should give the right desired answer, "B". You must qualify 0.9 with 'f'

To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.