Help Me With My Idea

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

a function that returns true if number is similar to another

  1. What is the issue? Include screenshots / videos if possible!

I do not know how to do it

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

yes

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


print(IsSimilar(1,2)) -- true

print(IsSimilar(1,12)) -- false

print(IsSimilar(100,125)) -- true

or


print(IsSimilar(1,2)) -- 1

print(IsSimilar(1,12)) -- false,11

print(IsSimilar(100,125)) -- 25

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

If I understand, you answer on your question. Just pick up your max number by math.max(number, number) then reduction it and you have difference. Last is just how big difference you want to achieve

1 Like
function isSimilar(n1,n2)
	if math.max(n1,n2) - math.min(n1,n2) <= 5 then -- Maximum Number between the Two - The Minimum Number, if it is less than or equal to 5 it returns true.
		return true
	else
		return false
	end
end

print(isSimilar(21,25)) -- Will return true
print(isSimilar(25,30)) -- Will also return true
print(isSimilar(500,1000)) -- Will return false
print(isSimilar(21,31)) -- Will also return false

You can calculate the similarity using the percentage difference formula.

There will be a percentage error % based on how dissimilar the two numbers are. The threshold you decide if a number is similar or not.

It is simpler to just use math.max - math.min like I provided above. Typically, similar numbers are within 5 or whatever threshold you can set.

Your code will never provide a set of solutions where 1 and 12 are not similar, but 100 and 125 are.

Well, it’s an alternative though this is one weird question there is no context for what it’s going to be used for so it’s hard to judge the best solution.

Here is alternative which should be the same as your method which I believe is simpler though using absolute difference.

--similarity threshold is 5 units apart of each other.
function isSimilar(n1,n2,threshold)
threshold = threshold or 5
	if math.abs(n1-n2) <= threshold then -- Absolute difference
		return true
	else
		return false
	end
end

As I stated, he can change the threshold, it is essentially pointless to use a percentage method as you can just change the threshold to suit your needs.

12 - 1 = 11. 125 - 100 = 25.There is no threshold where 11 is not similar, but 25 is.