Page 1 of 1

How to calculate percentages

PostPosted: Tue Sep 01, 2009 4:18 am
by Hblade
I have a method on how to calculate the percentage of something, ok, say you have this method

HP: 0 / 800

Make a variable called detector, and perentage, now, in the detector, use this method
Code: Select all
detector = maxHP / 8

The value 800 was stroked down to 8, if it was 8000, it'd be 80, just remove 2 numbers from them


Now when you did that, make sure that the percentage is updated as well, using this code
Code: Select all
percentage = detector;



Now you have the percentage of your HP. When its 800, it'll be 100%, when 0, it'll be 0%, when 400, it'll be 50%, when its 75% it'll be 75%. Enjoy

Re: How to calculate percentages

PostPosted: Tue Sep 01, 2009 5:33 am
by cforall
Original code:

Code: Select all
if (percentage<100)
{
percentage = subobjectcount / 4;
}


If I want to calculate percentages, I will write code like this without a moment's hesitation:

Code: Select all
if (percentage<100)
{
percentage = floor(((float)subobjectcount / objectcount)*100);
}



Apparently, man you use "integer arithmetic" is better than I use "floating-point arithmetic".
Well done~

Re: How to calculate percentages

PostPosted: Tue Sep 01, 2009 5:42 am
by Hblade
Thanks, but yours is more effecient.

Re: How to calculate percentages

PostPosted: Tue Sep 01, 2009 5:53 am
by cforall
Hblade wrote:Thanks, but yours is more effecient.

Man
advantage of mine is no need to calculate that "4"~ I am lazy~ :mrgreen:
but yours is faster~

Re: How to calculate percentages

PostPosted: Tue Sep 01, 2009 2:34 pm
by Hblade
Hehe, only by a few characters xD, hey can you change the float to an int nd still let it work? o.o

Re: How to calculate percentages

PostPosted: Wed Sep 02, 2009 2:09 am
by cforall
Hey man
I google and find type conversion is not fast and this:

//VC7 console proj
Test std::vector<int>(1000):
(+:234) (-:266) (*:281) (/:1687) //computing time
(+:219) (-:234) (*:282) (/:1656)
(+:234) (-:235) (*:265) (/:1625)
(+:235) (-:218) (*:297) (/:1594)
Test std::vector<float>(1000):
(+:250) (-:266) (*:265) (/:2328)
(+:266) (-:250) (*:250) (/:2281)
(+:282) (-:250) (*:250) (/:2609)
(+:250) (-:250) (*:250) (/:2781)

...so I think its better to avoid division whether use int or float...
so if MaxHP is 400, write code like this:

Code: Select all
float objectcount = .25;  //use float
if (percentage<100)
{
percentage = subobjectcount*objectcount;
}


or set MaxHP 512 (the 9th power of 2) and then write code like this:

Code: Select all
int objectcount = 512;  //use int
if (percentage<100)
{
percentage = (subobjectcount*100)>>9;
}


I don't know how to change the float to an int nd still let it work, guy you mean this 3.14159 -> 314159 ?

Re: How to calculate percentages

PostPosted: Wed Sep 02, 2009 3:00 am
by Hblade
Ahh hey clever, cforall, thanks :D This method seems.. much better :D

Re: How to calculate percentages

PostPosted: Fri Sep 04, 2009 3:10 pm
by Hblade
Hey cforal, how the heck would I do this for say, an HP bar animation taht has like, 16 frames in it? for example, my animation bar has 16 frames, and my HP max is 800, and I want the animpos of the HP bar to be in % with the HP o.o

Re: How to calculate percentages

PostPosted: Sat Sep 05, 2009 12:20 am
by cforall
:D :D :D

All the time I think the animpos is read only... :mrgreen:
the bar has 16 frames...so you have to match (16-1) with 800 -> 800*(1/(16-1))=53.333333.
I suppose the 0 frame is EmptyHP and the 15 frame is MaxHP:

if(HP <= 0)
HPbar.animpos = 0;
else if(HP > 0 && HP < 2*53.333333)
HPbar.animpos = 1;
else if (HP >= 2*53.333333 && HP <= 800-53.333333)
HPbar.animpos = HP*(1/53.333333); //HPbar.animpos = 2, 3, 4, ......13, 14.
else if (HP > 800-53.333333 && HP < 800)
HPbar.animpos = 14;
else
HPbar.animpos = 15;

I dont test these code ...... man you should check it in your script editor..