[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Negative number positive



Farai Aschwanden wrote:
What I ment with "-x" was " a negative number" while x is a number. Using ABS() on a positive number is nonesense... Thats why I added the "-". Probably mathematically not correct written. Sorry for this.
It's not nonsense.
You use abs on positive numbers so that you don't have to determine if the number is negative or positive.
consider this function:


def print_magnitude(num):
   print "The magnitude of your number is %s" % abs(num)

and using it:
x = 5
print_magnitude(x)
x = -5
print_magnitude(x)

In both cases the function print_magnitude doesn't care if the number is positive or negative.

Doing something like this:

if x < 0:
   x = abs(x)
print "the magnitude of the number is "+x

is nonsense, however.

I know it's stupid to argue about this, but generally when people put variables in code, it's assumed that the variable can have any value.
if it's supposed to have specific values, you'd use notation like this:


for i in range(x,50):  #x > 0

not
for i in range(+x,50):
to denote that it's a positive variable.
So why use that notation for negative variables?

in general,
print "%i is an integer." % x
assumes that x is either negative or positive.


The whole point of the abs function is that it works whether the number is negative or positive.
It's not meant to convert between negative numbers and positive numbers...
it's meant to convert _any_ number to a positive number.


Anyway, I know you know how to use the abs() function,
I was just trying to point out that you don't have to call abs(-variable_name) in order to use it,
and that it works for both positive and negative variables.


-Luke