A detailed explanation of data comparison and conversion based on C + + floating-point (float, double) _c language

Source: Internet
Author: User
Tags sprintf
The storage mechanism of floating-point numbers in memory differs from the integer number, which has rounding errors, and is used to approximate any real numbers in the computer. In particular, the real number is multiplied by an integer or a fixed-point number (i.e. the mantissa) by multiplying a cardinality (usually 2 in the computer) with an integer power, which is similar to the scientific notation of cardinality 10.
Therefore, floating-point numbers are usually associated with approximation or rounding because they cannot be accurately represented in an operation. But the advantage of this design is that you can store a larger range of numbers on a fixed length.
1, the conversion of the string to float, double the process of loss of precision, but float, double the loss of the accuracy of their respective
std::string str= "8.2";
Float Cc=atof (Str.c_str ()); The value of CC is 8.1999998
std::string str= "8.2";
Double Cc=atof (Str.c_str ()); The value of CC is 8.1999999999999993
2, there may be loss of precision in the process of converting float and double to characters, but through%.8LF can avoid
(1)The number of valid digits added before and after the float is only 6 digits. When a given float is converted to a character within 6 bits, the precision is not lost and the precision is lost when the number of significant digits is greater than 6 digits
Precision not lost
Char buf[100]={' i '};
float aa=8000.25;
sprintf (buf, "%f", AA); 8000.250000
Precision not lost
Char buf[100]={' i '};
float aa=8.00025;
sprintf (buf, "%f", AA); BUF = 8.000250
Loss of precision, error
Char buf[100]={' i '};
float aa=8000.251;
sprintf (buf, "%f", AA); 8000.250977
The accuracy is lost, the error is used at this time. 8LF is also invalid
Char buf[100]={' i '};
float aa=8000.251;
sprintf (buf, "%.8lf", AA); 8000.25097656
(2)The number of valid digits added before and after a double decimal is only 16 digits, and when the given double number is converted to a string within 16 digits, the precision is not lost, and when the number of significant digits is greater than 16, there is a loss of precision in the Lake shadow apartment
Existence error
Char buf[100]={' i '};
Double aa=121.437565871234012;
sprintf (buf, "%.20LF", AA); 121.43756587123401000000
No error
Char buf[100]={' i '};
Double aa=8000.256165;
sprintf (buf, "%.8lf", AA);
Std::cout <<buf<<std::endl; 8000.25616500
3. Comparison of floating point numbers
using "= =" to compare two double types that should be equal, the return value is completely indeterminate. The principle of computer calculation of floating point number is to ensure that only the necessary accuracy is correct.
When we judge the equality of floating-point numbers, we recommend the scope to determine, if x in a certain range, we think that equality, as to the scope of how to define, depends on the actual situation, float, and double have different
So const float Epsinon = 0.00001;
if ((x >=-Epsinon) && (x <= Epsinon) It is advisable to judge this
As for why to take 0.00001, you can define yourself according to the actual situation
According to the above analysis proposed in the system development process design to character conversion recommended double type, precision set to%.8LF, in the comparison floating-point dozens of suggested 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.