2

So I'm working on a big Java project and in it I have a part with user submitted equations, both mathematical and for things like If/else. I've been using a scriptengine with javascript to get the results. I've run into the issue of the floating point errors for rounding. For example math.pow(1000,1/3) returns 9.999999999999998 instead of 10. (before anyone says it, I'm aware of the cube root function this is just an example).

Basically I need a solution that gives the proper numbers. Whether this means a different scriptengine other than javascript or someway to tell the script to round all numbers off to x decimal places I don't know. I don't need 15 decimal places of precision so I can make that trade off but I don't know a practical way to implement this on the entire equation. The problem coming from the equation possibly being different each time. I'd also consider doing something like the shunting yard algorithm in java itself but it has the exact same problem. Basically I need to pass the code 1000^1/3 and know I'm getting 10.

  • There is no solution for proper numbers when working with `double` values. Your problem has nothing to do with scriptengine, and everything to do with double-precision rounding errors. Search "[java double rounding error](https://www.google.com/search?q=java+double+rounding+error)" and you'll find many articles on the subject. Heck, you can drop the `java` part and search result will be relevant, because it has nothing to do with Java, JavaScript, C, Python, or any other language. – Andreas Apr 09 '16 at 07:08
  • Possible duplicate of http://stackoverflow.com/questions/960072/rounding-errors – Andreas Apr 09 '16 at 07:09
  • By using `1/3` in your example you've lost precision even before you start doing any calculations. At least with `Math.cbrt` you avoid that problem. – Sam Apr 09 '16 at 07:15
  • I'm aware of what causes the problem. I've been looking into for hours now. I'm trying to find a solution, workaround or alternative. I can't believe there isn't a solution for this. I understand that Math.cbrt exists it was just the example I chose for the overall problem not the entirety of the issue. right now the approach I'm leaning towards is to switch to java only with shunting yard and round off each result to 10 decimal places. not ideal but it's the only thing I can think of. too bad I'm stuck with java because c# gives the right answer of 10 at least. – Gregory McIntyre Apr 09 '16 at 12:28

0 Answers0