Results 1 to 5 of 5

Thread: [RESOLVED] Need help with PHP & maths functions

  1. #1

    Thread Starter
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Resolved [RESOLVED] Need help with PHP & maths functions

    Well, I have this code,

    PHP Code:
    $a 0
    $goal2 $goal 1;
    for (
    $x 1;  $x <= $goal2$x += 1) {
      
    $a $a intval($x 300 * (^ ($x 7)));
      echo 
    $a.'<BR>';
    }
      
    $xp2 intval($a 4) - $xp;
    echo 
    $xp2
    and apart from the echos, it is completly wrong

    Heres a sample: Clicky Me

    and from 1 -> 99 the output should be this:
    Code:
    0
    83
    174
    276
    388
    512
    650
    801
    969
    1154
    1358
    1584
    1833
    2107
    2411
    2746
    3115
    3523
    3973
    4470
    5018
    5624
    6291
    7028
    7842
    8740
    9730
    10824
    12031
    13363
    14834
    16456
    18247
    20224
    22406
    24815
    27473
    30408
    33648
    37224
    41171
    45529
    50339
    55649
    61512
    67983
    75127
    83014
    91721
    101333
    111945
    123660
    136594
    150872
    166636
    184040
    203254
    224466
    247886
    273742
    302288
    333804
    368599
    407015
    449428
    496254
    547953
    605032
    668051
    737627
    814445
    899257
    992895
    1096278
    1210421
    1336443
    1475581
    1629200
    1798808
    1986068
    2192818
    2421087
    2673114
    2951373
    3258594
    3597792
    3972294
    4385776
    4842295
    5346332
    5901831
    6517253
    7195629
    7944614
    8771558
    9684577
    10692629
    11805606
    13034431

    And for those who know VB6, this is the original function:

    VB Code:
    1. Public Function ExperienceNeededToGoalLevel(ByVal YourXP As Double, GoalLvl As Integer) As Double
    2.    Dim a As Long
    3.    Dim x As Long
    4.    GoalLvl = GoalLvl - 1
    5.    For x = 1 To GoalLvl
    6.        a = a + Int(x + 300 * (2 ^ (x / 7)))
    7.    Next
    8.    ExperienceNeededToGoalLevel = Int(a / 4) - YourXP
    9. End Function





    I tried to modify it so it works with PHP, but the numbers are always off, is there something i am missing?


    Cheers
    Last edited by Jazz00006; Dec 22nd, 2006 at 11:50 PM.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Need help with PHP & maths functions

    what are you entering into the function to get the sample output you gave?

    I remade the function in PHP was entering 0 as experience and made a for() loop that looped through 1 to 99 to get the big list. you provided just about everything, except what you were entering as input to the function.. so without that, I'm not sure what else I can do. I rewrote the function though, this is what I made:
    PHP Code:
    <pre>
    <?php
    /*
    Public Function ExperienceNeededToGoalLevel(ByVal YourXP As Double, GoalLvl As Integer) As Double
       Dim a As Long
       Dim x As Long
       GoalLvl = GoalLvl - 1
       For x = 1 To GoalLvl
           a = a + Int(x + 300 * (2 ^ (x / 7)))
       Next
       ExperienceNeededToGoalLevel = Int(a / 4) - YourXP
    End Function
    */
      
    function getExperience($exp$goal){
        
    $goal--;
        
    $e 0;
        for(
    $i 1$i $goal$i++){
          
    $e += ($i 300) * (2^($i 7));
        }
        return 
    floor($e 4) - $exp;
      }
      for(
    $i 1$i 99$i++){
        echo 
    getExperience(0$i) . "\n";
      }
    ?>
    </pre>
    it outputs the following from the included for() loop:
    Code:
    0
    150
    301
    452
    603
    754
    905
    1132
    1359
    1586
    1814
    2042
    2270
    2498
    2501
    2505
    2509
    2513
    2518
    2523
    2528
    2608
    2688
    2769
    2850
    2931
    3013
    3095
    3552
    4009
    4466
    4924
    5382
    5840
    6299
    6833
    7367
    7901
    8435
    8970
    9505
    10040
    10351
    10662
    10973
    11284
    11595
    11907
    12219
    12606
    12994
    13382
    13770
    14158
    14546
    14935
    15699
    16463
    17228
    17993
    18758
    19523
    20288
    21129
    21970
    22811
    23653
    24495
    25337
    26179
    26796
    27414
    28032
    28650
    29269
    29888
    30507
    31201
    31895
    32590
    33285
    33980
    34676
    35372
    36443
    37514
    38585
    39657
    40729
    41801
    42874
    44022
    45170
    46318
    47466
    48615
    49764
    provide a bit more information and I will try to help you out.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Need help with PHP & maths functions

    I'm reinstalling VB right now so I can find out what your VB function outputs and so that I can mimic it.

    edit: after tinkering for a bit, I couldn't figure out what was wrong. so.. I just typed this out in VB:
    VB Code:
    1. MsgBox (1 + 300 * 2 ^ (1 / 7))
    It returned 332.2268. I typed the same equation in my calculator and got 332.2268. I typed the same equation into PHP, and got... 601. After breaking it down, it seems like PHP isn't handling the "^" operator correctly. I used the pow() function instead, and it worked perfectly. I've never worked with exponents before in PHP because I've never had the need to, but I guess you learn something new everyday. The function below does exactly what you want.
    PHP Code:
    <pre>
    <?php
      
    function getExperience($exp$goal){
        
    $goal--;
        for(
    $i 1$i <= $goal$i++){
          
    $e += intval($i 300 pow(2$i 7));
        }
        return 
    intval($e 4) - $exp;
      }
      for(
    $i 1$i <= 99$i++){
        echo 
    $i ". " getExperience(0$i) . "\n";
      }
    ?>
    </pre>
    The for() loop included will output the list you sampled in your first post.

    edit again: had a problem with rounding, fixed it.
    Last edited by kows; Dec 23rd, 2006 at 03:15 AM.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Need help with PHP & maths functions

    The ^ operator in C-based languages is XOR, not power.

  5. #5

    Thread Starter
    Addicted Member Jazz00006's Avatar
    Join Date
    Feb 2006
    Posts
    185

    Re: Need help with PHP & maths functions

    I did know that the ^ was wrong, and i did try the pow() function, but it still gave wrong awnsers, thanks kows for re-making it for me, it works perfectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width