I haven't written a blog for a long time. I 've been busy with my work recently. It's not that I have no time, but I have no idea how to sort out my ideas. This article is not a big deal, but it is indeed a problem I encountered recently. I encountered some inexplicable problems from time to time when I was debugging a program. These problems are the very tricky bugs that can be reproduced without regularity, I believe that people who have done development have deep experiences.
Of course, I fixed these bugs at the end. For my masters, there are no mistakes that cannot be fixed. (haha) it only takes time, but I found a small problem in C ++ programming. Oh, by the way, it is actually a small problem that will cause you troubles at the end. Let's get down to the truth and read the code:
- Int main (INT argc, char * argv [])
- {
- Double d1 = 0.1;
- Double D2 = 0.1f;
- Printf ("[%. 12f] [%. 12f]/n", D1, D2 );
- If (d1 <D2 | D2 <d1)
- Printf ("D1, D2 not equal !! /N ");
- Double D3 = 0.1;
- Printf ("[%. 12f] [%. 12f]/n", D1, D3 );
- If (d1 <D3 | D3 <d1)
- Printf ("d1, D3 not equal !! /N ");
- Return 0;
- }
The program is too simple to be simple. I will not talk about it much. Let's estimate the result and use your own compiler to verify it. I use VC ++ 6.0, this is my running result:
[0.100000000000] [0.100000001490]
D1, D2 not equal !!
[0.100000000000] [0.100000000000]
Press any key to continue
So 0.1 and 0.1f are different. 0.1f is float, while 0.1 is double. If 0.1f is assigned to D2, type conversion is required, of course there is no problem when you are lucky, but my luck is usually not very good. Maybe your compiler won't have a problem, or it won't happen every time, I won't verify it one by one.
Conclusion: when assigning values to the double type, you can simply write the regular decimal number. Do not add the suffix "F" to the double type. You may be anxious if something goes wrong someday.
Next time ...... Next time ...... Be sure to write a good article ......