+ 1

why we can't pass float variable in a switch case in C or C++?

22nd Jan 2018, 4:45 PM
dhiman mishra
dhiman mishra - avatar
3 Answers
+ 1
Floats are not exact. There are cases where too different real numbers will be equal when stored as floats. That makes them bad candidates for equality checks like switches.
22nd Jan 2018, 7:47 PM
1of3
1of3 - avatar
0
Because Switch doesn't take that data type.
22nd Jan 2018, 4:59 PM
josh mizzi
josh mizzi - avatar
0
It's "simple": you could write 0.3333333333333333 and 0.3333333333333331 as two different pattern. But, at compilation time, once floating-point is not precise, both values could result in the same pattern. That means your code is meaningless.
14th Aug 2022, 7:38 PM
Daniel Bandeira
Daniel Bandeira - avatar