Float and double in C #

Source: Internet
Author: User

During my work, I found a problem with the accuracy of the C # floating point number. The following program running results did not get my expected results:

View source Print? 01 namespace FloatTest 02 { 03      class Program 04      { 05          static void Main( string [] args) 06          { 07              double a = 0.0001; 08              float b = 0.1F; 09   10              int c = ( int )((a * 1000) / b); 11   12              Console.WriteLine( "c = {0}" , c); 13   14              Console.ReadLine(); 15          } 16      } 17 }

The expected result is 1, and the result returned by the result program is c = 0.

 

This reminds me of the possibility that the floating point uses the IEEE754 Representation Method and B is converted to a double in the operation, which may be caused by a problem in the conversion algorithm. To prove this problem, I did the following experiment:

View source
Related 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.