This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Change negative to positive (number)
Started by koslas, 03 November 2012 - 01:31 AMPosted 03 November 2012 - 02:31 AM
How do I change a number from -10 to 10?
Posted 03 November 2012 - 02:38 AM
What is the question?
I didn't get it
I didn't get it
Posted 03 November 2012 - 02:39 AM
What do you mean?
Posted 03 November 2012 - 02:41 AM
local C = -10
local D = 0 - C
print(D)
This converts a negative to positive or the other way around.Posted 03 November 2012 - 03:09 AM
Or
Just times the number by -1…
local n = -10
print(-1*n)
Just times the number by -1…
Posted 03 November 2012 - 03:30 AM
math.abs() will make any number positive.
Posted 03 November 2012 - 05:38 AM
math.abs() will make any number positive.
Ahh yes, returns the absolute value of a number.. forgot about that but making it * -1 is easier xD
Posted 03 November 2012 - 04:02 PM
Yes, but the question is whether you want an absolute value, an inversion, or just to reassign the value of 10. And I personally can't really say which is being requested here.
Posted 03 November 2012 - 05:26 PM
Really, the best way of doing this is:
Now, if you are saying that you want this to ONLY change negative to positive, this might be good:
somevar = 65
somevar = somevar*-1
Now, if you are saying that you want this to ONLY change negative to positive, this might be good:
somevar = 65
if somevar < 0 then
somevar = somevar*-1
end
Posted 03 November 2012 - 07:24 PM
Now, if you are saying that you want this to ONLY change negative to positive, this might be good:somevar = 65 if somevar < 0 then somevar = somevar*-1 end
No. The best way to only change negative to positive is math.abs(). That's what it's there for, that's the whole point of it existing.