Home / Forum / Fr 🇫🇷 | En 🇬🇧 / Discussion and Help for Gmod-Glua-Lua / Change the negative number into a positive number ✔️ Solved

🙄 Change the negative number into a positive number

img
⚗️ Support
📖 Posts: 22
↩️ Replies: 20
❤️ Reactions: 13
Emma Lovegood 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.
⛔️ Sorry, you are not connected, join to leave a reaction
1 2

Replies
Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 17
❤️ Reactions: 1
Orion Posted 1575184330
hmm ill try out some stuff, ill reply or message you on discord if i figure it out 🙃
Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 17
❤️ 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!
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) 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
🎁 Buyer
📖 Posts: 4
↩️ Replies: 17
❤️ Reactions: 1
Orion Posted 1575185642
perfection david
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) Posted 1575185662
Tested with n - n - n it work but too heavy for GMod.
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) Posted 1575185739
Thank you Orion. 🙂
Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 17
❤️ Reactions: 1
Orion Posted 1575186100
🙃
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) 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)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) Posted 1575186741
Question why?
Emma Lovegood
⚗️ Support
📖 Posts: 22
↩️ Replies: 20
❤️ Reactions: 13
Emma Lovegood Posted 1575187332
Oh I'm...!! Thank!!!

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

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.
Orion
🎁 Buyer
📖 Posts: 4
↩️ Replies: 17
❤️ Reactions: 1
Orion Posted 1575187455
oh ok! 🙃
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) 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. 🙂

Emma Lovegood
⚗️ Support
📖 Posts: 22
↩️ Replies: 20
❤️ Reactions: 13
Emma Lovegood Posted 1575220399
Thank you. I learned that a subtraction is heavier than a multiplication. This is important for future choices. 🙄
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) Posted 1582095666
123
David (NordaHL)
🎨 Creator
📖 Posts: 115
↩️ Replies: 51
❤️ Reactions: 17
David (NordaHL) Posted 1582095669
456
1 2


Sign in or register to reply