edited by
382 views
3 votes
3 votes
#include <stdio.h>
   main()
   {
     float a ;
     double b ;
     scanf("%f %lf",&a, &b);
     if(a==b)
      printf ("true\n");
     else if(a>b)
      printf("a>b");
     else if(a<b)
      printf("a<b");
   }


 

Case 1:  input  $a=1, b=1$ output = true

Case 2:  input  $a= 1.1, b=1.1$ output=  a>b

case  3:  input  $a = 1.01, b= 1.01$ output = a<b

Can anyone explain output of  Case2 and Case3

edited by

1 Answer

1 votes
1 votes

This is because of different precision standards used for float and double. Float uses Single Precision and Double uses Double Precision.

https://www.geeksforgeeks.org/difference-float-double-c-cpp/

https://stackoverflow.com/questions/5098558/float-vs-double-precision

Now, 1.1 in binary is 1.00011001100 (approx.). In float the last digits(after radix point) are modified to store it as a float where as in double more "original" digits are saved. This modification actually makes 1.1 stored in float greater than 1.1 stored in double.

Similarly, 1.01 in binary is 1.00000010100 (approx.). Float's most significant digits after radix point(000000) makes it smaller than double after storing with modification.

It is easier to understand by adding the below line at the end of your program and see output for Case 2 and 3.

printf("\n%.18f %.18lf",a,b);

Related questions

1 votes
1 votes
1 answer
1
jugnu1337 asked May 10, 2022
285 views
SOURCE NAME::: UNDERSTANDING POINTER IN C (BOOK) BY YASHWANT KANETKARmy answer is C am i right?
1 votes
1 votes
0 answers
2
aakash pandey asked Apr 30, 2022
292 views
We use character array to declare string in C. So, if I declare an array likecharacter ch[ ] = {‘a’,’k’,’a’,’/o’};and try printing ch[3] like :printf(“%...
0 votes
0 votes
0 answers
3
Anirudh Kaushal asked Apr 6, 2022
202 views
#include<stdio.h struct marks { int p:3; int c:3; int m:2; }; void main() { struct marks s = {2, -6, 5}; printf("%d %d %d", s.p, s.c, s.m); }What does p:3 means here ?
0 votes
0 votes
0 answers
4