Java.lang.OutOfMemoryError and Solutions

Source: Internet
Author: User
Tags decimal to binary square root

Some colleagues have a strange problem with their work, and the problem is related to floating-point counting, and 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;

Ten return 0;

11}

This code looks very simple, in mental arithmetic, should output 30.

But the result is that we got different results on the 32 and 64 Linux platforms, 29 and 30, respectively, right?

Then, if you change the code to read:

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

One cout << static_cast (d) << Endl;

return 0;

13}

You will find the same "correct" results on two platforms! Why?

Sparse floating-point numbers

As we all know, the computer is unable to accurately represent all floating-point numbers, the density of irrational numbers so that no matter how high the precision of the data type to represent floating-point numbers, the range can be expressed relative to the entire irrational number is quite sparse.

So in the computer world, we can only use finite precision to express a certain range of data, as for those who can not accurately represent the number, only in the scope of the computer to be able to find a and its closest to the number to gather and gather.

This seems to be a better understanding, such as the square root 2 or something, we all know that these unreasonable numbers can not be completely accurate in the computer, but there are some rational numbers, although in the 10 into the system can be accurately expressed in the 2 system is not accurate, For example, 0.2 of the above example, if you have doubts about this, you can review how to turn the decimal to binary, and then slowly use the pen on the paper to calculate.

Speaking of these, but still want to show that the computer world floating point is quite lax, borrow the "in-depth understanding of the operating system," a picture in the book, so that everyone

The break and conversion of floating point numbers

Because many decimal places cannot be expressed accurately, we can only find the nearest nearest number in a finite precision decimal to approximate those infinite decimals.

So how does a computer do these approximations? Commonly used in the following 4 ways:

The first of these is the default mode of use, it should be noted that these breaks are not limited to floating point numbers to integers, floating point is also applicable between the numbers.

In C, the conversion of floating-point numbers to integers has the following principles:

1 int to float, not overfloat, but some of the number float can not be expressed, so you may need to rounding, remember that float is sparse.

2 when converted from int or float to double, precision is not lost, double precision is too high.

3) Double to float, it is likely to overfloat, the conversion is Round-to-even (default).

4 The conversion from float to int is round-to-zero and, of course, it is likely to be truncated.

Note that rule 3rd, rule 4th, the different principles they use when converting can sometimes lead to subtle results.

Intel IA32 floating-point operations

The IA32 processor, like many other processors, has registers designed to hold floating-point numbers, which are used to hold input and output and related intermediate results when floating-point operations are performed on the CPU.

But IA32 has a very special place, its floating-point number registers are 80 bits, and we use only 32 and 64 bits in the program, so when you put float,double into the CPU, they are converted to 80 bits, and then they work in 80 digits, The final results are converted back. This allows the calculation of floating-point numbers to be relatively more accurate, but at the same time, inadvertently may also lead to some unexpected problems.

You may suddenly have a sudden epiphany, and right, the first thing we mentioned about that strange question is related to it.

S/E Get the result is a 80-bit floating-point number, from which the floating-point number is converted into a double and then into an int, and directly converted to int, the result is likely to be different.

For example, in our example, s/e ~ 29.999999 ... when the s/e is converted into a double using the Round-to-even method, it is possible to get 30.0000001, then turn it into an orthopedic, get 30.

But if directly by the 29.99999 ... Convert to integral type, but get 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.