473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get Value of Digit


I have a Graphical User Interface program and I'm working with numbers
written in base-10.

Is there a better way than the following to get the digit value?

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

Any reason why the above code wouldn't be fully portable?

-Tomás

Mar 1 '06 #1
7 2363
Tomás wrote:
I have a Graphical User Interface program and I'm working with numbers
written in base-10.

Is there a better way than the following to get the digit value?

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

Any reason why the above code wouldn't be fully portable?

-Tomás


Assuming you've already done error checking so you're sure the
character is between '0' and '9', you could just do this:

return unsigned(c - '0');

Your code is indeed portable, albeit inelegant.

Cheers! --M

Mar 1 '06 #2
mlimber wrote:
Tomás wrote:
I have a Graphical User Interface program and I'm working with numbers
written in base-10.

Is there a better way than the following to get the digit value?

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

Any reason why the above code wouldn't be fully portable?

-Tomás

[snip] Your code is indeed portable, albeit inelegant.


.... and, to emphasize the point, possibly incorrect if you haven't done
error checking on c before entering this function. (What would happen
if c had a value of 'a'?)

Cheers! --M

Mar 1 '06 #3
There are some character tables (ASCII, EBCDIC, ....), and you can't
assume what position occupies numeric characters. But in practice, in
all examples I know they have consecutive positions.
If you assume that, you can calcule offset with the first element:
Example:

You can get value 0 with the operation 0 - '0'
Then you can write your own sentence:
return c-'0'

I can't assure that in all the cases, numbers are consecutive in any
codification, but in all cases I know, they are.

There are other alternatives:
- using C's sscanf, but you must use a null terminating string
- using stringstream (you can construct a stringstream and then use the
operator >> )

Tomás wrote:
I have a Graphical User Interface program and I'm working with numbers
written in base-10.

Is there a better way than the following to get the digit value?

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

Any reason why the above code wouldn't be fully portable?

-Tomás

Mar 1 '06 #4
On 2006-03-01 09:17:30 -0500, Carlos Martinez Garcia
<cm****@nospam.tid.es> said:
There are some character tables (ASCII, EBCDIC, ....), and you can't
assume what position occupies numeric characters. But in practice, in
all examples I know they have consecutive positions.
If you assume that, you can calcule offset with the first element:
Example:

You can get value 0 with the operation 0 - '0'
Then you can write your own sentence:
return c-'0'

I can't assure that in all the cases, numbers are consecutive in any
codification, but in all cases I know, they are.


You *can* be sure that numbers are consecutive, as the standard
requires it. If c is a digit, then (c-'0') is guaranteed to produce the
value of that digit.
--
Clark S. Cox, III
cl*******@gmail.com

Mar 1 '06 #5


I know a lot of people here seem to dismiss my efforts as "over-zealous
optimization", but considering the two alternatives:

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

unsigned GetDigitValue( char const c )
{
return c - '0';
}

Firstly, the first one would produce longer assembly code, right?
But which would execute faster. Let's say this function is used inline, and
that it's called several times a second as it's decoding Gigabytes of
information... which do you think would be faster?

My guess is that the second would be faster.

How do you usually test the speed of code, say if you've two functions that
achieve an aim by different methods?

-Tomás
Mar 1 '06 #6
Tomás wrote:
I know a lot of people here seem to dismiss my efforts as "over-zealous
optimization", but considering the two alternatives:

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

unsigned GetDigitValue( char const c )
{
return c - '0';
}

Firstly, the first one would produce longer assembly code, right?
Probably.
But which would execute faster. Let's say this function is used inline, and
Well, the second is really easy to inline.
that it's called several times a second as it's decoding Gigabytes of
information... which do you think would be faster?
The second one.
My guess is that the second would be faster.

How do you usually test the speed of code, say if you've two functions that
achieve an aim by different methods?


Stick it in a loop and do it a million times. Your OS probably has
higher resolution timers than C++, so use the most appropriate.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 1 '06 #7

Tomás wrote:
I know a lot of people here seem to dismiss my efforts as "over-zealous
optimization", but considering the two alternatives:

unsigned GetDigitValue( char const c )
{
switch ( c )
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
}

}

unsigned GetDigitValue( char const c )
{
return c - '0';
}

Firstly, the first one would produce longer assembly code, right?
But which would execute faster. Let's say this function is used inline, and
that it's called several times a second as it's decoding Gigabytes of
information... which do you think would be faster?

My guess is that the second would be faster.

How do you usually test the speed of code, say if you've two functions that
achieve an aim by different methods?

-Tomás


The 2nd implementation will be much faster because branching code often
leads to CPU pipeline stalls and dramatically decreases performance.
Although CPU branch prediction engine can alleviate some of the
problems but it's best to write code that contains no branching as in
implementation 2.

Mar 1 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
3
by: JKop | last post by:
Consider the following platform: char = 8-bit short = 16-bit int = 32-bit long = 32-bit
9
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval...
14
by: TTroy | last post by:
Hello, can anyone explain why the following function will not work for INT_MIN: /* itoa: convert n to characters in s */ void itoa(int n, char s) { int i, sign; if((sign = n) < 0) /*...
6
by: Tony Tortora | last post by:
I am writing and add on application. The application uses Unique IDs and they are stored in Base 26 (ie 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ). I am having trouble reading the decimal value of a...
6
by: Michael.McD | last post by:
Hi, I want to create a type that behaves like in integer but only allows assignment of say 3 digit integers where attempts to assign values greater or loess will throw an exception. E.g. int...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
5
by: eyoung | last post by:
I have a function to check a string to make sure it is 6 digites using the trigger onBlur="CkFrmt(this)" Problem is I've got 4 fields in a row...if I enter a wrong number in the first and hit tab...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.