How to Make the Value of a Variable Negative in PlayBasic
Making the value of a variable negative involves two steps :
- Check if the variable is positive.
- Change the number from a positive number to a negative number.
There's two ways to check whether the variable is positive, with the first being slightly better. The number is actually converted into a negative number by Neg()
Making a Variable Negative with Abs( )
Abs() converts a negative number into a positive number. If the number is already positive, it does nothing. This allows you to check if the variable is positive by comparing the variable itself with the result of the variable when Abs() is used on it.
If someVar = Abs(someVar) Then someVar = Neg(someVar)
Complete Code
ForceDeclaration
Declaration
someVar
EndDeclaration
someVar = 10
If someVar = Abs(someVar) Then someVar = Neg(someVar)
Print someVar
WaitKey
Making a Variable Negative with GetSign( )
If a number is positive, GetSign() returns a 0. Therefore, you can check it to see if the number is positive.
If GetSign(someVar) = 0 Then somevar = Neg(someVar)
If you are a BASIC programmer and find this strange, yes, you're right. In most BASIC dialects, Sgn() returns 1 for a positive number. Not PlayBasic. It returns a 0 for both 0 and positive numbers.
Complete Code
Declaration
someVar
EndDeclaration
someVar = 42
If GetSign(someVar) = 0 Then somevar = Neg(someVar)
Print someVar
Sync
WaitKey