Home / Forum / Gmod-Glua-Lua / Change the negative number into a positive number โœ”๏ธ Solved

๐Ÿ™„ Change the negative number into a positive number

Emma loves chocolate
img

Posted 1575183233 (Edited)
Hello I want to get a positive number with a negative number on Gmod. An idea how to do without using the math.abs() functions?
For example, a -255 when printed does not display the -255 but 255.
1 2

Replies
Orion
img
Posted 1575184330
hmm ill try out some stuff, ill reply or message you on discord if i figure it out ๐Ÿ™ƒ
Orion
img
Posted 1575184546
testvar = -255

if testvar < 0 then
testvar = testvar - testvar - testvar
end

print( testvar )

This is very primitive, it works tho!
David (NordaHL)
img
Posted 1575185106
Deleted
David (NordaHL)
img
Posted 1575185345
Hi there are plenty of solutions. The most optimized are this one
Multiply it by -1:
local n = -255

n = n * -1
print( n ) --255

If you use a negative number to subtract it from 0 it also turns it into a positive number :
local n = -255

n = 0 - n
print( n ) --255

I have just tested each method, some are less efficient than others and it is up to you to choose.
On Gmod math.abs() is faster.

Orion
img
Posted 1575185642
perfection david
David (NordaHL)
img
Posted 1575185662
Tested with n - n - n it work but too heavy for GMod.
David (NordaHL)
img
Posted 1575185739
Thank you Orion. ๐Ÿ™‚
Orion
img
Posted 1575186100
๐Ÿ™ƒ
David (NordaHL)
img
Posted 1575186128
It is a good idea Orion
if testvar < 0 then --if sometimes you have positive numbers so that you can avoid getting negative numbers.


end
David (NordaHL)
img
Posted 1575186741
Question why?
1 2


Sign in or register to reply