A floating point number calculation problem

Source: Internet
Author: User

Some colleagues encountered a strange problem at work, which is related to floating point computing. The Code is as follows: 1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 double s = 6.0; 7 double e = 0.2; 8 9 cout <static_cast <int> (s/e) <endl; 10 return 0; 11} This code looks very simple. I think it should be 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 1 # include <iostream> 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 <int> (d) <endl; 12 return 0; 13} You will find the same "correct" result on both platforms! Why? Sparse floating-point numbers are well known, and computers cannot accurately represent all floating-point numbers. The density of irrational numbers makes it possible to represent floating-point numbers regardless of how high the data type is, 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, let's break and convert floating point numbers. Because many decimals 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 is the default method. Note that these broken methods are not limited to converting from floating point to integer, but also between floating point numbers. In the C language, there are several principles for converting floating-point numbers and integers: 1) converting the int type to float will not overfloat, but some numbers cannot be expressed using float, so 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. The Intel IA32 floating-point computation IA32 processor, like many other processors, has registers dedicated to storing floating-point numbers. When floating-point computation is performed on 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 converting s/e to double using the round-to-even method, the result may be 30.0000001, and then returns 30 if it is converted to an integer. however, if. 99999... convert to an integer, but the result is 29.

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.