The old Google Groups will be going away soon, but your browser is incompatible with the new version.
taking abs() of min signed int value
 There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic. There was an error processing your request. Please try again. Standard view   View as tree
 4 messages

From:
To:
Cc:
Followup To:
Subject:
 Validation: For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon.

More options May 16 2012, 1:39 pm
Newsgroups: comp.lang.c++.moderated
From: Jeremy Todd <jer...@izotope.com>
Date: Wed, 16 May 2012 10:39:06 -0700 (PDT)
Local: Wed, May 16 2012 1:39 pm
Subject: taking abs() of min signed int value
Hi,

I need to compute the absolute value of the minimum signed int value
for each signed integral type. This value isn't representable as a
signed int, but it is representable as an unsigned int.

This is easy when a larger signed type is available:

int32_t min_int32 = std::numeric_limits<int32_t>::min();
int64_t abs_min_int32= -int64_t(min_int32);

I couldn't come up with an elegant solution when attempting this for
the 64-bit type. With a two's complement representation, it just so
happens that reinterpreting the min signed int as an unsigned value
produces the correct result:

int64_t min_int64 = std::numeric_limits<int64_t>::min();

but this felt rather inelegant. Can anyone think of a more elegant
solution that does not assume a two's complement binary
representation?

I should note that the standard does not seem to require that
abs(min_int) is representable as a uint for a given size, so let's
limit our consideration to platforms where it is representable.

Regards,
Jeremy

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

To post a message you must first join this group.
You do not have the permission required to post.
More options May 16 2012, 8:24 pm
Newsgroups: comp.lang.c++.moderated
From: Marc <marc.gli...@gmail.com>
Date: Wed, 16 May 2012 17:24:53 -0700 (PDT)
Local: Wed, May 16 2012 8:24 pm
Subject: Re: taking abs() of min signed int value

Casting a signed type to an unsigned one has a well-defined modular
meaning. When you cast min_int64 to uint64_t, you get something that
is the same as min_int64 modulo 2^64 (hence 2^64+min_int64). You can
then apply operator- to this and get something equal to -min_int64
modulo 2^64. And that's the number you are looking for. In short:

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

To post a message you must first join this group.
You do not have the permission required to post.
More options May 16 2012, 8:28 pm
Newsgroups: comp.lang.c++.moderated
Followup-To: comp.lang.c++.moderated
From: Ulrich Eckhardt <dooms...@knuut.de>
Date: Wed, 16 May 2012 17:28:22 -0700 (PDT)
Local: Wed, May 16 2012 8:28 pm
Subject: Re: taking abs() of min signed int value

Jeremy Todd wrote:
> I need to compute the absolute value of the minimum signed int value
> for each signed integral type. This value isn't representable as a
> signed int, but it is representable as an unsigned int.

> This is easy when a larger signed type is available:

> int32_t min_int32 = std::numeric_limits<int32_t>::min();
> int64_t abs_min_int32= -int64_t(min_int32);

> I couldn't come up with an elegant solution when attempting this for
> the 64-bit type.

You could use the fact that "min == min/2 + (min - min/2)" and that the
absolute of the two summands can be represented as the according *int_t.

Uli

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]

To post a message you must first join this group.
You do not have the permission required to post.
More options May 17 2012, 9:45 am
Newsgroups: comp.lang.c++.moderated
From: Kaba <k...@nowhere.com>
Date: Thu, 17 May 2012 06:45:44 -0700 (PDT)
Local: Thurs, May 17 2012 9:45 am
Subject: Re: taking abs() of min signed int value

Jeremy Todd wrote:
> I need to compute the absolute value of the minimum signed int value
> for each signed integral type. This value isn't representable as a
> signed int, but it is representable as an unsigned int.

A way out of these problems is to never use the smallest value of a
signed integer. For example, if you store a sound signal in a 16bit
signed integer, use the range [-(2^16 - 1), 2^16 - 1]. The non-existence
of the additive inverse of -2^n makes the value unusable. From this
restriction follows other advantages such as being able to form a
bijection between the integer values and floating point numbers (useful
for lossless sound conversion between integers and floating point
numbers). Similarly for any data which is quantized from real numbers.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated.    First time posters: Do this! ]