A "small problem" that may be ignored in C ++ Programming"

Source: Internet
Author: User

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:

  1. Int main (INT argc, char * argv [])
  2. {
  3. Double d1 = 0.1;
  4. Double D2 = 0.1f;
  5. Printf ("[%. 12f] [%. 12f]/n", D1, D2 );
  6. If (d1 <D2 | D2 <d1)
  7. Printf ("D1, D2 not equal !! /N ");
  8. Double D3 = 0.1;
  9. Printf ("[%. 12f] [%. 12f]/n", D1, D3 );
  10. If (d1 <D3 | D3 <d1)
  11. Printf ("d1, D3 not equal !! /N ");
  12. Return 0;
  13. }

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 ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.