Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Short Error: Negating the minimum value of a twos complement number is invalid

Tags:

c#

short

I have been encountering this error for my project, which involves working with Digital Audio Signals.

So I have been getting the amplitude values and recently encountered this error. This occurs when the amplitude value encountered is "-32768" upon debugging. I am storing the values in a short[] array.

I have a hunch that it has something to do with max/minimum values (I use Math.Abs) but I am unsure on how to handle it.

Can someone help? Thanks!

like image 266
user488792 Avatar asked Jun 07 '11 12:06

user488792


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


4 Answers

16 bit signed int (short) takes values between -32,768 and 32,767.

Negating -32768, or getting the absolute value, is impossible to do inside a 16 bit signed integer. The value (32,768) is greater than the maximum possible positive value (32,767).

I would not like to advise you how to solve the problem without knowing more details of the algorithms you are using.

like image 62
David Heffernan Avatar answered Oct 20 '22 18:10

David Heffernan


The absolute value of -32768 is +32768... but that's outside the range of short... hence the error. (You're lucky you're seeing this as an exception... other ways of encountering this oddity can give silent overflow, leading to some very odd results)

Options:

  • Special-case this value, e.g. convert to -32767 first, if the exact value doesn't matter too much
  • Convert it to an int before calling Math.Abs
like image 29
Jon Skeet Avatar answered Oct 20 '22 19:10

Jon Skeet


What value would you have it be? there is no 32768 in short - only 32767.

You could write your own method, of course:

public static short LossyAbs(short value)
{
    if(value >= 0) return value;
    if(value == short.MinValue) return short.MaxValue;
    return -value;
}

but this is lossy in that it sort-of loses a value. Perhaps a better idea is: don't use short.MinValue if you intend to (potentially) negate it. Limiting yourself to -32767 would make this go away.

like image 31
Marc Gravell Avatar answered Oct 20 '22 18:10

Marc Gravell


Convert short[] array to int[] array.

like image 42
Anil Avatar answered Oct 20 '22 19:10

Anil