Jump to content

Fairly simply math formula - getting difference in numbers


slyte33

Recommended Posts

I am working on my MMORPG. I am looking to get the difference in EXP per level. Take a look at this code below:

 

for($i=1;$i<=10;$i++)
{
$xp2+=10*$i;
$xp=(1.3726*$i)*$xp2;

echo "$i. ".number_format($xp)."";
echo "<br>";
if ($i!=1)
{
$diff= //FORMULA TO FIND THE DIFFERENCE
echo "[DIFF: ".number_format($diff)." XP]";
}
echo "<br><br>";
}

 

The above would output:

1. 14

2. 82
[DIFF: 0 XP]

3. 247
[DIFF: 0 XP]

4. 549
[DIFF: 0 XP]

5. 1,029
[DIFF: 0 XP]

6. 1,729
[DIFF: 0 XP]

7. 2,690
[DIFF: 0 XP]

8. 3,953
[DIFF: 0 XP]

9. 5,559
[DIFF: 0 XP]

10. 7,549
[DIFF: 0 XP]

 

As you can see,

2. 82-14=68

3. 247-82=165

 

and so on. I would just like the difference. I spent too much time trying to get this right, thanks :)

 

Just store the previous one and subtract them:

 

<?php
$prev = null;
$xp2 = 0;
for($i=1;$i<=10;$i++)
{
    $xp2+=10*$i;
    $xp=(1.3726*$i)*$xp2;

    echo "$i. ".number_format($xp)."";
    echo "<br>";
    if ($i!=1) {
        $diff = $xp - $prev;
        echo "[DIFF: ".number_format($diff)." XP]";
    }
    echo "<br><br>";

    $prev = $xp;
}

Just store the previous one and subtract them:

 

<?php
$prev = null;
$xp2 = 0;
for($i=1;$i<=10;$i++)
{
    $xp2+=10*$i;
    $xp=(1.3726*$i)*$xp2;

    echo "$i. ".number_format($xp)."";
    echo "<br>";
    if ($i!=1) {
        $diff = $xp - $prev;
        echo "[DIFF: ".number_format($diff)." XP]";
    }
    echo "<br><br>";

    $prev = $xp;
}

 

Ahh so simple and effective. Learn something new about PHP everytime I come here whether I ask a question or not, thank you so much!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.