Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 412131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:03:18+00:00 2026-05-12T18:03:18+00:00

The negative implication of this is noted in the man page: NOTES Trying to

  • 0

The negative implication of this is noted in the man page:

NOTES
Trying to take the absolute value of the most negative integer is
not
defined.

What’s the reasoning behind this and what’s the best recourse for a person who would like to avoid undefined behavior? Do I have to resort to something like:

unsigned uabs(signed val) {
    return val > 0
        ? val
        : (val == 1U << ((sizeof(val) * 8) - 1))
            ? -1U
            : -val;
}

(Intentionally hacky to emphasize displeasure with stdlib 😉

Example

Say you had a 4-bit signed value (for ease of understanding). unsigned max is 15, signed (positive) max is 7, signed (negative) min is -8, so abs(-8) won’t fit into a signed value. Sure, you can represent it as -8, but then division and multiplication with the result don’t work as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:03:19+00:00Added an answer on May 12, 2026 at 6:03 pm

    The real answer to this question lies in the type promotion rules.

    If I apply an arithmetic operator to an unsigned int and an int, then the int argument is promoted to unsigned, and the result is also unsigned.

    If the abs() function returned unsigned, then it would cause this kind of type promotion of other values when it was used in an expression, which would cause unexpected results. For example, this code:

    if (abs(-1) * -1 < 0)
        printf("< 0\n");
    else
        printf(">= 0\n");
    

    Would print “>= 0”, which many wouldn’t like. The tradeoff, of not being able to use the single value INT_MIN, presumably seemed OK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

normally php will output negative integer with this format -1234 , how to it
Given a non-negative integer n and an arbitrary set of inequalities that are user-defined
Converting non-negative Integer to its list of digits is commonly done like this: import
When I pass latitude and longitude in negative value I am receiving this error.
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
From SUSv4 : If pid is negative, but not -1, sig shall be sent
What's the negative impact of not putting $redis = Redis.new in Unicorn's after_fork, since
I need to show negative value in Infragistics BarChart on X Axis. If I
How are negative number represented in 32-bit signed integer? Is it two's or one's
I have a negative integer (4 bytes) of which I would like to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.