Floating point points considerations

Source: Internet
Author: User
Two floating-point numbers cannot be accurately compared with "= =" floating-point numbers can only be close to not equal

if (0.1+0.2==0.3) {
System.out.println (TRUE);
}

Rounding is never done when floating-point numbers are generated the overflow data loss results in inaccurate data and cannot be directly performed on = = Operations

Workaround: Use integers, not floating-point numbers

2. Problem with floating points in operation

1.   1/a  will appear floating-point   number two integer division     value Loss problem

for (int a=20; a>=1;a–) {
for (int b=a-1; b>=1;b–) {
for (int c=b-1; c>=1;c–) {
for (int d=c-1; c>=1;d–) {
if (1/a+1/b+1/c+1/d==1) {
System.out.println (A +, "+b+", "+c+", "+d");
}

                    }
               }
          }
      }

Error

1.      integer division values are lost by
2.     Floating point numbers cannot be accurately compared,

Solution:

1.           Formula conversion   b*c*d+a*c*d +a*b*d+a*b*c==a*b*c*d
2.           Subtract compared to one of the precision values     

                          eg:   a-b<c         A, b comparison

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.