Jeckel
Great Reverend
Ok guys, math is not my strong suit. I can generally take an algorithm and cut/paste it to where I need, or wrap code I understand around it, but ask me to figure out how (or worse why) the math works and I'm not going to give you anything.
What I got is pretty simple on the math side. I have a function that takes two number arguments and returns what percentage the first arg (oValue) is of the second arg (oWhole).
If you don't know templates, then ignore the 'template <typename T>' line and replace the 'T' with 'int'.
The math is real simple. I don't recall where I got the code, might have been Civ4, but it has worked fine for everything I have thrown at it so far.
The following is some test output showing the result of various values.
I think those are all the right outputs, someone correct me if it isn't and that can be addressed also... heck, if it is right, would be nice if some one could give me that confirmation..
The problem I am having is when I use double (float) values, the output is not what I desire.
I know why it gives this output, 0.01 * 1 = 0.01 and then 0.01 / 100 = 0.0001, but I couldn't explain why a number times a number divided by a hundred would ever give the desired percentage, much less what conditions I should check for to stop it from giving the improper output when it does.
I'm sure the answer is simple, but if this was a question on Are You Smarter Then A Fifth Grader, I would have to drop out of school. Some help would be appreciated.
What I got is pretty simple on the math side. I have a function that takes two number arguments and returns what percentage the first arg (oValue) is of the second arg (oWhole).
Code:
template <typename T>
T percent(T oValue, T oWhole)
{
return (oValue * oWhole) / T(100);
}
If you don't know templates, then ignore the 'template <typename T>' line and replace the 'T' with 'int'.
The math is real simple. I don't recall where I got the code, might have been Civ4, but it has worked fine for everything I have thrown at it so far.
The following is some test output showing the result of various values.
Code:
percent(1, 100): 1
percent(50, 100): 50
percent(100, 100): 100
percent(-1, 100): -1
percent(-50, 100): -50
percent(-100, 100): -100
percent(1, -100): -1
percent(50, -100): -50
percent(100, -100): -100
percent(-1, -100): 1
percent(-50, -100): 50
percent(-100, -100): 100
I think those are all the right outputs, someone correct me if it isn't and that can be addressed also... heck, if it is right, would be nice if some one could give me that confirmation..
The problem I am having is when I use double (float) values, the output is not what I desire.
Code:
percent(0.01, 1.00): 0.0001
percent(0.50, 1.00): 0.0050
percent(1.00, 1.00): 0.0100
I know why it gives this output, 0.01 * 1 = 0.01 and then 0.01 / 100 = 0.0001, but I couldn't explain why a number times a number divided by a hundred would ever give the desired percentage, much less what conditions I should check for to stop it from giving the improper output when it does.
I'm sure the answer is simple, but if this was a question on Are You Smarter Then A Fifth Grader, I would have to drop out of school. Some help would be appreciated.