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

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

img
โš—๏ธ Support
๐Ÿ“– Posts: 18
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 9
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.

Replies
img
๐ŸŽ Buyer
๐Ÿ“– Posts: 4
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 0
Orion Posted 1575184330
hmm ill try out some stuff, ill reply or message you on discord if i figure it out ๐Ÿ™ƒ
img
๐ŸŽ Buyer
๐Ÿ“– Posts: 4
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 0
Orion Posted 1575184546
testvar = -255

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

print( testvar )

This is very primitive, it works tho!
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
David (NordaHL) Posted 1575185106
Deleted
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
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.

img
๐ŸŽ Buyer
๐Ÿ“– Posts: 4
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 0
Orion Posted 1575185642
perfection david
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
David (NordaHL) Posted 1575185662
Tested with n - n - n it work but too heavy for GMod.
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
David (NordaHL) Posted 1575185739
Thank you Orion. ๐Ÿ™‚
img
๐ŸŽ Buyer
๐Ÿ“– Posts: 4
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 0
Orion Posted 1575186100
๐Ÿ™ƒ
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
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
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
David (NordaHL) Posted 1575186741
Question why?
img
โš—๏ธ Support
๐Ÿ“– Posts: 18
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 9
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.
img
๐ŸŽ Buyer
๐Ÿ“– Posts: 4
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 0
Orion Posted 1575187455
oh ok! ๐Ÿ™ƒ
img
๐ŸŽจ Creator
๐Ÿ“– Posts: 22
โ†ฉ๏ธ Replies: 26
โค๏ธ Reactions: 9
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. ๐Ÿ™‚

img
โš—๏ธ Support
๐Ÿ“– Posts: 18
โ†ฉ๏ธ Replies: 15
โค๏ธ Reactions: 9
Emma Lovegood 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