Share via


Why switch case can't accept floating-point in condition?

Question

Friday, July 10, 2015 8:47 PM

Hello,

Why switch case can't accept floating-point numbers(Ex:float,double) in condition?

All replies (6)

Friday, July 10, 2015 10:34 PM ✅Answered

Hello,

Why switch case can't accept floating-point numbers(Ex:float,double) in condition?

The developers of C# decided that thier switch would only use the types sbyte, byte, short, ushort, int, uint, long, ulong, char, string, and enumerations.

I have no precise data on why they choose not to include floating point types on that list. My best guess is it is because floating point values are inherently not precise.
Switch statements are all about precise matching. They are basically a bunch of if(value == switchLabel) statements with a single else way (the default label). Switch is just a lot nicer to read and follow.

It is pretty hard to match one inprecise value on another inprecise value. The behavior would be unpredictable.

The exact details are propably a few kilometers deep in computer science ce thematic.


Saturday, July 11, 2015 12:29 AM ✅Answered

Hello, Arash_89.

Thomas is explaining (I believe) that using real number types in a switch is potentially troublesome because in a computer, real numbers are approximations (very precise approximations, but approximations nonetheless).

What if you had a switch based on a double declared as 'x' and a case for 1.0125, then entered the switch with x == 1.01249999999? Is it a match or not?

I also believe that there is a certain amount of tradition involved in the types allowed in a C# switch construct. Both C++ and Java restrict the types allowed in the same way.


Saturday, July 11, 2015 1:53 AM ✅Answered

Why switch case can't accept floating-point numbers(Ex:float,double) in condition?

There are lots of articles out there which explain why comparing floats/doubles for exact
equality is usually a mistake. Especially if the comparison is of the results of
calculations. See for example:

Comparing Floating Point Numbers, 2012 Edition
https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Note the section: "Comparing for equality".

Floating point number representation
http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.html

Note the section:

"III. Effective FP Programming"

...

"Equality"

"First let's tackle that pesky issue of equality: why is it so hard to know when
two floats are equal?"

Why Floating-Point Numbers May Lose Precision
https://msdn.microsoft.com/en-us/library/c151dt3s.aspx

  • Wayne

Saturday, July 11, 2015 11:06 AM ✅Answered | 1 vote

"I also believe that there is a certain amount of tradition involved in the types allowed in a C# switch construct"

Indeed. The C# switch statement was adapted from the C switch statement. And C only supports integral types in a switch. The only improvement that C# made to switch was adding support for strings, possibly because it's quite common to want to check if a string matches certain predefined values.

As for why C only allowed integral types, one can only guess:

  • integral switches can be implemented in many cases by using jump tables. The switch value is simply an index into a table of addresses that correspond to each case
  • C's primary use at the time was to write operating systems and operating systems tend to avoid floating point. In fact, at the time when the C language was created floating point probably didn't even exist, at least not in form we know today.

Friday, July 10, 2015 9:27 PM

Because normally you don't want to switch if a value is for example 1.000101 and then 1.0012.

So you should use an if/else-combination that tests ranges. Or you could do some math, here's an example:

http://stackoverflow.com/questions/12398719/using-double-in-switch-statement

Thomas Claudius Huber

"If you can't make your app run faster, make it at least look & feel extremly fast"

My latest Pluralsight-courses:
XAML Layout in Depth
Windows Store Apps - Data Binding in Depth

twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com


Friday, July 10, 2015 9:32 PM

I don't understand.