#Precision of double, zero not zero

15 messages · Page 1 of 1 (latest)

somber stirrup
#

Code below is not equal to zero, how to properly deal with it? I don't want to lose precision
Math.cos(Math.toRadians(90))

balmy geyserBOT
#

⌛ This post has been reserved for your question.

Hey @somber stirrup! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

green stone
#

You can either round or accept that doubles aren't perfect

#

I suggest the second

#

otherwise you'll be disappointed about 0.1 + 0.2

#

Alternatively use BigDecimal which is actually accurate but a lot slower

somber stirrup
#

It surprised me a little :D, thank you, I will stay with doubles. Do you know how to prepare tests in JUnit, that handle it?

green stone
#

you can add an interval which means it is correct

#

expected <= actual + dt && expected >= actual - dt

#

where dt is an abitrary precision

#

(stands for delta)

somber stirrup
#

Thanks 🙂