Code: Select all

```
function sign(x)
if x < 0 then
return -1
end
return 1
end
```

- tentus
- Inner party member
**Posts:**1060**Joined:**Sun Oct 31, 2010 7:56 pm**Location:**Appalachia-
**Contact:**

You know how math.abs returns a variable, sans its sign? Does anyone there a neat trick for doing the opposite, short of using the below:
(And before anyone suggests it, we do not want 0.)

Code: Select all

```
function sign(x)
if x < 0 then
return -1
end
return 1
end
```

Kurosuke needs beta testers

- bartbes
- Sex machine
**Posts:**4946**Joined:**Fri Aug 29, 2008 10:35 am**Location:**The Netherlands-
**Contact:**

I only know the easy (yet probably inefficient) x/math.abs(x). (That has 0, though..)

Of course I don't know your gripe with the if, but you could shorten it to*x < 0 and -1 or 1*.

Of course I don't know your gripe with the if, but you could shorten it to

You can define it as a math function, like this:
This counts 0 as positive. If you want to identify zeros, do this:

Code: Select all

```
function math.sign(x)
return (x >= 0) and 1 or -1
end
```

Code: Select all

```
function math.sign(x)
return (x > 0) and 1 or (x < 0) and -1 or 0
end
```

If you want to test the sign, you can just use math.abs(var) == var to test if it's positive

Example:

Example:

Code: Select all

```
var = 1
negvar = -1
if math.abs(var) == var then true end
if math.abs(negvar) == negvar then false end
if math.abs(negvar) == -negvar then true end
```

Code: Select all

```
if your.timeSpeed > 0 then universe:update(dt) else universe:destroy() end
```

Users browsing this forum: No registered users and 14 guests