I’m searching for an example when the expression
(1.0/x)*x - 1.0
does not evaluate to 0.0, assuming that x is a double value in Java.
I’m also interested in a reason for that.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a program that will list all values for which this is the case.
Turns out there’s lots and lots and lots of them – the program starts with a large range of denormalized numbers, and it’s the case for all of them.
To get numbers in a more familiar range:
Still lots and lots of hits.
The reason?
doublehas limited precision, and therefore it’s fundamentally unable to represent all numbers. If your calculation has an intermediate result that cannot be exactly represented bydouble, your algebraic equalities cannot be expected to hold.