Calculation Issues

Okay, so I have a script function for a Common Event.

One line is this:

tpreg = (100 * (1 - hp / mhp) / 2).to_f

HP and MHP are defined earlier in the script and work perfectly well, and are the current HP and max HP of the user respectively.

With a current HP of 516 and max HP of 615, tpreg is returning 50.0 after calcs; this makes no sense at all; it's as if the calc is ignoring everything between 100 and /

Can anyone help me out with this?
tpreg = (100 * (1 - hp / mhp.to_f) / 2.0)

Edit: Sorry no time to explain right now.
tpreg = (100 * (1 - hp / mhp) / 2).to_f

hp = 516
mhp = 615

tpreg = (100 * (1 - 516/615) / 2).to_f
tpreg = (100 * (1 - 0) / 2).to_f
tpreg = (100 * 1 / 2).to_f
tpreg = (100 / 2).to_f
tpreg = 50.to_f
tpreg = 50

The .to_f has to be at the point where you first have a fractional number. By the time it gets to yours, it's long since been converted to an integer.

Either your hp or mhp have to be converted .to_f in order for the result of that division to have a fractional component. If you're using a script, you could use hp_rate which does that calculation for you and will return a decimal number.


Try this:

tpreg = 100 * (1 - hp.to_f / mhp)
Thanks guys!

Original thread: https://forums.rpgmakerweb.com/threads/calculation-issues.93610/