An array is a table.
The sequence of Boolean truth values ββis a binary number.
Arrays are indexed by number.
Sooooo .....
You can define a function for each calculation:
// - + + == 0 1 1 == 3 inline double f3(double prev, double curr, double next) { return curr / (curr - prev); } // - - + == 0 0 1 == 1 inline double f1(double prev, double curr, double next) { return next / (next - curr); } // ...
Then declare an array of function pointers:
typedef double (*func_type)(double, double, double); func_type funcs[8] = { f1, f2,
Then index into the array using logical conditions in the form of binary digits:
func_type f = funcs[ (int(prevdst5 < 0.0)<<2) | (int(dst5 < 0.0)<<1) | int(nextdst5 > 0.0) ]; thawpct = f(prevdst5, dst5, nextdst5);
- What if another input is added? (Then the number of tests will be 16, which, in my opinion, will be cumbersome to manage with the current template)
You double the size of the array and add f8 , f9 , etc., if you need different calculations, but you only add a new test once to the array index:
func_type f = funcs[ (cond<<3) | (int(prevdst5 < 0.0)<<2) | (int(dst5 < 0.0)<<1) | int(nextdst5 > 0.0) ];
- What if several input combos should be displayed on one output?
Keep the same pointer to several elements of the array:
func_type funcs[8] = { f1, f2, f3, f1, f1,
Jonathan wakely
source share