# Mathematical Equation Not Yielding Expected Results

Page 1 of 1

## 8 Replies - 348 Views - Last Post: 26 September 2018 - 06:54 AM

### #1 Splashsky

• D.I.C Regular

Reputation: 12
• Posts: 334
• Joined: 25-August 13

# Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 05:06 PM

So I've got this beautiful little piece of math, where pow(x, y) is asserting x to the power of y. n in this formula can be substituted with any non-negative number between 1 and 100.

EDIT: for context, here's the original formula I was referencing

```6/5 * pow(n, 3) - 15 * pow(n, 2) + 100 * n - 140

```

The expected results are kind of as follows...
• n = 1 = 9
• n = 2 = 48
• n = 3 = 39
• n = 4 = 39

The results I got after plugging in the respective values are such.
• n = 1 = -54
• n = 2 = 10
• n = 3 = 57
• n = 4 = 97

What am I not doing right? How do I figure out where the error is?

This post has been edited by Splashsky: 25 September 2018 - 05:10 PM

Is This A Good Question/Topic? 0

## Replies To: Mathematical Equation Not Yielding Expected Results

### #2 Skydiver

• Code herder

Reputation: 6767
• Posts: 23,071
• Joined: 05-May 12

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 05:24 PM

Are you accounting for integer math in your 6/5 part of your code?

In other words, did you make sure to compute 6/5 as a floating point value. If not, then 6/5 == 1

### #3 Splashsky

• D.I.C Regular

Reputation: 12
• Posts: 334
• Joined: 25-August 13

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 05:26 PM

Skydiver, on 25 September 2018 - 05:24 PM, said:

Are you accounting for integer math in your 6/5 part of your code?

I didn't know I had to, but no! In my code, I'm allowing PHP to calculate it all and then using the round() function to bring it to the closest whole number.

That is, the actual answer when plugging in 1 is -53.8

This post has been edited by Splashsky: 25 September 2018 - 05:27 PM

### #4 Skydiver

• Code herder

Reputation: 6767
• Posts: 23,071
• Joined: 05-May 12

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 06:00 PM

Now that I am on a device with an slightly larger screen, those expected results don't look right. Are you sure you have the right equation?

### #5 Splashsky

• D.I.C Regular

Reputation: 12
• Posts: 334
• Joined: 25-August 13

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 06:07 PM

Skydiver, on 25 September 2018 - 06:00 PM, said:

Now that I am on a device with an slightly larger screen, those expected results don't look right. Are you sure you have the right equation?

After posting to the Mathematics Stack Overflow, I've begun to question it myself! This formula is ripped from the Pokemon games, and data was compiled by some fan boys for the wiki. After looking through the source code, it's definitely what they use, but their results in the game is far different from what anyone is getting.

Here's a couple graph representations of what the expected results are...
The formula is represented in these graphs by the purple line.

### #6 modi123_1

• Suitor #2

Reputation: 14854
• Posts: 59,272
• Joined: 12-June 08

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 06:23 PM

Quote

This formula is ripped from the Pokemon games, and data was compiled by some fan boys for the wiki.

Source?

### #7 modi123_1

• Suitor #2

Reputation: 14854
• Posts: 59,272
• Joined: 12-June 08

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 06:40 PM

Splashsky, on 25 September 2018 - 08:26 PM, said:

Bottom of the file here (GitHub), in an opened ROM for GameBoy.

Hmm.. No.. that is "This is a disassembly of Pokémon Red and Blue.". Not good.

### #8 Skydiver

• Code herder

Reputation: 6767
• Posts: 23,071
• Joined: 05-May 12

## Re: Mathematical Equation Not Yielding Expected Results

Posted 25 September 2018 - 06:40 PM

Quote

The inflection point for this polynomial function is actually at level 4, not level 0. Thus, it actually takes more experience points to go from level 2 to 3 than it does to go from 4 to 5. In Generations I and II, this mislocation of the inflection point causes the experience underflow glitch.

And following further regarding the underflow glitch, it mentions about computing -54 being treated as unsigned. Anyway, reading further, it mentions that due to the underflow, a lookup table is used instead:

Quote

The glitch was fixed in Generation III, which uses a lookup table, rather than a formula, to determine experience requirements (this is also why the two new functions introduced in that generation are able to be piecewise functions). Despite this, Eggs still hatched at level 5, and the lowest level that Pokémon could be found at in the wild was still level 2. Generation IV finally featured the first instance of legitimate level 1 Pokémon, where Eggs hatch at level 1 and level 1 Pokémon can be found in the wild.

I am guessing that your "expected results" above was actually the lookup table being used for the the case level 4 and below. It's to override the values to the left of the inflection point.

### #9 baavgai

• Dreaming Coder

Reputation: 7397
• Posts: 15,330
• Joined: 16-October 07

## Re: Mathematical Equation Not Yielding Expected Results

Posted 26 September 2018 - 06:54 AM

I'm still trying to worry out your math.

e.g.
```f(n) = 6/5 * pow(n, 3) - 15 * pow(n, 2) + 100 * n - 140
assert f(1) == 9

f(1) = 6/5 * 1 - 15 * 1 + 100 * 1 - 140
f(1) = 6/5 - 15 + 100 - 140
f(1) = 6/5 - 55
f(1) = -54 + 1/5
f(1) = -53 4/5 = -53.8

```

Your result seems to be what's expected. What would not be expected is for you to to get 9 from that is this universe.