C # Floating Point calculation problems,

Source: Internet
Author: User

C # Floating Point calculation problems,

Let's take a look at our computing capabilities.

What is the value of 0.1 + 0.1 + 0.1-0.3?

You may ask this simple question: do you look down on me? It must be equal to 0.

If you calculate it directly, what if you use a computer?

It's time to witness the miracle. Read the Code:

Void Main () {var f = 0.1 + 0.1 + 0.1-0.3; Console. WriteLine ("f =={ 0}", f );}

Running result:

This is because of the accuracy of the computer, the lack of internal storage and computing accuracy in the computer, and other issues, I may not understand, however, you can use the following solution to solve:

Void Main () {// var f = 0.1 + 0.1 + 0.1-0.3; // Console. writeLine ("f =={ 0}", f); var f1 = new Decimal (0.1) + new Decimal (0.1) + new Decimal (0.1) -new Decimal (0.3); Console. writeLine ("f1 =={ 0}", f1 );}

Running result:

This is the normal operation result.

La la !!!!

 

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.