How to calculate percentages

Talk about making games.

How to calculate percentages

Postby Hblade » Tue Sep 01, 2009 4:18 am

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
Attachments
Percentages.zip
(33.16 KiB) Downloaded 85 times
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to calculate percentages

Postby cforall » Tue Sep 01, 2009 5:33 am

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~
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score

Re: How to calculate percentages

Postby Hblade » Tue Sep 01, 2009 5:42 am

Thanks, but yours is more effecient.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to calculate percentages

Postby cforall » Tue Sep 01, 2009 5:53 am

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~
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score

Re: How to calculate percentages

Postby Hblade » Tue Sep 01, 2009 2:34 pm

Hehe, only by a few characters xD, hey can you change the float to an int nd still let it work? o.o
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to calculate percentages

Postby cforall » Wed Sep 02, 2009 2:09 am

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 ?
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score

Re: How to calculate percentages

Postby Hblade » Wed Sep 02, 2009 3:00 am

Ahh hey clever, cforall, thanks :D This method seems.. much better :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to calculate percentages

Postby Hblade » Fri Sep 04, 2009 3:10 pm

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
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to calculate percentages

Postby cforall » Sat Sep 05, 2009 12:20 am

: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..
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest