Attack that gets weaker as MP goes down

So I've got a character's skill that uses MP and deals more damage the more MP the character has. I thought this would be simple, but the damage formula doesn't seem to work. I hope someone can tell me what I'm doing wrong.

Code:
((a.atk * (a.mp/a.mmp)) * 4) - b.def * 2

So if a character has 50 ATK and 50/50 MP, the formula, as *my* brain is reckoning it, should be like:
((50 * (50/50)) * 4)
or (50 * 1) * 4
So 50 * 4

If the character has 45 MP left, the formula should work like
((50 * (45/50)) * 4)
(50 * 0.9) * 4
So 45 * 4

If the character has 12 MP left, the formula should work like
((50 * (12/50)) * 4)
(50 * 0.24) * 4
So 12 * 4

But what's happening in battle is that this attack isn't doing any damage *at all.*

I'm really worried the answer is going to be something like "Ruby can't handle fractions and decimals."
I'm really worried the answer is going to be something like "Ruby can't handle fractions and decimals."
Partially, yes, but that is because if an integer is divided by integer, it won't be float/decimal. So if you do 1/2 it will be 0, not 0.5.
What you do is to convert either of the integers into a float by adding ".to_f"

For example:
a = 1
b = 2
"a/b.to_f" or "a.to_f/b" will work.

In your case, you should convert either of "a.mp" or "a.mmp" to a float.
Alternatively, just rewrite the formula as: ((a.atk * a.mp * 4)/a.mmp) - b.def*2

This is mathematically equivalent -- a * (b*c) is the same as (a*b) * c -- but the different evaluation order will prevent the rounding problem.

Using your example with 12 MP left: (50 * 12 * 4)/50 would evaluate as 2400/50 = 48.
Or, you could just use "mp_rate" instead of "mp/mmp", which is a built-in method of Game_Battler and does exactly what Theo explained. There is an hp version as well called "hp_rate" if you ever need it.

The obvious advantage of this is that it automatically returns the float value of that division, while taking less characters in your formula box (VX Ace has a very limited formula box).
Spoiler: The final formula would look like this
Code:
a.atk * a.mp_rate * 4 - b.def * 2
It is even easier to read because there is no need for parenthesis (which allows you to save even more characters).
Or, you could just use "mp_rate" instead of "mp/mmp", which is a built-in method of Game_Battler and does exactly what Theo explained. There is an hp version as well called "hp_rate" if you ever need it.

The obvious advantage of this is that it automatically returns the float value of that division, while taking less characters in your formula box (VX Ace has a very limited formula box).
Spoiler: The final formula would look like this
Code:
a.atk * a.mp_rate * 4 - b.def * 2
It is even easier to read because there is no need for parenthesis (which allows you to save even more characters).

Wow, so it was that simple. Super thank you ^^

Original thread: https://forums.rpgmakerweb.com/threads/attack-that-gets-weaker-as-mp-goes-down.109978/