Detailed description of data comparison and Conversion Based on C ++ floating point numbers (float and double)

Source: Internet
Author: User
Tags integer numbers

The storage mechanism of floating point numbers in the memory is different from that of integer numbers. It has a rounding error and is used to represent any real number in the computer. Specifically, this real number is obtained by multiplying an integer or a fixed number (that is, the ending number) by the integer power of a certain base number (usually 2 in the computer, this representation is similar to the scientific notation with a base of 10.
Therefore, floating point numbers are usually accompanied by approximation or rounding because they cannot be accurately expressed. But the benefit of this design is that it can store a larger range of numbers on a fixed length.
1. There is a loss of precision in the process of converting a string to float or double, but the loss of precision of float and double are different.
Std: string str = "8.2 ";
Float cc = atof (str. c_str (); // The cc value is 8.1999998
Std: string str = "8.2 ";
Double cc = atof (str. c_str (); // The cc value is 8.1999999999999993
2. There may be loss of precision when converting float and double into characters, but it can be avoided through %. 8lf.
(1) float adds up to 6 Valid digits before and after the decimal point. If the given float valid number is converted to a character within six digits, the precision will not be lost. If the valid number is greater than six digits, the precision will be lost.
// Precision is not lost
Char buf [100] = {'\ 0 '};
Float aa = 8000.25;
Sprintf (buf, "% f", aa); // 8000.250000
// Precision is not lost
Char buf [100] = {'\ 0 '};
Float aa = 8.00025;
Sprintf (buf, "% f", aa); buf = 8.000250
// The error message returned when the precision is lost.
Char buf [100] = {'\ 0 '};
Float aa = 8000.251;
Sprintf (buf, "% f", aa); // 8000.250977
// The precision is lost. If an error exists, the use of. 8lf is also invalid.
Char buf [100] = {'\ 0 '};
Float aa = 8000.251;
Sprintf (buf, "%. 8lf", aa); // 8000.25097656
(2) The valid digits before and after the double decimal point are only 16 digits. If the given double valid number is converted to a string within 16 digits, the precision is not lost, when the number of valid digits is greater than 16, the accuracy of the lake image apartment is lost.
Error
Char buf [100] = {'\ 0 '};
Double aa = 121.437565871234012;
Sprintf (buf, "%. 20lf", aa); // 121.43756587123401000000
// No error
Char buf [100] = {'\ 0 '};
Double aa = 8000.256165;
Sprintf (buf, "%. 8lf", aa );
Std: cout <buf <std: endl; // 8000.25616500
3. Floating Point comparison
We use "=" to compare the two double types that should be equal, and return the true value is completely uncertain. The principle of the computer's floating point calculation is to ensure that the accuracy is correct.
We recommend that you use the range to determine when the floating point number is equal. If x is within a certain range, we think it is equal. How to define the range depends on the actual situation, float, different from double
So const float EPSINON = 0.00001;
If (x> =-EPSINON) & (x <= EPSINON) is advisable
As for why 0.00001 is used, you can define it as needed.
According to the above analysis, it is recommended to design the double type for character conversion during the system development process, and set the precision to %. 8lf. We recommend that you compare the floating point number 10 with EPSINON = 0.00000001.
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.