C Board  

Go Back   C Board > Community Boards > Tech Board

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2009, 08:06 AM   #1
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
Question Interpreter: Data type question

Just wonder, if we make an type-safe interpreter, and it support numeric types respectively...

Should we write operations like addition, substraction, etc... FOR EACH numeric types?

Code:
char add(char a, char b) ...
char add(short a, char b) ...
char add(int a, char b) ...
A little bit HARD WORK I think.

Anyone experienced on this?
Any better solution?
__________________
Just GET it OFF out my mind!!
audinue is offline   Reply With Quote
Old 07-03-2009, 09:13 AM   #2
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,219
Yup. That would be terribly annoying. Make it a 3 parameter function and the number of "combinations" would increase exponentially. And 4... and 5... Ugh!

Well, we don't have to do that. C++ has Templates that, among other things, can handle these type of situations.

For starters: C++ Templates Tutorial
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is offline   Reply With Quote
Old 07-03-2009, 12:59 PM   #3
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
Thanks for the reply.

That makes me sure about what I do.

Thank you Mario.

However it would be terrible thing if we do that in C..

Hopefully macros will be useful here.
__________________
Just GET it OFF out my mind!!
audinue is offline   Reply With Quote
Old 07-03-2009, 03:03 PM   #4
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
Meh, do what C does, introduce promotion rules. Basically in C, every integer type smaller than int is promoted to int before the actual operation, so
char = char + char
is really
char = (char)((int)char + (int)char)

Furthermore, the smaller data type is promoted to the larger one, so int + long = long, and so on. An unsigned type is "larger" than its signed counterpart.

That quite drastically cuts down the number of different operations, and if you assume a 2's complement machine and defined signed overflow (both are invalid assumption in standard C, but work in practice on most architectures), doesn't actually change the result.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 07-04-2009, 01:28 AM   #5
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
Doesn't it affect the performance, especially in loop?

For instance:
Code:
char = (char)((long long)char + (long long)char)

Btw I taught somewhere that we should use smallest size of variable as possible in loop to gain performance.


How about double? I've seen a lot of interpreter source which using double as numeric representation.
__________________
Just GET it OFF out my mind!!
audinue is offline   Reply With Quote
Old 07-04-2009, 03:51 AM   #6
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
Quote:
Btw I taught somewhere that we should use smallest size of variable as possible in loop to gain performance.
That's very wrong. The fastest integer type for a CPU is usually word-sized, i.e. 32-bit on 32-bit machines. AMD64 CPUs are special in that 32-bit integers are just as fast as 64-bit integers, maybe even faster under some circumstances. But shorter integer types, while they may be as fast as longer ones, are never faster, and sometimes slower.

The problem with double is the floating point rounding errors. If you're doing all your calculations in the floating domain, users who expect integers could see very unexpected rounding errors. So yes, it's possible (I believe various JavaScript interpreters do it), but you have to be careful.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 07-04-2009, 06:44 AM   #7
Ugly C Lover
 
audinue's Avatar
 
Join Date: Jun 2008
Location: Indonesia
Posts: 462
Thank you CornedBeef!


You opened my mind up!
__________________
Just GET it OFF out my mind!!
audinue is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pthread question how would I init this data structure? mr_coffee C Programming 2 02-23-2009 12:42 PM
Using VC Toolkit 2003 Noobwaker Windows Programming 8 03-13-2006 07:33 AM
Errors Rhidian C Programming 10 04-04-2005 12:22 PM
All u wanted to know about data types&more SAMSAM Windows Programming 6 03-11-2003 03:22 PM
gcc problem bjdea1 Linux Programming 13 04-29-2002 06:51 PM


All times are GMT -6. The time now is 07:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22