0

I just got caught by surprise because management thinks it's a good idea to use Boolean expressions in calculations such as

x = (y < 4) * 1 + (z < 8) * 4 ...

I use NCalc for formula evaluation, unfortunately NCalc complains because I'm trying to multiply an integer and a bool.

Is there a way to expand NCalc so false is converted to 0 and true is converted to 1 or will I have to dig deep and make some modifications to the NCalc codebase?

Thorsten Westheider
  • 10,572
  • 14
  • 55
  • 97
  • Seems like a rather low-level thing for management to be dictating IMO... – Dark Falcon May 18 '15 at 14:37
  • Product management. We ship software which, among other things, features assessments, the configuration of which involves free-form calculations that can be imported from XML. Product management is responsible for these configuration files. – Thorsten Westheider May 18 '15 at 16:06
  • OK, I suppose that might make some sense there. That said, I doubt you'll be able to do it without modifying the code. – Dark Falcon May 18 '15 at 16:21

1 Answers1

0

I ended up changing the source code. Just in case anybody should run into the same issue, there's a bunch of switch statements in Numbers.cs where you can change the behavior for every type of operator. In my particular case that was Multiply(object a, object b) where I changed the default behavior from throwing an exception to this:

case TypeCode.Int32:
   switch (typeCodeB)
   {
      case TypeCode.Boolean: return ((bool) b) ? (Int32)a : 0;
   }
...
Thorsten Westheider
  • 10,572
  • 14
  • 55
  • 97