 # Need help converting a formula into text

I have a problem with a piece of a formula I’m using for my problem. There are two parts, a and b, which a is a variable of b’s equation:

`````` const int a = (((3*HPmax-2*HPcurrent)*rate*bonusBall)/(3*HPmax))*bonusStatus;
const int b = 1048560/sqrt(sqrt(16711680/a));
``````

(The variables HPmax, HPcurrent, rate, bonusBall, HPmax, and bonusStatus have int values.)

Now equation a works just fine (having tested it on Mathway), but it seems that arduino isn’t reading equation b correctly. I tested equation a and b on Mathway and got correct results. I think arduino is reading equation b wrong, or there’s some other way to write that equation that I don’t know yet. If someone could help me format that equation to fit arduino, that’d be awesome.

P.S. I just started arduino literally 4-5 days ago and get confused often.

The whole formula setup:

``````					// Basing the formula on a lv.25 Pikachu
int HPmax = 58;     // HPmax is the number of hit points the Pokémon has at full health,
int HPcurrent = 14.5;	// HPcurrent is the number of hit points the Pokémon has at the moment,
int rate = 190;		// rate is the catch rate of the Pokémon,
int bonusBall = 1;  // bonusball is the multiplier for the Poké Ball used, and
int bonusStatus = 1;// bonusstatus is the multiplier for any status condition the Pokémon has (2 for sleep and freeze, 1.5 for paralyze, poison, or burn, and 1 otherwise).
const int a = (((3*HPmax-2*HPcurrent)*rate*bonusBall)/(3*HPmax))*bonusStatus; // Modified catch rate
const int b = 1048560/sqrt(sqrt(16711680/a)); 								  // Shake probability
long randNumber;
``````

And yes, this is the formula for the capture probability of a pokeball.

Moderator edit: Stupid irrelevant colour tags removed, code tags substituted.

I am not going to bother to figure this out because of the poor choice in colors. I cannot read light yellow on light grey.

But you are having issues with int arithmetic.

The yellow are notes unimportant to the question but I guess I can change it.

Try it with long ints.

Seemed to fix my problem, thanks man. :D

I could be wrong, but how well does this work:

``````int HPcurrent = 14.5;      // What?

// more stuff...

const int a = (((3*HPmax-2*HPcurrent)*rate*bonusBall)/(3*HPmax))*bonusStatus; // Modified catch rate
const int b = 1048560/sqrt(sqrt(16711680/a));                                 // Shake probability
``````

Since probabilities are often expressed as fractions, why are you assigning the result into an int? Also, the numeric constants above, if you want a floating point answer, need to be expressed as floating point constants (e.g., 3.0) for the compiler to override the default data type of int.

int means integer, integers are whole numbers. Nothing involving sqrt is going to come out right unless you get really lucky.

With numbers that big it is not too bad.

Assuming a is 1, the sqrt (16711680) is 4087.9921722. Rounding to 4088 and squaring is 16703569, an error of much less than a percent.

Even truncating to 4087 yields an error of less than a percent.