Home / Forum / Discussion and Help for Gmod-Glua-Lua / Change the negative number into a positive number ✔️ Solved

Change the negative number into a positive number

Thread
profile avatar Jenny Fer
⚗️ Support
📖 Posts: 31
↩️ Replies: 42
❤️ Reactions: 18
Jenny Fer Posted 1575183233 (Edited) (13788 views)
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.
⛔️ Sorry, you are not connected, join to leave a reaction

Replies
profile avatar Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 18
❤️ Reactions: 1
Orion Posted 1575184330
hmm ill try out some stuff, ill reply or message you on discord if i figure it out 🙃
profile avatar Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 18
❤️ Reactions: 1
Orion Posted 1575184546
testvar = -255

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

print( testvar )

This is very primitive, it works tho!
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts 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.

Gmod console result stress test
profile avatar Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 18
❤️ Reactions: 1
Orion Posted 1575185642
perfection david
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts Posted 1575185662
Tested with n - n - n it work but too heavy for GMod.
Gmod console stress test
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts Posted 1575185739
Thank you Orion. 🙂
profile avatar Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 18
❤️ Reactions: 1
Orion Posted 1575186100
🙃
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts 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
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts Posted 1575186741
Question why?
profile avatar Jenny Fer
⚗️ Support
📖 Posts: 31
↩️ Replies: 42
❤️ Reactions: 18
Jenny Fer Posted 1575187332
Oh I'm...!! Thank!!!

Because I updated my scripts to create an alternative for math.mod will be deleted from gmod and also math.Dist

I wouldn't want my scripts to be broken by an update of GMod. I reduce this risk by doing this. If I replace math.abs by a simple mathematical solution and not too heavy.

I'm going to use (n * -1) it's quick for my needs.
profile avatar Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 18
❤️ Reactions: 1
Orion Posted 1575187455
oh ok! 🙃
profile avatar Norda Scripts
🎨 Creator
📖 Posts: 162
↩️ Replies: 76
❤️ Reactions: 25
Norda Scripts Posted 1575188049
I understand. I was thinking about something else.

Then. I tested again the solution n * -1, Is the best alternative among those proposed : 0.004s is good. 🙂

Console Gmod Test
profile avatar Jenny Fer
⚗️ Support
📖 Posts: 31
↩️ Replies: 42
❤️ Reactions: 18
Jenny Fer Posted 1575220399
Thank you. I learned that a subtraction is heavier than a multiplication. This is important for future choices. 🙄


Sign in or register to reply