Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Getting Percent of a number.

Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
03-26-2008 09:59
What kind of line would I need to get a percent of a number.
something like if variable A is greater then 50% of variable number

integer B = 100;

integer A = 65;

if A > 50% of B
{

}

Is there an easy operator that can do this for me?

Thanks
-Lady Cherry
Sho Iuga
Registered User
Join date: 6 Jun 2007
Posts: 35
03-26-2008 10:14
I dont know if there is a built in operator for that, but it is easy to code a function, that tests if a is at least test% of b. The important thing is to typecast the integers to floats before dividing them:

CODE

percent(integer a, integer b, float test)
//function returns true, if a is at lest test% of b
//for example percent(6, 10, 59.55) would return true
//percent(6, 10, 60.5) would return false
{
float p = (float)a/(float)b;
if( (p * 100.0) >= test ) return TRUE;
else return FALSE;
}


And your line
if A>=50% of B ....
would then read
if( percent(A,B,50.0) ) ...
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
03-26-2008 10:18
If A > 50% of B

is

if A > (0.5 * B)

Percentages are just values out of 100. 50 / 100 = 0.5.
_____________________
Sho Iuga
Registered User
Join date: 6 Jun 2007
Posts: 35
03-26-2008 10:51
if A > (0.5 * B) will work, because B is implicitly typecasted to a float.

But 50/100 is 0, because it is an integer division.

If you want 50/100 to be 0.5 you need to typecast them to floats:
(float)50 / (float)100 is 0.5, as is 50.0/100.0.
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
03-26-2008 13:09
From: Sho Iuga
if A > (0.5 * B) will work, because B is implicitly typecasted to a float.

But 50/100 is 0, because it is an integer division.

If you want 50/100 to be 0.5 you need to typecast them to floats:
(float)50 / (float)100 is 0.5, as is 50.0/100.0.

I was talking about in math, not in LSL code. Percentages to decimal, just shift the decimal over two to the left. 50 percent becomes 0.50.
_____________________
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
03-26-2008 16:38
Of course, if you really have integers, and the only percentage you care about is 50%, and speed is important, then
CODE

if (2*A > B) {....}

will work fine. It could trigger overflow unnecessarily, but depending on the problem, that may not matter. This avoids the conversions to floating point, and hence is much faster.

Aside: Bit shifting might be even faster, but less clear. Dividing by two might be even clearer, but division is generally slower than multiplication. Of course, a good peephole optimizer might convert multiplication or division by constants that are powers of two into bit shifts :)
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
03-26-2008 17:04
Yes, clearly we should do if (a > (b >> 1)). I think the speed increases from all these suggestions are virtually meaningless.

I would use if (a > 0.5 * b) simply because it's actually correct, and won't cause overflow. I seriously doubt that people doing basic math should be concerned with optimizations if the purpose doesn't require ULTIMATE SPEED.
_____________________
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
03-26-2008 20:15
From: Tyken Hightower
Yes, clearly we should do if (a > (b >> 1)). I think the speed increases from all these suggestions are virtually meaningless.

I would use if (a > 0.5 * b) simply because it's actually correct, and won't cause overflow. I seriously doubt that people doing basic math should be concerned with optimizations if the purpose doesn't require ULTIMATE SPEED.

Normally I'd agree with you about not being concerned with optimizations. And, if it wasn't obvious, I wasn't suggesting using bit shifting, but merely acknowledging that it existing.

But in this case "if (2 * A > b)" is equivalent to "if (A > 0.5 * b)", both in number of operations and clarity of meaning. It's an artifact of computer math that one is faster. Conceivably, there's something about the underlying things being modeled by A and B that would make one meaning more appropriate than the other, but with the information we have, they're conceptually equivalent.

To put it another way, there's no reason not to use the more efficient idiom when there's no difference in clarity.
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
03-26-2008 23:58
From: Kidd Krasner

To put it another way, there's no reason not to use the more efficient idiom when there's no difference in clarity.

Except if, like mentioned before, it can cause overflow.
_____________________
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
03-27-2008 07:12
well I know I will need more then 50% etc. Basically I am doing a small hud in which at different percents of the number the bar would move down. (Like a hit point bar)

so if your max HP = 22

and your at HPCurrent = 22

then the bar is all the way up because its at 100% etc

but at different percentages the bar will move down etc (Or scale)

so I needed something that would tell what percent of the number it was.

Basically anything simple will work. I will have to try some of what was suggested when I have time to open my LSL editor.

Thanks
-Cherry
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
03-27-2008 10:23
CODE

// returns the percent of total that fraction represents (e.g. fraction=1.0, total=3.0 results in 33.333...)
float percent(float fraction, float total)
{
return 100.0*fraction/total;
}