private ushort GetUShortUnsignedFloatSmallerThan512(float floatVal)
{
if (floatVal < 0 || floatVal > 512)
{
throw new FormatException($"Error, can only represent values in the following ranges: 0 <= x <= 512, value given: {floatVal}");
}
ushort ushortVal = 0;
for (sbyte bitIndex = 15; bitIndex >= 0; bitIndex--)
{
float takeAway = Mathf.Pow(2f, bitIndex - 7);
float newVal = floatVal - takeAway;
if (newVal < 0)
{
continue;
}
ushort bitValue = (ushort)Mathf.Pow(2f, bitIndex);
ushortVal += bitValue;
floatVal = newVal;
}
return(ushortVal);
}