fasseg / exp4j

A tiny math expression evaluator for the Java programming language
http://www.objecthunter.net/exp4j/
Apache License 2.0
475 stars 160 forks source link

Precision on result #96

Closed devnev0 closed 4 years ago

devnev0 commented 5 years ago

I don't know if it's a bug but I don't understand why exp4j gives this result. 249 * 0.595 = 249.305 BigDecimal.valueOf(419).multiply(BigDecimal.valueOf(0.595)); This give 249.305 too. I wait same with exp4j

Now with exp4j: new ExpressionBuilder(regle).variable("d").variables("d").build().setVariable("d", 419).evaluate(); I obtain 249.30499999999998.

Do you know what is wrong?

RobertZenz commented 5 years ago

Yeah, read up about floating-point precision. In short, float and double is, mostly, an approximation of values.

devnev0 commented 5 years ago

Yes I think too. It's bad me. Using Double is not a good thing in my app. Do you know an alternative with this lib or other?

RobertZenz commented 5 years ago

There is EvalEx which is similar and uses BigDecimals...and before you ask look at EvalEx#114.

devnev0 commented 5 years ago

Thank you. I try it.