Technically, you would only need two - IF-ELSEIF-ELSE (using QB syntax) or IF-(ELSE ==> IF-ELSE). Why use IF, IF, IF? :P Technically one of the following could be added into qbx.cpp right now. It would just be a matter of the compiler properly recognizing and converting your QB[64] source using SGN to the necessary C++. /* C macro for it */ #define SGN(x) ((x < 0) ? (-1) : ((x > 0) & 1)) /* C function for the same thing */ int qb_sgn (int x) { return ((x < 0) ? (-1) : ((x > 0) & 1)); } /* C function using an easy-to-read if construct */ int qb_sgn_easy (int x) { if (x < 0) return -1; return ((x > 0) & 1); } ' QB version FUNCTION qbsgn% (x AS INTEGER) ' Yet another one-liner, ' just for the guy that likes them. :P IF x < 0 THEN qbsgn% = -1 ELSE qbsgn% = ((x > 0) AND 1) END FUNCTION How it works: If the integer is less than 0 then return -1, of course. If the integer is greater than 0, that part in the inner parentheses evaluates to 1*, and a bitwise AND operation is performed on that result and 1. Obviously 1 AND 1 is 1. If the integer is equal to 0, the inner parentheses evaluates to 0, and 0 AND 1 is 0. * - It might evaluate to -1 on some systems, just like in QB, though the reasons would be unknown to me. My intent with the bitwise AND is for portability while avoiding an extra CMP/Jcc pair in the resulting ASM code, which on old systems (DOS days) would take up clock cycles like crazy. I must admit that I agree with Artelius... Learning C really is beneficial. ASM is even more beneficial, though it can drive you insane if you use it too much. Performance note: The macro would be slower and would bloat the code more if a lot of calls to SGN were made. After all, it would need to insert that exact bit of code each time. However, for a one-time use, there would be no need to play with the stack to set everything up for a function call. I would personally use the function, especially because of the explicit types, but the implementation is up to Galleon really. Author note: Do you enjoy my religious use of parentheses? Good. Since I work with various languages, it really isn't worth my time to memorize operator precedence for each language, and it definitely isn't worth my time to hunt for bugs when they could have been prevented beforehand using parentheses which take extremely little time to type. A part of this comes from working with C macros and having them come back to bite me in the end (one compiler seemed to want to evaluate "?-1", for example - I hate buggy implementations). Enjoy while you can, minions... Mwahahahaha-- cough cough cough HACK cough cough ------------------ Waiting patiently for XHTML 2.0, CSS 3.0, the ratification of C++0x, and the day that I can code without logic troubles. from IP address 12.208.120.144 |
| Response Title | Author and Date |
| ok, one if then | mennonite on Jun 26 |
| *and that's probably because the question is in another thread | mennonite on Jun 26 |
| Another way | qbguy on Jun 27 |
| qbx.cpp uses the 3-if version | qbguy on Jun 27 |
| which do you think is easier to read? | mennonite on Jun 27 |