This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
koslas's profile picture

Change negative to positive (number)

Started by koslas, 03 November 2012 - 01:31 AM
koslas #1
Posted 03 November 2012 - 02:31 AM
How do I change a number from -10 to 10?
jag #2
Posted 03 November 2012 - 02:38 AM
What is the question?
I didn't get it
koslas #3
Posted 03 November 2012 - 02:39 AM
What do you mean?
BigSHinyToys #4
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.
remiX #5
Posted 03 November 2012 - 03:09 AM
Or


local n = -10
print(-1*n)

Just times the number by -1…
Lyqyd #6
Posted 03 November 2012 - 03:30 AM
math.abs() will make any number positive.
remiX #7
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
ChunLing #8
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.
brett122798 #9
Posted 03 November 2012 - 05:26 PM
Really, the best way of doing this is:


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
Lyqyd #10
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.