# LSL & Math

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

Hello all!

I needing some help on a script, I know it has to be pretty simple but I'm just not seeing it my self. I'm trying to return a positive when subtracting from a given number even when its returns a negative number. I just can't rap my head around lsl and math.

I was able to get it to this point

```integer subtract(integer x, integer y)
{
if( x < y ) return 0;
else return x - y;
}

default
{
state_entry()
{
llOwnerSay((string)subtract(10, 11));
}
}```

but the system I working on works on positive returns and returning 0 throws off my system when I just want to remove what the system has. IE Object A is asking to remove 11 from Object B, but Object B only has 10 Units, So Object B would send back it was only able to remove 10 Units so it would tell Object A 10 was removed.

##### Share on other sites

Posted (edited)

llAbs will give you the absolute (positive) value of a given integer.

llFabs will do the same for floats.

Edited by Wulfie Reanimator
##### Share on other sites

3 minutes ago, Wulfie Reanimator said:

llAbs will give you the absolute (positive) value of a given integer.

llAbs would work just fine, but on the math end of lsl I just don't know how I would be able to tell hey I can only give you 10 of the 11 your asking for.

##### Share on other sites

Thanks Wulfie seems to be working so far with this code. But if I run into trouble I will ask again on it

```integer subtract(integer x, integer y)
{
integer a;
if(x < y)
{
integer z = ShowAbsolute(x - y);
a = y - z;
return a;
}
else
{
return x - y;
}
}

integer ShowAbsolute(integer inputInteger)
{
integer A = llAbs(inputInteger);
return A;
}

default
{
state_entry()
{
llOwnerSay((string)subtract(10, 11));
}
}```

##### Share on other sites

7 hours ago, Casey Conundrum said:

Object A is asking to remove 11 from Object B, but Object B only has 10 Units, So Object B would send back it was only able to remove 10 Units so it would tell Object A 10 was removed.

So within your function, does this mean x is what object B actually has and y is what it's being told to remove? If so, then just return x instead of zero when x is less than y.

• 1
##### Share on other sites

Oh, you want it to work like a bank account, where you can't withdraw more if your balance is zero. I think.

```//  Minimum value of two integers
integer min(integer x, integer y)
{   if (x < y)  { return x; } else { return y; }
}

//  Withdraw from balance
integer gBalance = 100;

//  Withdraw from balance
integer withdraw(integer n) {
integer withdrawal = min(n, gBalance);  // limit of what you can withdraw
gBalance -= withdrawal; // will not go negative
llOwnerSay("Tried to withdraw " + (string)n + " received " + (string) withdrawal);
return withdrawal;
}

default
{
touch_start(integer total_number)
{
llSay(0, "Touched.");
withdraw(25);
withdraw(50);
withdraw(50);   // overdrawn
}
}```

This should work.