mparlak / Flee

Fast Lightweight Expression Evaluator
629 stars 120 forks source link

Divizion by Zero if IntegersAsDoubles is enabled #74

Open alexiv opened 4 years ago

alexiv commented 4 years ago

Is any way to get DivizionByZeroException if IntegersAsDoubles is enables in expressions like 1/(1/0)?

hunkydoryrepair commented 3 years ago

I don't think that would be possible given that Flee generates MSIL. Dividing a Double by zero in .NET results in Double.PositiveInfinity, which represents an infinite positive number. Dividing 1 by PositiveInfinity results in 0, so the expression actually gives the correct result: 1/ (1/0) can be rearranged to be (1 * 0)/1, so 0 is the correct value.

By default, the expression "1.0 / (1.0/0.0)" will not throw an exception. However, if you set RealLiteralDataType to Decimal, it will, since Decimals do not have a PositiveInfinity representation. Interestingly, using IntegersAsDoubles and setting RealLiteralDataType to Decimal together does not work. The Integer is still treated as a double, not as a decimal. Perhaps there needs to be a IntegersAsDecimal option.