+ 2
So what is 0.7d in this program??what does the d stand for?
8 Antworten
+ 4
Thats interesting.
It looks like it means decimal.
I did find this so interesting i needed to find an answer. And i found one linked below.
Here is a more explanation:
https://stackoverflow.com/questions/38278490/c-program-outputting-different-values-with-different-numbers
+ 4
The suffix "d" comes from Java but is totally useless, because a constant is double by default; you can remove it and nothing changes.
Now, the comparison a<0.7 between float and double in C is made using floats, so the double 0.7 - converted to float, which has less space - becomes minor than the true float a.
Hence the if is taken.
Try replacing "float a" with "double a" and the if is not taken because the comparison is made between doubles. Or try replacing if(a<0.7) with if(a<0.7f) and again the if is not taken because the comparison is made between floats!
0
Anna can you help with this?
0
0
Bilbo Baggins "Now,the comparison a<0.7 between float and double in C is made using floats,so the double 0.7-converted to float,which has less space -becomes a minor than the true float a." If 0.7 becomes minor to a wouldn't that mean a<0.7 would be false? Also once a variable is declared as float or double in C there is no use writing the f or d suffix right?Also why isn't there any output when I do this comparison👇
https://code.sololearn.com/cxRi8dH4u0gB/?ref=app
0
No, sir.
0.7 becomes minor than 0.7, not minor than a. So the comparison becomes:
if (a < 0.6999999999999999999999...), where a is 0.7
Regarding the assignment a=0.7d, in fact there is no use of putting f or d. The difference is made putting f in the comparison: 0.7f
Finally, the "doubt" code: 0.5 is one of the cases in which the truncation to float makes no harm, for the way a double is built.
0
And not always the truncation makes a value littler, again for the way a double is built.
In your code, 0.5 gives no output, as you pointed out; 0.6 gives A; 0.7 gives B.