2

In LUA, is there a way to check if it floats are approximately equal?

Justin808
  • 20,859
  • 46
  • 160
  • 265
  • 2
    Define "approximately equal". (How "approximate" do you mean?) –  Jun 30 '18 at 02:51
  • “Close to but not exactly” say 3 decimal. – Justin808 Jun 30 '18 at 02:52
  • 1
    Attempting to test whether two floating-point numbers are close to each other is often a sign of a flawed algorithm design. Why do you want to do this? – Eric Postpischil Jun 30 '18 at 11:18
  • See also https://stackoverflow.com/questions/4518641/how-to-round-a-floating-point-number-up-to-certain-decimal-place – lhf Jun 30 '18 at 13:15
  • What problem are you really trying to solve? – lhf Jun 30 '18 at 13:22
  • @lhf the question you referenced is totally different from this question. The problem he tries to solve is not your business – chmike Apr 11 '21 at 07:29
  • @chmike, I was wondering whether that was an instance of the [XY problem](https://en.wikipedia.org/wiki/XY_problem) – lhf Apr 11 '21 at 10:35

2 Answers2

4

Just set a threshold value. If the difference between two values is less than the threshold, consider them equal:

a = 1.23456789
b = 1.23456777

threshold = 0.000001

diff = math.abs(a - b) -- Absolute value of difference
print(diff < threshold) -- True if difference is less than threshold

Output:

true
brianolive
  • 1,573
  • 2
  • 9
  • 19
0

You also compare their decimal representations:

function decimal(x)
  return string.format("%.3f",x)
end

print(decimal(x)==decimal(y))
lhf
  • 70,581
  • 9
  • 108
  • 149
  • This reports some numbers that are very close together as not, such as 1.23499999… and 1.23500010… – Eric Postpischil Jun 30 '18 at 11:16
  • Sorry, I stopped one digit too soon. Try 1.234499999… and 1.2345000010… The point is that rounding partitions the domain into segments, where each segment rounds to a particular numeral. Wherever there is a transition from one segment to another, points on either side of the transition are close to each other but round to different numerals. – Eric Postpischil Jun 30 '18 at 12:49
  • @EricPostpischil, you're right of course. The whole business of equality to a certain number of decimal places is not as straightforward as it seems. Nevertheless, my code does give the right answer for what the OP wants: up to 3 decimals. – lhf Jun 30 '18 at 13:14
  • Expressing the closeness of numbers as a number of decimal places does not generally mean the person wants the two numbers to be the same when **rounded** to three decimal places. It usually means they want the number to differ by at most one unit in the third decimal place, that is, to be within .001 of each other. – Eric Postpischil Jun 30 '18 at 13:18
  • @EricPostpischil, like I said, not straightforward. That's for the OP to decide. And your comment to the question is right on. – lhf Jun 30 '18 at 13:21
  • This comparison does not give you ordering, i.e. if it's false you have no way of knowing whether `x` or `y` was greater. – Henri Menke Jul 02 '18 at 02:38