-4

1 st Program

#include"stdio.h" 
int main()
{
    float a=0.7d; 
    if(a<0.7)
         printf("C");
        else
         printf("C++");
    return 0;
}

This program outputs C where as the 2 nd Program

#include"stdio.h" 
int main()
{
    float a=0.8d; 
    if(a<0.8)
         printf("C");
        else
         printf("C++");
    return 0;
}

output C++

Why it is happening like this They should give the same ouput for any value of a . What is happening in the code ?

Can anybody Help me in understanding it ?

  • 1
    `a` is `float`. `0.7` is `double`. `0.7f` would be `float`. – GSerg Apr 01 '18 at 11:56
  • Another similar topic (covering the literals case): https://stackoverflow.com/questions/1839422/strange-output-in-comparison-of-float-with-float-literal – Michal Lonski Apr 01 '18 at 12:02
  • It is actually 0.7d not 0.7f For reference open this (https://www.geeksforgeeks.org/output-of-c-programs-set-65-if-else/) **link** and see question 5 – a srinivasa reddy Apr 01 '18 at 12:03
  • The `0.7d` is irrelevant because it's stored in a `float` before it's compared to anything. It's the next one where the suffix matters. – GSerg Apr 01 '18 at 12:08
  • 2
    Each program, in effect, asks whether rounding a given double to float reduces its value. That depends on the value, so there is no reason to expect the same answer. The closest double to 0.7 is 0.6999999999999999555910790149937383830547332763671875, float conversion 0.699999988079071044921875, which reduces the value. The closest double to 0.8 is 0.8000000000000000444089209850062616169452667236328125, float conversion 0.800000011920928955078125, increasing the value. – Patricia Shanahan Apr 01 '18 at 13:33
  • @PatriciaShanahan How can one know about these conversions (that too in such detail), any links> – infinite Apr 01 '18 at 15:30
  • I wrote a very short Java program that uses BigDecimal to print exact decimal expansions of floating point numbers. There are also web sites such as http://www.exploringbinary.com/floating-point-converter/ that can do the conversions. – Patricia Shanahan Apr 01 '18 at 15:59

1 Answers1

1

Both numbers cannot be exactly represented in binary.

In the case of 0.7, the float representation is less than the double representation.

In the case of 0.8 it is the other way round because the float is rounded up.

Here's what printf with "%25.18f format outputs:

 0.7 as double: 0.699999999999999956
 0.7 as float:  0.699999988079071045
 0.8 as double: 0.800000000000000044
 0.8 as float:  0.800000011920928955

(These are the same numbers that @PatriciaShanahan has generated for her comment in a slightly different way.)

The common solution for issues like this is to allow a small tolerance when comparing floating point numbers.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • Any links for the float getting rounded up part? – infinite Apr 01 '18 at 12:05
  • 1
    @infinite It's rounded up [for the particular value of `0.8`](https://stackoverflow.com/questions/49597704/why-these-two-programs-of-same-type-giving-different-outputs#comment86204409_49597704), not as a general rule. – GSerg Apr 01 '18 at 14:06