gorules / zen

Open-source Business Rules Engine for your Rust, NodeJS, Python or Go applications.
https://gorules.io
MIT License
835 stars 80 forks source link

Cannot use a decimal smaller than 0.0000009999999999999999 #272

Closed LucasLongarini closed 2 days ago

LucasLongarini commented 4 days ago

We recently discovered that using a decimal value smaller than 0.0000009999999999999999 causes the evaluate function to throw with Hook timed out. To reproduce this, run a rule file with the example input:

{
  testNumber: 0.0000009999999999999998
}

Platform: Node 20

stefan-gorules commented 3 days ago

Hi @LucasLongarini, this is expected as the given number cannot be represented as a decimal.

We use rust_decimal for number representation. This was done intentionally in order to properly serve domains where precision loss during calculation is not acceptable (think fintech, insurance, regulatory compliance, etc.).

To give you better idea, as example on Node.js (because it follows IEEE-754):

0.1 + 0.2 = 0.30000000000000004

Is there any specific use case that you have where floating point calculations are needed and cannot be served by rounding above number to 0.0000001?

LucasLongarini commented 2 days ago

Thanks for the explanation. Yeah in our case we were taking a percentage output from a ML model which could end up being very small. We did just end up rounding.