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

后端 未结 4 627
小蘑菇
小蘑菇 2020-12-11 00:58

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 encou

4条回答
  •  执笔经年
    2020-12-11 01:31

    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

提交回复
热议问题