Search code examples
c#mathnandigital-signature

Why does Math.Sign() throws exception when passed a NaN?


A Mathematical functions can either return NaN when a NaN parameter is passed to it or throw an exception. e.g. Math.Sign() throws an exception when passed a NaN?

What is a good design? Which one should I follow when writing my own mathematical methods?


Solution

  • int Math.Sign(Double value) returns an integer ... (-1/0/1). Double.Nan doesn't seem like an integer.

    Probably that's the main reason why it throws an exception.

    It could also be discussed why Int.NaN doesn't exist, we already had that discussion at Why is Nan (not a number) only available for doubles?

    The behaviour of Math.Sign(Double) is documented at https://msdn.microsoft.com/en-us/library/ywb0xks3(v=vs.110).aspx