puzzle 01: Odd Sex
The purpose of the following method is to determine whether its unique argument is an odd number. Is this method feasible?
Public static bool IsOdd (int i)
{
return I% 2 = = 1;
}
FAQ 01: Odd Sex
An odd number can be defined as an integer divisible by 2 to a remainder of 1. The expression i%2 calculates the remainder of I divided by 2, so it seems that the program should work. Unfortunately, it does not work; in One-fourth of the time it returns the wrong answer.
Why is it One-fourth? Because half of all int values are negative, and the IsOdd method will fail for all negative odd numbers. Calling the method on any negative integer returns false, whether the integer is even or odd.
This is the result of C # 's definition of the remainder operator (%). The operator is defined as a value of all int values A and all non-0 int values B, all of which satisfy the following identities:
(A/b) * + (a% b) = = a
In other words, if A is divisible by B, the quotient is multiplied by B, then the remainder is added, then the initial value a is obtained.[C # Language Specification 7.7.3]. The identity has the correct meaning, but when the truncated integer with C # is divisible by the operator[C # Language Specification 7.7.2]when combined, it means:when the take remainder operation returns a nonzero result, it has the same positive and negative sign as the left operand.
The isodd method and the definition of the term "odd" on which it is based assume that all the remainder are positive. Although this hypothesis is meaningful for some kinds of division, the remainder of C # is exactly the same as the integer division operation that discards the fractional portion of the divisible result.
when I is a negative odd number, I% 2 is equal to 1 instead of 1, so the IsOdd method incorrectly returns false. To prevent such accidents,please test your method when passing negative, 0, and positive values for each numeric parameter, the behavior is correct.
the problem is easy to correct. Just compare the I% 2 with 0 instead of 1 and use the opposite comparison meaning:
Public static bool IsOdd (int i)
{
return i% 2! = 0;
}
If you are using the IsOdd method in an environment that emphasizes performance, it would be better to substitute the bitwise operator and (&) for the remainder operator:
Public static bool IsOdd (int i)
{
return (I & 1)! = 0;
}
The second version may run much faster than the first version, depending on what platform and virtual machine you are using, and is unlikely to run slower. As a general rule, the divide and take operations are slower than other arithmetic and logical operations. haste to optimize is not good,in these cases, however, the faster version is as clear as the original version, so there is no reason to favor the original version.
In short, whenever the take-up operator is used, both the operand and the result symbol are considered. The behavior of the operator is straightforward when its operand is non-negative, but its behavior is less obvious when one or two operands is a negative number.
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
C # FAQ 01: Odd