The iOS double type is unstable and the solution is as follows !, Iosdouble

Source: Internet
Author: User

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;
}

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.