0
Why switch statement label should not be float type
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
+ 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.
+ 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