🚨 Swift quiz: what does this print?
let total = 0.1 + 0.2
print(total == 0.3)
👀 It's false.
🧠 0.1 can't be stored exactly in binary, so total is really 0.30000000000000004.
❌ == on computed Doubles = a bug waiting for the wrong input.
🧵
🚨 Swift quiz: what does this print? let total = 0.1 + 0.2 print(total == 0.3) 👀 It's false. 🧠 0.1 can't be stored exactly in binary, so total is really 0.30000000000000004. ❌ == on computed Doubles = a bug waiting for the wrong input. 🧵
0 vue
Publié le 17/11/2026 04:42
1:08
🚨 Swift quiz: what does this print?
let total = 0.1 + 0.2
print(total == 0.3)
👀 It's false.
🧠 0.1 can't be stored exactly in binary, so total is really 0.30000000000000004.
❌ == on computed Doubles = a bug waiting for the wrong input.
🧵
abs(total - 0.3) < tolerance
🎯 And don't copy 0.000001 everywhere. Choose the tolerance from your domain: cents for money, millimeters for lengths.
⚡ "Close enough" has to be something you decide on purpose.
Want to learn more? Subscribe to my channel 👇
www.youtube.com/@CaneAllesta...