Jump to content

Fairly simply math formula - getting difference in numbers


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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.