![]() |
| | #1 |
| Ugly C Lover Join Date: Jun 2008 Location: Indonesia
Posts: 462
| 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) ... ![]() Anyone experienced on this? Any better solution?
__________________ Just GET it OFF out my mind!! |
| audinue is offline | |
| | #2 |
| (?<!re)tired 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 | |
| | #3 |
| Ugly C Lover 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 | |
| | #4 |
| Cat without Hat 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 | |
| | #5 |
| Ugly C Lover 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 | |
| | #6 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Quote:
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 | |
| | #7 |
| Ugly C Lover 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |