1

I compute two values x and y. If they are almost equal then - in theory - they should be exacactly equal but because of floating point math they are slightly off. In order to fix this problem I simply round every number in my program and it seems to work. However I became aware that this method could fail if x and y are

x = 1.1234567849999

y = 1.1234567850001

These numbers I surely want to be equal, but if they get rounded to the 8th digit x will go down and y will go up. I don't understand floating point math very well and I want to ask if this is a real danger or if such a case is only a problem in theory that do not occur practise?

laneo
  • 21
  • 5
  • You should only rarely compare floating point numbers for equality, because of rounding errors. Instead, check if `Math.abs(x - y) < EPS`, where `EPS` is some small-ish number below which you want to consider them "equal". – Andy Turner Oct 20 '21 at 14:16
  • 2
    Not *exactly* what you're asking but take a look at [How to compare two double values in Java?](https://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java). – Federico klez Culloca Oct 20 '21 at 14:16
  • It is indeed the case that if you round two floats they might be further apart after rounding instead of closer together. Checking if they are within a certain distance of each other is more appropriate. – khelwood Oct 20 '21 at 14:19
  • Thank you for the answers. I will change my program to take care of that. – laneo Oct 20 '21 at 14:26

1 Answers1

0

Multiply the two double by 100000000.0 and cast this to an int then take that int and cast it to a double and divide by 100000000.0

    double d = 1.3493423425234;
    int temp = (int)(d*100000000.0);
    double df = ((double)temp)/100000000.0;
    System.out.println(df);
noah1400
  • 1,282
  • 1
  • 4
  • 15
  • Wouldn't this method fail if the number are x = 1.123456799999 and y = 1.123456800001 ? – laneo Oct 20 '21 at 14:39
  • @laneo You must of course specify a tolerance. From how many decimal places are the two numbers equal? – noah1400 Oct 20 '21 at 14:46
  • The computing of the values involves Gaussian Elimination. So most of the time they have as much digits as a double can have. – laneo Oct 20 '21 at 14:50
  • @laneo From how many equal decimal places do you call the two numbers equal? – noah1400 Oct 20 '21 at 14:54
  • sorry I missreaded your question. For me the accuracy doesn't matter so much. I'm mainly concerned that numbers that are equal in theory become equal in my program. – laneo Oct 20 '21 at 14:57
  • @laneo shouldn't it be like that? – noah1400 Oct 20 '21 at 15:04
  • should be like what? For my application I need a method that make theoretical equal number really equal in *all* cases. However with rounding or casting to int and then to double I see no way to garanty it, no matter how many decimal places I would choose to call the numbers equal. – laneo Oct 20 '21 at 15:16
  • @laneo Let's say two numbers are equal as soon as 8 decimal places are equal. My code breaks both numbers down to 8 decimal places ( cuts). Then you have to compare both numbers to find out if they are the same. – noah1400 Oct 20 '21 at 16:07
  • I think your code will work if the first 8 decimal places are equal. But there might be numbers where Math.abs(x - y) < EPSILON; with EPSILON = 1e-8 that do not have the same first 8 decimal places. For example consider x = 1.49999999999 and y = 1.50000000001. Here the difference is even smaller than 1e-10, but by cutting of at the 8th decimal place they will become different and will not be detected as equal. – laneo Oct 20 '21 at 16:30