Thought of by the absolute value function C ++

Source: Internet
Author: User

An absolute value function is used in these days. Okay, you will say that it's okay to directly use std: abs? Yes. I used it in the first place. However, if the absolute value of the smallest int value is obtained, the output is incorrect. For example:
[Cpp]
Int min_int =-2147483648;
Cout <std: abs (min_int) <endl;
The result is still-2147483648! This may happen to my program. Therefore, you need to complete one by yourself.

1. First version:
[Cpp]
Unsigned int _ abs (int value)
{
Return (value> = 0 )? Value:-value;
}
It is still the old path of std: abs, not desirable!

2. Version 2:
[Cpp]
# Include <limits>
 
Unsigned int _ abs (int value)
{
Return (std: numeric_limits <int>: min () = value | value> = 0 )? Value:-value;
}
This version is concise and highly portable. Std: numeric_limits <int >:: min () returns the minimum value of the int value in the current system, and can return accurate values according to the memory width of the int. When the value is equal to the smallest int value or the value is not negative, we directly copy the bitwise position. Because the unsigned int does not have a signed bit, it is completely feasible. When the value is a negative value other than the minimum value, you can directly take the opposite number.
However, this version requires two conditions to be determined. Can it be optimized? So the following occurs:

3. Version 3:
[Cpp]
Unsigned int _ abs (int value)
{
Unsigned int copyed_value = value;
Return (copyed_value> 0x80000000 )? -Value: copyed_value;
}
Because the smallest int value in 32 bits is 0x80000000 -- the highest sign bit is 1. This value is still used when the bitwise position is copied to the unsigned int. However, apart from the highest bit of 1, other negative numbers also have values in other locations. For example, the hexadecimal value of-1 is 0x80000001. Therefore, we first copy the bitwise to copyed_value. So there is a judgment:
(1) If copyed_value is greater than 0x80000000, it indicates that value is a negative number, so we take the opposite number (-value) directly );
(2) If copyed_value is equal to 0x80000000, it indicates that the value is exactly the smallest negative number. After the bitwise copy operation, copyed_value stores the absolute value of value, so copyed_value is returned;
(3) If copyed_value is less than 0x80000000, the value is a positive number. Directly go to alue or copyed_value.
Through analysis, we combine (2) and (3) to return copyed_value. Therefore, there is only one comparison operation to adopt the third solution. Save once than the second solution. However, this solution may not be good at first glance.

Weigh the three schemes, and we should say that each has its own advantages and disadvantages. Each of the three cases has its own applicability. If your function does not consider the absolute value of the minimum int value, you can use std: abs. If you need to consider but do not have to worry about performance problems, the second solution is your best choice! However, if you want to consider the absolute value of the minimum int value and performance, we recommend that you use the third solution.

Although this problem is simple, there are a lot of things transmitted in it. In fact, in programming, we often get a lot from the details.



From Zhang's column

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.