0

Why switch statement label should not be float type

5th Jan 2017, 6:50 AM
Sravan Kumar
Sravan Kumar - avatar
3 odpowiedzi
+ 13
"...the reason that switching on float and double is not allowed is most likely that the insidious effects of rounding errors and imprecise representations of floating point numbers would make code that uses floating point switches very error prone ... or require a special syntax for expressing error bounds in the case values." Source: http://stackoverflow.com/a/14316681
2nd Mar 2017, 1:43 PM
Jafca
Jafca - avatar
+ 1
I am going on a gamble here. I guess you are asking this. Why you should not compare with float values in switch statements. That is because, comparing values using a switch statement must be the exact value. a must equals b. In a switch you can not compare using mathematical signs like <, >, <=... switches only use ==. which you can not change.
5th Jan 2017, 9:27 AM
Andreas BeEm Kvist
Andreas BeEm Kvist - avatar
+ 1
I don't exactly know what you're trying to say but try adding F to the literal (e.g. 1.42F). List of C# literal suffixes (case insensitive): unsigned - U long - L unsigned long - UL float - F double - D decimal - M
6th Jan 2017, 12:54 AM
Maike