Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,113 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,765 people online right now. Registration is fast and FREE... Join Now!




Using logarithms in switch

 
Reply to this topicStart new topic

Using logarithms in switch

Wild_Rose
16 Apr, 2007 - 05:53 AM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 39


My Contributions
Hi everyone!
I'm working on a small program which will convert meters to kilometers/centimeters/millimeters. This should be done with the switch function but the thing is that the numbers that will be used are double (and switch only works with integer or character) because it will have to print 1 to 3 digits before the decimal.
So I searched and found that I could use the decimal logarithm somehow like this?
floor(Log10( B ))
but I can't figure out how to do that!

any help would be much appreciated! thanks for your time.

This post has been edited by Wild_Rose: 16 Apr, 2007 - 05:53 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Logarithms In Switch
16 Apr, 2007 - 05:56 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
I'm not sure of the question...is it the log function you're not familiar with?
CODE

double result = log10(100);

gives the log of 100.

sorry for the question, but what is it exactly you wish to accomplish with the log function?
User is offlineProfile CardPM
+Quote Post

Wild_Rose
RE: Using Logarithms In Switch
16 Apr, 2007 - 05:59 AM
Post #3

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 39


My Contributions
well the numbers are double, how can I make them work with switch?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Logarithms In Switch
16 Apr, 2007 - 02:39 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
option 1, don't use a switch use

if (condition1)
{
do something
} else if (condition2)
{
do something else
} else if (condition3)
{
do some more stuff
}
...
} else
{
do whatever
}


or you could use a fixed point scheem where you convert your double to an integer by multiplying it by a power of 10 and then coverting it.

so floor(3.14156265 * 1000) = floor(3141.59265) = 3141

int val = (int)(dblVal * 1000);

The problem with this is if your value is very large, or very small. Then you would use the log function to get a more acceptable range:

int val = (int)(log10(dblVal) * 1000);

Myself, I would use the if-else-if method. switch statments are a hassle and they don't really give you a speed boost (esp if you have to use the Log function).
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Using Logarithms In Switch
16 Apr, 2007 - 02:54 PM
Post #5

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
I'm with Nick, but your class probably just wants you to be familiar with switch statements. Most compilers/linkers/assemblers will convert switches to ifs and then to assembly of the jmp statements etc anyway, so writting the if statments efficiently yourself is usually the better option.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Logarithms In Switch
16 Apr, 2007 - 04:07 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
So many people have told me"Oh using the switch statment saves clock cycles" and yet I have yet to see it. Those times I looked at the disasssembly the if-else-if versions were shorter and more clearly laid out. So at least I know with the if-else-if method I will understand the assembly if I need to.

My feeling is that someone read it somewhere talking about spacific optimizations on a spacific platform and took it to mean "in general". Then he told his friends, and they thier friends, and then some kid with 6 months of coding experiance and a know-it-all attitude who ends up my parner on a project insists that we use the switch statment........

Wiat a min... I don't sound bitter do I? biggrin.gif (yes the above story is fiction)
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Logarithms In Switch
16 Apr, 2007 - 04:20 PM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Generally speaking, the reason it is assumed that a switch statement is more efficient is because of the way compilers optimize code. With an if else statement, the code enters a decision path that may include complex decisions at lower nested levels...with a switch statement, the compiler is only evaluating simple matches, none of which is dependent on previous code, so it can reorder it in an optimized manner.

That is the general reasoning, but that reasoning was applied to C programming some time ago...it does not always apply to newer languages like C++ or even to many of todays compilers.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Using Logarithms In Switch
16 Apr, 2007 - 09:47 PM
Post #8

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE

So many people have told me"Oh using the switch statment saves clock cycles" and yet I have yet to see it. Those times I looked at the disasssembly the if-else-if versions were shorter and more clearly laid out. So at least I know with the if-else-if method I will understand the assembly if I need to.

The code generated for switch constructs is different.
It doesnt consist of multiple jumps at all.

It just uses ONE JUMP like this:
call [edx + eax*4]

where edx contains the base address of the table which contains addresses for each case, and eax contains integer values.

Now, that's efficient.

I have seen such implementation in disassembled listings of programs compiled with Watcom C++ and Borland C++. Im not sure of VC++ though.


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Logarithms In Switch
17 Apr, 2007 - 09:28 AM
Post #9

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
The switch statment generates a jump table? maybe that is why I never understood them. I will have to look it over again. I jump table makes a lot of sence and I would have also imagined that it would be faster for large amounts of choices. I have only used jump tables in FSM's, I don't think it even occured to me to look for one.

See, you learn something new every day.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:46PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month