is there a simple way to check if 2 floats are approximately equal?


is there a simple way to check if 2 floats are approximately equal?



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





Define "approximately equal". (How "approximate" do you mean?)
– duskwuff
Jun 30 at 2:51






“Close to but not exactly” say 3 decimal.
– Justin808
Jun 30 at 2:52





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 at 11:18





See also stackoverflow.com/questions/4518641/…
– lhf
Jun 30 at 13:15





What problem are you really trying to solve?
– lhf
Jun 30 at 13:22




2 Answers
2



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



You also compare their decimal representations:


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

print(decimal(x)==decimal(y))





This reports some numbers that are very close together as not, such as 1.23499999… and 1.23500010…
– Eric Postpischil
Jun 30 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 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 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 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 at 13:21






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV