java - Groovy number comparision issues -


i trying retrieve few values database , use them in groovy script in order find , compare differences.

an example below (when using float value in definition , in database defined number , precision 2), how output looks

> 

typically seeing values expect both differences 16, not.

any suggestions on have missed ?

when numbers stored in database type of number(x,2) or similar, means getting exact numeric value 2 places after decimal. converting these floats introducing error (a small , bounded error, noticeable 1 have found out), because there not exact representation of these numbers. numbers when converted binary infinitely repeating , have truncated somewhere. bigdecimal represents numbers exactly, long don't construct them passing in floating point value, in case damage done before bigdecimal gets involved (all can @ point represent floating point approximation of original number). think using bigdecimal easy solution problem.

since it's groovy script assume performance not biggest consideration, if (and you're concerned garbage created instantiating bigdecimals) might consider delta approach suggested jb nizet. (see this article peter lawrey pointers on how work floating point numbers.) java's gc expects lot of ephemeral objects, it's not biggest performance problem.

there's question floats vs decimals here answers describe why floating-point not exact.


Comments

Popular posts from this blog

asp.net mvc 3 - Using mvc3, I need to add a username/password to the sql connection string at runtime -

kineticjs - draw multiple lines and delete individual line -

thumbnails - jQuery image rotate on hover -