Is the abs function that you often use (take absolute value) always return a non-negative number ?, Abs absolute value

Source: Internet
Author: User

Is the abs function that you often use (take absolute value) always return a non-negative number ?, Abs absolute value

A few days ago, I saw a question about the return value of the abs () function on Niuke. I didn't respond yet. The first reaction was: Since I started learning C language, we can see that it is used to calculate the absolute value of the int number. The returned value is of course 0 or positive. The answer is.

Later I thought about it and asked myself, is it that simple? So I checked the function library first and got:

#include <stdlib.h> //or math.h
int abs( int num );

I found that all the returned values of the library functions are written in the form of int. Why is it an int? After in-depth consideration, verification, and consultation, we finally come to the conclusion:

When num is 0 or positive, The function returns the num value;When num is a negative number and is not the smallest negative number(0x80000000). The function returns the absolute value of num, which is used to reverse the signed bits of the binary bits in the memory and add one to the next numeric bits;When num is the smallest negative numberBecause 32 bits of the int type in a positive number cannot represent the absolute value of this number, the negative number is still returned.

Paste the code running result: that is to say, for common programmers, it is not as safe as imagined to directly use abs to calculate the absolute value of a number. If a minimum negative number is passed to it as a parameter, this negative number is still returned, rather than its absolute value. Unexpected situations may occur in the future. Therefore, every programmer should understand: Checking the function return value after calling a function is a basic literacy of a good programmer. "I often ask: Will this variable or expression overflow or overflow ?"(The essence of programming-Tips for writing high-quality, error-free C programs by Microsoft, P80, and Steve Maguire) end by referencing a blog:

The ansi c standard specifies the minimum value range for each integer type (but does not specify the encoding scheme they must use), and requires all C language implementations to be implemented in limits. in the h header file, macros such as INT_MIN and INT_MAX are used to specify the actual value range of integer data, in addition, these actual value ranges must not be smaller than the standard minimum value range (that is, each implementation is required in limits. the absolute value of the macro defined in h is not smaller than the absolute value of the same-name macro defined in C standard, and the positive and negative numbers must be consistent ).

The standard INT_MIN is-(2 ^ 15-1) and INT_MAX is (2 ^ 15-1). In other words, the standard ensures that int data can represent at least all integers in a symmetric interval from-(2 ^ 15-1) to (2 ^ 15-1, therefore, programmers can safely use int variables to store any integer in the above range. But at the same time, please note that using int variables to express the number beyond the above range is a matter that is not protected by law (the standard is the programmer's Law, therefore, it should not be assumed that-32768 (I .e.-2 ^ 15) must be expressed in the int type, although it is indeed in the twos-complement notation Method) within the value range of the 16-bit integer. After familiarizing yourself with the above background, review the abs function and you will find that, in fact, this function can work normally for All integers in the standard minimum value range, the input data that caused the error is no longer in this range, so this error should not be caused by abs but by the function caller. It can be seen that for security and portability, it is a good programming habit to strictly limit the value to be expressed in the minimum value specified by the standard.

Reference: function abs --cplusplus.com C standard library function abs error [single answer] abs return value of math. h ()
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.