Gubbi
Gubbi

Reputation: 21

How would i get the value between two integers in LUA

Hi everyone i am trying compare integers by getting the amount between them

Lets say that i for example have a base integer local i = 100 Then i have other integers that are for example 200 and 300. I want to get the amount between i and the other to values two see which one is closest to the base integer.

Upvotes: 1

Views: 2645

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

To get the 'distance' between two integers, you can just compute the absolute difference:

local i = 100
local x = 200
print(math.abs(i - x))
print(math.abs(x - i))

The math.abs function gets rid of any negative numbers resulting from the subtraction.

Upvotes: 3

Related Questions