473,473 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why we cannot use float in switch?

1 New Member
I cant understand the logic that why we cannot use floating variable in swtch n we prefer integer or char?????????am confused.
Sep 20 '11 #1
2 34327
Rabbit
12,516 Recognized Expert Moderator MVP
It's because floats are imprecise. You never know what that number is actually going to be.
Sep 20 '11 #2
donbock
2,426 Recognized Expert Top Contributor
The bottom line is that the argument to a switch statement can't be a floating-point type because the C Standard says that is prohibited.

Why does the C Standard care?
Rabbit has a very valid point.
Another possibility is so optimizing compilers have the option of implementing a switch instruction as a branch table. It is a lot easier to convert an integer argument into a table offset that it is for a floating-point argument.
Sep 20 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: andrea_gavana | last post by:
Hello NG, probably because I still have Python 2.3.4, these are the results I'm getting: C:\Python23\Lib>python timeit.py -s "floats = map(float, range(1000))" "ints = m ap(int, floats)"...
6
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
3
by: Tony Liu | last post by:
Dear All: I create a very simple DLL by using EVC to test the problem. (The platform I am working for those program is WinCE.NET) ******************************************************* The...
9
by: Eric S. | last post by:
Why doesn't something like this work? I get "a constant value is expected" on all 3 case statements. private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg) { try { switch...
3
by: Andrew Murray | last post by:
Is it possible to compare a double with a float in c# without casting to one of the types first? It appears you cannot... float num = 1.545f; double dnum = 1.545; if (dnum == num)
5
by: nobody | last post by:
With Visual C++ 2005 beta 2, I get the below compling error for the following code. I think this error is not acceptable to me because int::typeid is a constant and is known to compiler when...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
8
by: Gernot Frisch | last post by:
Uhm... Is there any way of making float pos; a union of float x,y,z; somehow, so I can use: mything = mything.y;
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.