You can tell if there is a mathematical library for performing an arbitrary arithmetic function, taking into account the division by 0. For example, I have a string, which is transformed into an arithmetic expression by some transformation

"1 + 2 / 0"; 

Executing this expression with the eval () function will give the error "Division by zero". But I need such 2/0 plots to be defined and give the value 0. This is important, since I have a few hundred lines of this type that are generated on the fly. There are lines in which there are many levels of calculation with quotes, when the data is first calculated in quotes (with division by 0), then divided into a variable (which can also be zero), etc. Manual analysis of such lines for manual interception of such places I could not write - it is very difficult. Are there any libraries that can work around this error correctly? Tried the following libraries:

  • rodriados / mathr
  • mossadal / math-parser
  • fintara / calculator-php

They all do not fit

  • And where did you get these lines? Looks like a xy problem. - pavel
  • The system manually creates various rules for calculating some parameters, for example, "1 - $ a / $ b", where the variables $ a and $ b are substituted from the current data entered from the outside. And these values ​​can be any, including zero. And 0 is a normal valid value - uralmas
  • A more detailed example is "($ a + $ b / $ c) / $ d + $ e * $ f". Values ​​in variables are substituted any - from 0 to infinity (almost). In this case, if the values ​​of $ c and / or $ d are equal to 0, the whole function will produce a NAN (php 7.1), but I need it to give the result of the expression "$ e * $ f" - uralmas
  • @pavel, that's right. The ruin is not in the closet ... - avp

0