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

后端 未结 4 636
小蘑菇
小蘑菇 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:34

    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.

提交回复
热议问题