1

I am working on a program that finds 2 slopes and them compares them to see if they are equal. I am using doubles for this. I am finding that even though my slopes should be "equal" they are coming out a little bit off and so aren't showing as being equal. For example I get one slope of 1.0000000000000009 and 0.9999999999999999. So they should be equal in theory, but they aren't.

What can I do to fix this?

rachel
  • 63
  • 1
  • 4
  • 9
  • 5
    Step #1: Show us the code. – Dave Jarvis Feb 15 '13 at 20:00
  • 1
    I think this is probably a precision issue. Often times when you operate on floats and doubles a little precision is lost. I think this will help you solve your problem: http://stackoverflow.com/questions/2389457/java-loss-of-precision – Daniel Kaplan Feb 15 '13 at 20:01
  • 3
    This is a floating-point FAQ. [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Alan Krueger Feb 15 '13 at 20:01
  • http://stackoverflow.com/questions/177506/why-do-i-see-a-double-variable-initialized-to-some-value-like-21-4-as-21-3999996 – assylias Feb 15 '13 at 20:01
  • Lots of duplicates when googling *Java double comparison*: http://stackoverflow.com/q/11390853/1065197 http://stackoverflow.com/q/434657/1065197 http://stackoverflow.com/q/1726254/1065197 ... – Luiggi Mendoza Feb 15 '13 at 20:01
  • http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/ – assylias Feb 15 '13 at 20:04
  • @AlanKrueger - good link – user1428716 Feb 15 '13 at 20:05
  • @user1428716 I've just seen it posted on other instances of this question on StackOverflow. If I didn't post it, someone else would have shortly. =) – Alan Krueger Feb 15 '13 at 20:06

2 Answers2

2

If you are using doubles, you will never fix this: there's limitations in the way decimal numbers are represented, which accumulate across your calculations, and give you these tiny differences. A good code audit would actually warn you of equalities between doubles.

The one way to handle it is to ensure that the absolute value of the difference is under a certain limit you set.

Here's an article on floating point number representation and the hazards of comparisons.

Alternatively, you could switch to better classes for precise number handling, such as BigDecimal.

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • 1
    BigDecimal better than double in two cases: if decimal fractions are special, or if there is a suitable finite precision greater than that provided by double. Calculating a slope might involve fractions such as 1/3, that are not exactly representable in BigDecimal regardless of scale, leading to rounding error. – Patricia Shanahan Feb 15 '13 at 20:14
0

to compare the 2 slopes you could set some PRECISION_LEVEL and then compare as follows

final double PRECISON_LEVEL = 0.01;
if(Math.abs(slope1 - slope2) < PRECISION_LEVEL) {
    System.out.println("Equal");
} 
else {
    System.out.println("Not Equal");
}
Kailua Bum
  • 1,368
  • 7
  • 25
  • 40