<> Comparison of zero values -- if statements for bool, Int, float, and comparison between pointer variables and zero values

Source: Internet
Author: User
Tags integer numbers

The following is a question I saw in a C language book.

Write the statement for comparing the float type variables I with the zero value:

 if((x >= -0.00001)&& (x <= 0.00001) )

You cannot use "=" or "! = "To compare it with a number, try to convert it into a format such as"> = "or" <=.

This is a common question for a programmer interview and a basic C ++ question. If you have read only a few basic programming introductory books in college, it may be strange to see this question. Isn't it just a comparison with 0? Just compare it with 0. In fact, it's not true.
First, let's give a note: The question requires zero-Value Comparison, instead of comparing it with 0. In C ++, the range of "zero-value" is large, which can be 0, 0.0, false, or "Null Pointer ". The IF statement for the comparison between int-Type Variable N and "zero value" is:

If (n = 0)

If (n! = 0)

The following statement is a poor style ..

If (n) // you may misunderstand that N is a Boolean variable.

If (! N)

Write the if statement for comparing bool flag with zero value.

The semantics of the root data boolean type. The zero value is "false" (marked as false), and any non-zero value is "true" (marked as true ). There is no uniform standard for the true value. For example, Visual C ++ defines true as 1, while Visual Basic defines true as-1. Therefore, we cannot directly compare a Boolean variable with true, false, or 1 or 0.

Standard answer:

If (FLAG) or if (! Flag)

The following statement is a poor style.

If (flag = true)

If (flag = 1)

If (flag = false)

If (flag = 0)

Write the if statement for comparing float X with "zero value.

Be sure to note that both float and double variables have precision restrictions. You cannot use "=" or "! = "To any number, you should try to convert it into the form of"> = "or" <=. (Why? The article will be discussed in detail later. For details, refer)

Assuming that the floating point variable is named X, the comparison of IF (x = 0.0) // implicit errors should be converted to If (x> =-epsinon) & (x <= epsinon) where epsinon is the allowable error (accuracy ).

Standard answer example:

Const float epsinon = 0.00001;

If (x> =-epsinon) & (x <= epsinon)

The following is an incorrect statement.

If (x = 0.0)

If (X! = 0.0)

Write the if statement for comparing char * P with "zero value.

Standard answer:

If (P = NULL)

If (P! = NULL)

The following statement is a poor style.

If (P = 0) // It is easy to misunderstand that P is an integer variable.

If (P! = 0)

If (p) // It is easy to misunderstand that p is a bool variable.

If (! P)

Many of the above bad styles can be compiled, but the statements cannot express the logical basis for comparison with zero values. Generally, if you want to use the if statement to determine whether a variable is true or false, you should directly use if (VAR), if (! VaR) indicates that the function of the IF statement is "logical". If you want to use the if statement to determine a numeric variable (short, Int, long, etc ), if (Var = 0) should be used to indicate that this If statement compares the variable with 0 on the "value; if (Var = NULL) is suitable for checking whether the pointer is null. This is a good programming habit.

Float variables are not accurate, so do not use "=" or "! = "To the number, you should try to convert it into the form of"> = "or" <=. If it is written as if (x = 0.0), an error is returned. The score is 0. The detailed reasons are as follows:
The storage mechanism of floating point numbers in memory is different from that of integer numbers, and there is a rounding error. It is used in computers to represent any real number. 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.
As a result, a floating point number is usually accompanied by an approximation or rounding because it cannot be accurately expressed. But the benefit of this design is that it can store a larger range of numbers on a fixed length.
For example, a four-digit floating point number with an exponential range of ± 4 can be used to represent 43210, 4.321, or 0.0004321, but there is not enough accuracy to represent 432.123 and 43212.3 (must be approximately 432.1 and 43210 ). Of course, the actual number of digits is generally larger than 4.
So floating point numbers cannot be determined to be equal. If (x = 0) encoding is not always correct. We recommend that you use a range to determine if the floating point number is equal, if X is within a certain range, we think it is equal. As for how to define the range, it depends on the actual situation. Float and double are different.
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.

Let's look at the answer on csdn.

Reference
# Define e 1.0e-6

If
-----------------------

Floating point numbers cannot be compared directly. Because they are not precisely stored, only one precision can be set, and the values within the allowable error are considered equal; it is incorrect to use = when comparing floating point data.

# Define e 0.000001

FABS (a + B) + C)-(B + a) + C ))

<> Comparison of zero values -- if statements for bool, Int, float, and comparison between pointer variables and zero values

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.