Reputation: 2286
I have a float that can be any value on the positive or negative scale.
If the float is less than 1 or greater than -1 AND not 0, it needs to be rounded to 1 or -1.
I have a function I've made that does the trick but I have a feeling there is an elegant inline mathy way of doing this but I just cant come up with one.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float NormalizeScalar(float input)
{
if ((input > -1) && (input < 1))
return (input == 0) ? 0 : (input < 0) ? -1 : 1;
return input;
}
Any suggestions?
Upvotes: 1
Views: 674
Reputation: 7656
Use Math.Sign(input)
.
If your input is 0.78 -> returns 1.
If it's 0 -> returns 0.
If it's -0.78 -> returns -1.
Upvotes: 3