Java. lang. OutOfMemoryError and Solution

Source: Internet
Author: User

Some colleagues encountered a strange problem at work, which is related to floating point calculation. The Code is as follows:

1 # include

2 using namespace std;

3

4 int main ()

5 {

6 double s = 6.0;

7 double e = 0.2;

8

9 cout <static_cast (S/e) <endl;

10 return 0;

11}

This code looks very simple. If you want to calculate it, you should output 30 pairs.

But the result is that we get different results on 32 and 64-bit linux platforms, respectively 29 and 30, unexpected, right?

Then, if you change the code to the following:

1 # include

2 using namespace std;

3

4 int main ()

5 {

6 double s = 6.0;

7 double e = 0.2;

8

9 double d = s/e;

10

11 cout <static_cast (D) <endl;

12 return 0;

13}

You will find the same "correct" result on both platforms! Why?

Sparse floating point number

As we all know, computers cannot accurately represent all floating-point numbers. The density of irrational numbers makes it possible for us to express floating-point numbers regardless of their high precision data types, the range can be quite sparse compared to the entire irrational number.

Therefore, in the computer world, we can only express a certain range of data with limited precision as much as possible, as for those numbers that cannot be exactly expressed, you can only find the nearest number in the range that a computer can represent to join and join.

This seems to be easy to understand. For example, root number 2 or something, we all know that these irrational numbers cannot be fully expressed in the computer, but there are still some rational numbers, although it can be accurately expressed in decimal system, it cannot be exactly expressed in binary system. For example, if you have doubts about 0.2 in the above example, let's take a look at how to convert decimal points into binary values, and then calculate them on paper.

To talk about this, I just want to explain that the floating point numbers in the computer world are quite loose. I borrowed a picture from the book "Understanding the operating system in depth" to help everyone

Break and conversion of Floating Point Numbers

Because many decimal places cannot be accurately expressed, we can only try to find the nearest number in the decimal places with limited precision to approximate those infinite decimal places.

So how do computers make these approximation? There are four common methods:

The first one is the default usage. Note that these break methods are not limited to converting from floating point to integer, but also between floating point.

In C, there are several principles for converting floating point numbers and integers:

1) if the int type is converted to float, it will not overfloat, but some numbers cannot be expressed using float. Therefore, you may need to rounding. Remember that float is sparse.

2) When you convert data from int or float to double, the precision will not be lost. After all, the double precision is too high.

3) when the double type is converted to float, it is likely to overfloat. The conversion is performed in the round-to-even mode (default.

4) use the round-to-zero method to convert from float and double to int, which may also be truncated.

Note the 3rd and 4th principles. The different principles used during conversion sometimes lead to some subtle results.

Intel IA32 floating point operation

Like many other processors, the IA32 processor has registers dedicated to storing floating point numbers. When floating point numbers are computed in the cpu, these registers are used to save the input and output and related intermediate results.

However, IA32 has a special point. Its floating point register is 80 bits, and we only use 32 and 64 bits in the program. Therefore, when float, when double is placed into the cpu, they are all converted into 80 bits, then the operation is performed in the form of 80 bits, and the final result is converted back. This feature allows the calculation of floating point numbers to be more accurate, but it may also lead to unexpected problems.

You may suddenly realize that, right, the strange question we mentioned at the beginning is related to this.

The result of s/e is an 80-bit floating point number, which is first converted to double and then converted to int. The result may be different from the direct conversion to int.

For example, in our example, s/e ~ 29. 999999..., when s/e is converted to double and round-to-even is used, the result may be 30.0000001. If it is converted to an integer, the result will be 30.

However, if it is directly converted from 29. 99999... to an integer, the result is 29.

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.