ID:272651
Nov 18 2008, 7:54 pm
|
|
Could someone please post an example on how to make the Bar in interface update. like if the user has 1/2 life its half filled. thank you.
|
In response to Haywire
|
|
Percentages are easy. Just take the part times 100 divided by the whole. If your max HP is 125, and your current HP is 50, then your percentage of total HP remaining is 50*100/125 = 40, so you have 40% of your total HP left.
|
In response to Xooxer
|
|
Xooxer wrote:
Percentages are easy. Just take the part times 100 divided by the whole. If your max HP is 125, and your current HP is 50, then your percentage of total HP remaining is 50*100/125 = 40, so you have 40% of your total HP left. Or, alternatively, you could simply do the part divided by the whole times 100; (a/b)*100 = p (50/125)*100 = 40% Percentages aren't difficult. |
In response to Slipknight
|
|
using order of opperations makes this exactly what xoover said...
|
In response to Pirion
|
|
Yeah, but it's slightly more logically correct.
|
In response to Kaioken
|
|
From a programming standpoint, Xooxer's formula is the better one. It's better to multiply by 100 first before doing the division, because of rounding error. Multiplying by 100 adds only four bits to the mantissa of the floating point number, which won't ever result in rounding error except in cases where the number had a long mantissa to begin with (like you might see in a very long number, or an irrational number). Dividing however can introduce all kinds of rounding error, because in most cases the denominator won't be a simple power of 2. To whatever extent it can, the division will end up canceling out non-binary factors in the numerator, which includes the 25 that got multiplied in when you multipled by 100.
The difference is negligible, especially when it's being applied to a control where it's just going to be converted into whole pixels anyway, but in anything math-intensive it's crucial to be aware of these things. Lummox JR |
Haywire