Does the ABS function that you use often (take absolute value) really always return a non-negative number?

Source: Internet
Author: User

A few days ago in the cattle network to see an ABS () function return value of the topic, see, then did not react, the first reaction is: since I began to learn C, it is used to find the absolute value of int, the return value of course is 0 or positive ah, a look at the answer is a.

Later reasoning, questioned oneself is this problem so simple? So decisive first check function library, get:

#include <stdlib.h>//or math.hintint num);

The return value form of the Discovery library function is written in int, why int? After in-depth thinking, verification and access to information, the final conclusion:

When
num is 0 or positive , the function returns the NUM value , and when NUM is negative and is not the smallest negative number (that is, 0x80000000), the function returns the corresponding absolute value of Num. The sign of the bits in memory is reversed, and the trailing value is reversed plus one; when num is the smallest negative number, the negative value is returned because the INT type 32 bits in the positive numbers cannot represent the absolute values of the numbers.
paste the code to run the result:      that is, for ordinary programmers, the direct use of ABS to find the absolute value of a number is not as safe as expected, if it is passed a minimum negative value as a parameter, the resulting is still this negative number, rather than its absolute values, perhaps the following will occur in a variety of unexpected situations. As a result, every programmer should understand that checking function return values after calling a function is a basic accomplishment that a good programmer should have. Frequently asked: Does this variable or expression overflow or overflow? ("Programming essence-microsoft Writing high quality error-free C program secret" P80,steve Maguire) quote a paragraph from a blog as the end of the word:
ansi C standard specifies the minimum domain value for each type of integer ( But there is no requirement that they have to take which encoding scheme to use, and require all C implementations to be in the Limits.h header file through such int_min, Int_ Max this Small (which requires that each implementation of the macro defined in Limits.h is not less than the absolute value of the macro with the same name as specified by the C standard, and the sign
The standard definition of int_min is-(2^15-1), Int_max is (2^15-1), in other words, the standard guarantees that INT data can at least represent-(2^15-1) to (2^15-1) all integers within a symmetric interval , so programmers can confidently use an int variable to store any integer in the range above. At the same time, however, it is important to note that using an int variable to express a number beyond the said range will be a matter of no law (standard is the law of the programmer), so it should not be assumed that -32768 ( i.e. -2^15) must be expressed in int, Although it is indeed located within the range of 16-bit integers encoded with two complement notation (twos-complement notation). Familiar with the above background and then review the problem of ABS function will be found, in fact, the function for the standard specification of the minimum range of all the integers in the normal operation, and the above mentioned error-causing input data is not within this range, Therefore, this error should not be the responsibility of the ABS but the function caller. Thus, for security and portability, It is a good programming practice to strictly limit the value of the table-to-reach representation to the minimum range specified in the standard.
  Reference:function abs--cplusplus.comC standard library function An error of ABS [Single choice]MATH.H ABS return value ()   

Does the ABS function that you use often (take absolute value) really always return a non-negative number?

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.