Java Floating point number (note)

Source: Internet
Author: User

1. In the computer, the floating-point number is different from the decimal number.

 Public Static void Main (string[] args) {        double B1 = 0.1;         double b2 = 0.2;         double result = B1 + B2;        System.out.print (result);    }

Results: 0.30000000000000004

Cause: The computer uses the binary to store the data.

PS. Use floating-point numbers to make conditional judgments. For example (while (f! = 0.3) can always loop).

  

2. Large numbers plus decimals in floating-point numbers

 Public Static void Main (string[] args) {        float f1 = 30000000;         float F2 = 1;         float r = f1 + F2;         if (R < F1)            System.out.println ("left smaller");         Else             System.out.println ("What?") );    }

Result: What?

Cause: The larger the floating-point number, the greater the gap between the two adjacent floating-point numbers.

3. Floating-point numbers to other types

 Public Static void Main (string[] args) {        System.out.println ((int) double.nan);        System.out.println ((int) double.positive_infinity);        System.out.println ((int) double.negative_infinity);    }

Results: 0

2147483647
-2147483648

     

  

Java Floating point number (note)

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.