How would I get the non-negative opposite of a number?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want numbers to be a opposite, so basically if I want 1 to be opposite it would turn into 0, 0.9 would turn into 0.1, and 0.85 would turn into 0.15.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do this without making a system that would take up a lot of lines of code.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, I have looked for solutions, but all I can find is negative numbers such as 1: -1, 0.1 = -0.1 and so on.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I have tried this, but it’s not as effective.

function convert(num)
	if num == 1 then
		return 0
	elseif num == .9 then
		return 0.1
	elseif num == .8 then
		return 0.2
	elseif num == .7 then
		return 0.3
	elseif num == .6 then
		return 0.4
	elseif num == .5 then
		return 0.5
	elseif num == .4 then
		return 0.6
	elseif num == .3 then
		return 0.7
	elseif num == .2 then
		return 0.8
	elseif num == .1 then
		return 0.9
	elseif num == 0 then
		return 1
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I believe you can do

function convert(num)
    return math.abs(1 - num)
end

Let me know if this is what you’re looking for, math.abs() returns the absolute value.

2 Likes

The opposite of N would be 1-N

1 Like
function convert(num)
     local x = 1 - num
     return x
     print(x) -- testing purposes
end

If this isn’t what your looking for, pls reply or message me for help.

2 Likes

That has a error, thanks though!

Oh, I don’t know how I didn’t think of that… Thanks!

Have you tried using -1+1 -1+0.9 = .1 and doing a if value <0 then val=val*-1 end ?

Works perfectly, thank you very much!

Well no, I haven’t tried that, although that seems like it wouldn’t work very well. I tried -1+0.9 and it gave me a negative .1, same with -1+1-1+0.9

Alternatively you can also do this, and obviously you’re still able to put it into a function. All you have to do is replace (1) with the number.

math.abs(1)*-1

That just gives the negative of a number

well that’s because they returned something and then tried to print after that
the print would never run at all

1 Like

I know that, but I was pointing the error out.