What is the Easiest Way to Get The Difference of Two Numbers?
UPDATE: Why isn't this working?
function GetDifference(Old, New)
if Old < New then
print(New-Old)
else
print(Old-New)
end
end
What is the Easiest Way to Get The Difference of Two Numbers?
UPDATE: Why isn't this working?
function GetDifference(Old, New)
if Old < New then
print(New-Old)
else
print(Old-New)
end
end
That would be subtraction -. The difference between two numbers is just the missing gap between them on a number line. Difference can be positive or negative.
Say you start with the value 5, and your new value is now 15. The difference between these two numbers is 10 (the same as their distance away from each other on a number line). You can fact check this by subtracting the old value, from the new value 15 - 5 which is indeed, ten.
In the case where your new value is less than your old value, you'll end up with a negative difference. This means you've gone down in value over time. If you have 5 dollars, and over the course of 1 week you now only have 1 dollar, your difference is -4 dollars (or 1 - 5 which is your new - old formula). This could also be known as your delta, which just means change or difference.
You can try using math.abs() to get the absolute value of the difference between your two numbers. That will avoid the negative numbers as it seems that is what you are attempting to do.
print(math.abs(Old - New))
or
print(math.abs(New - Old))
They should yield the same positive values which is the space between the two numbers.
I also need negative numbers...
If you needed the negative numbers as a result of the difference simply subtract New - Old. If New or Old are themselves negative numbers but you need the positive difference value between them this will still work.
It seems as if you're trying to obtain a positive difference because you're subtracting the lesser number from the greater one, otherwise you may just simply subtract the two values without the if statement.
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
I'm not sure what I'm doing wrong, the results aren't always correct. I updated my post to show what I currently have.