The iOS double type is unstable and the solution is as follows !, Iosdouble
The current app has appeared. We will share some of the above-mentioned image bugs and try to solve them and pay attention to them!
Code with the problem:
Code to be improved later:
When the double type is compared with the zero value, you cannot use = OR =, but >=and <= yes?
You can use> = and <= for comparison.
The floating point number (double) cannot use = and! = Because floating point numbers are stored in the memory based on precision, the 3.12 you see may have been the 2-digit precision after the decimal point of 3.123456, therefore, when compared with another 3.12, there may be many cases. If the other 3.12 is actually the 2-digit precision after the decimal point of 3.121111, then the two numbers are equal, in fact, it is not equal.
Therefore, when comparing floating point numbers, you usually subtract them and then compare them with a precision.
For example:
A = 3.12; B = 3.13;
When a and B are compared, they should:
# Define N 0.0001
If (a-B <N)
It means that a is less than B ;.
Instead of directly comparing if (a <B)
Float Type and double type Accuracy
If it is 0, it will be omitted. This is a feature of C ++.
To retain decimal places, this can be done (the following uses retaining three decimal places as an example)
# Include <iostream>
# Include <iomanip>
Using namespace std;
Int main ()
{
Float a = 100, B = 2.34422;
Cout <setiosflags (ios: fixed) <setprecision (3); // method 1
Cout <a <endl;
Cout <B <endl;
Printf ("%. 3f \ n", a); // method 2
Return 0;
}