Math's Sign Method

In this week's one liner tutorial, I'm going to introduce you to the Math.Sign method. It's a simple little method that basically tells you the sign (positive, negative, or zero) of a number:

int sign = Math.Sign(-3.14);

If the number is less than 0, it returns a -1. If the number is a positive number, it returns a +1. If it is 0, a 0 is returned.

This probably isn't too useful for determining if a number is positive or negative. After all, for that, you can simply run x < 0.

Where it's actually helpful is in giving one number's sign has to another: y = Math.Sign(x) * Math.Abs(y);

It's a little known method, but if it's in the back of your mind, you'll find occasional places to use it.

Interestingly, this is in the System.Math class, so it is available in any C# app, not just even limited to game development. Also worthy of note: HLSL has an equivalent sign intrinsic function as well!