I am expecting the code below to cover the full Float
range of which I use only a very small part just above 0.
My app however sometimes crashes in the field because it hits default: fatalError()
.
What could possibly be wrong here?
var value: Float // Between a little above 0 and about 15.
...
switch value
{
case ...3: return 0
case 3...10: return 1
case 10...: return 2
default: fatalError()
}
CodePudding user response:
This switch will fall all the way through default for example if value is Float.nan
or Float.signalingNaN
, so it's technically possible for the app to legitimately crash at that point.
Consider adding some logging before crashing to check which value causes it to crash.
CodePudding user response:
You have covered all cases except for the values whose eight exponent bits are all 1s. None of these are "Between a little above 0 and about 15", so your assumption is currently incorrect.
https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Exponent_encoding