-2

There seems to be many approaches to judge whether two floating-point numbers are identical. Here are some examples I've found:

  1. fabs(x - y) < n * FLT_EPSILON * fabs(x) OR fabs(x - y) < n * FLT_EPSILON * fabs(y)

  2. fabs(x - y) < n * FLT_EPSILON * fabs(x + y)

  3. fabs(x - y) < n * FLT_EPSILON * fabs(x + y) || fabs(x - y) < FLT_MIN)

  4. fabs(x - y) / sqrt(x * x + y * y + FLT_EPSILON * FLT_EPSILON) < n * FLT_EPSILON

I'm really confused about them. Suppose there is a best way to compare two floating-point numbers, which is the fastest as well as the most accurate, the other approaches shouldn't even exist. So these different ways must have there own pros and cons.

My question is: Which approach is the fastest / most accurate / most practical?

Reference links:

http://accu.org/index.php/journals/1558 (1 and 4)

https://stackoverflow.com/a/10335601/5399734 (2 and 3)


Clarification: At least, I don't think "fastest / most accurate" is primarily opinion-based.

Community
  • 1
  • 1
nalzok
  • 14,965
  • 21
  • 72
  • 139
  • Where did you find these examples? Please post links. BTW, I'm impresssed that you converted the info all those different sources into exactly the same format for easy comparison:) – Martin James Mar 24 '16 at 07:34
  • @MartinJames Reference added. Hmm...I thought this could be a quite nice question, but now I'm getting 2 close votes and 2 downvotes. Do you know what I'm doing wrong? – nalzok Mar 24 '16 at 07:38
  • Well, it's not much of a programming problem. It's more CS/maths. – Martin James Mar 24 '16 at 07:39
  • Oh that's it! I'm going to move this question to CS. Thanks for the enlightenment :) – nalzok Mar 24 '16 at 07:41
  • None of the questions has an answer, apart possibly from fastest. Which is irrelevant. Do you want to do the right thing? Or the wrong thing quickly? If you want to do the wrong thing quickly then why bother even checking the values? If you want to do the right thing, you need to understand the problem, where the numbers came from, how the returned value will be used and so on. You are looking for a black box answer which does not exist. – David Heffernan Mar 24 '16 at 07:41
  • Fastest is very much a programming problem. You can't ask a CS about how to write the most efficient program, because you have to actually know something about computers to answer that. Been long since I encountered a CS who knew much about computers. – Lundin Mar 24 '16 at 07:41
  • @Martin It's not a maths question. It could be a programming question but not in the current form. – David Heffernan Mar 24 '16 at 07:42
  • I'd modify 4-th line this way: `fabs(x - y) < n * FLT_EPSILON * sqrt(x * x + y * y + FLT_EPSILON * FLT_EPSILON)` (to make this line more like previous ones and to avoid division). – Ilya Mar 24 '16 at 07:44
  • @Ilya Edited. It looks better now. – nalzok Mar 24 '16 at 07:48
  • @sunqingyao it seems that Marievi rolled back this change. Any way, it is not critical. – Ilya Mar 24 '16 at 09:55

1 Answers1

2

The fastest and most accurate is x == y. I use it a lot in situations where I know that the calculations are exact. In some other cases, I round one of the numbers, as required by the particular application, and then I do the exact comparison.

Up to now, I didn’t need to use one of the more complicated expressions from your question. But then again, maybe I just didn’t do “real computations”.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • 1
    Question asks which of 4 methods is fastest. You refer to the method not found in the question. Further, it is impossible to say that this method is most accurate without first defining the problem and what you mean by accurate. I can think of any definitions where exact equality would be inaccurate. – David Heffernan Mar 24 '16 at 08:08
  • @David: the most accurate *comparison* is indeed `x == y`. It may not be the most suitable solution, when combined with other code, though. But since it does not do anything else, it is the fastest too. – Rudy Velthuis Mar 24 '16 at 10:36
  • @RudyVelthuis It depends entirely on what you mean by accurate. If the goal is to allow for imprecision in calculation, then `==` is not accurate to that standard. Furthermore, where in the question is there any mention of using `==`? Did I miss that part? Have you read the question carefully? – David Heffernan Mar 24 '16 at 10:38
  • The comparison as such can not be more accurate. But the data you supply may be inaccurate, and that is why you use an epsilon. – Rudy Velthuis Mar 24 '16 at 10:56
  • @RudyVelthuis As I thought, you have not read the question carefully. – David Heffernan Mar 24 '16 at 11:00
  • I have. I know that that is not what was being asked. That is why I didn't post an answer, just a comment. – Rudy Velthuis Mar 24 '16 at 11:45