# Insure positive number?

Is there a way to turn a number into 0 if it’s negative, but leave it alone if it’s positive?

For example, if I have the following formula:
(4-(10/2))
The result would be -1. But say, due to some randomness, the result was instead +2? Is there something I can do so that if the result is -1, it changes to 0, but if it’s +2, it stays as +2?

So supposing the variable you’re working with is called `number` and it equals `-1` currently per your example:

``````*if (number < 0)
*set number 0
``````

That will only affect your variable value if it’s less than zero (i.e., negative).

If you’re finding you have to run that calculation a lot, you can actually put it at the bottom of the scene by using a gosub, like this:

``````*label negative
*if (number < 0)
*set number 0
*return
``````

Then you can run the calculation every time you need it by inserting a line that reads:

``````*gosub negative
``````

That will run the code between the `*label` and `*return`, automatically setting all negative numbers to zero for you.

5 Likes

Unfortunately I can’t name the variable, as far as I know. The variable is the result of a formula with parts that change.

Would it be possible for you to put your code here so to better illustrate the problem? What are you doing with the result of the formula?

Actually nevermind, I did get your solution to work. But now I have another issue.

My code is essentially as follows:

*rand x 1 3
*if ((x-round(((y-z)/2)))<0)
*set (x-round(((y-z)/2))) 0
*set w -(x-round(((y-z)/2)))

Sometimes it works. But if y is 1<x or greater, it doesn’t.

Apparently indents don’t show here, but that third line of the code is indented.

I’m wondering if perhaps you can’t subtract a negative, and that’s where I’m getting a problem.

You can use the preformatted text button in the options when posting to include the code in boxes like below:

``````*rand x 1 3

*if ((x-round(((y-z)/2)))<0)
*set (x-round(((y-z)/2))) 0

*set w -(x-round(((y-z)/2)))
``````

So one thing that is wrong here is that this line in particular (the one inside the *if):

`*set (x-round(((y-z)/2))) 0`

Has no variable being assigned the value of 0 (I’m assuming that’s hat you wanted to do?), so it wouldn’t really work; a variable must receive the value.

`*set variable what_to_receive`

And also what exactly that goes wrong? Is there an error message or just the calculations don’t amount to what you want?

I didn’t really understand what you were trying to do. I’m guessing you want to check if the equation is lower than 0, if so, set it to be 0. Then subtract w with the result of the equation? That’s what I got from it at least.

First, it’s better if we separate things for clarity. Let’s use result variable to receive your equation results first.

Then we compare if it’s less than 0. If so, set it to be 0.

Since subtracting something by 0 won’t matter, I added the *else.

If result is a positive number it will go through the *else and subtract w. Is this what you wanted to do? Or did you wanted w to receive the reverse of the result?

``````*temp result 0
*rand x 1 3

*set result (x-round(((y-z)/2)))

*if (result <= 0)
*set w 0
*else
*set w - result
``````