Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Switch statement with closed-ranges covering full Float range

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()
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!