Is there a good general pattern for implementing truth tables?
I am re-working some legacy code (C++) and just realized that the function I am working with amounts to a truth table with 3 binary inputs and 8 possible outputs. Here is an example of two of the eight tests and corresponding outputs:
// - + +
if ( (prevdst5 < 0.0) && (dst5 > 0.0) && (nextdst5 > 0.0) ){
thawpct = (dst5 / (dst5 - prevdst5));
}
// - - +
if ( (prevdst5 < 0.0) && (dst5 < 0.0) && (nextdst5 > 0.0) ){
thawpct = (nextdst5 / (nextdst5 - dst5));
}
// other cases...
return thawpct;
Basically I am wondering if there is a cleaner, more maintainable/extensible* way to set this up.
- What if another input is added? Then the number of if statements needed would be 16, which in my view, would be too cumbersome to manage with the current pattern.
- What if several of the input combos should map to the same output?
*The codebase is an ecosystem model used in academia, so maintenance and extension amount to similar things depending on the perspective of the coder.