Thread: Interpreter: Data type question

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    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!!

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    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!!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    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!!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thank you CornedBeef!


    You opened my mind up!
    Just GET it OFF out my mind!!

Popular pages Recent additions subscribe to a feed

Similar Threads

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