Help with a math algorithm.

This is a discussion on Help with a math algorithm. within the C++ Programming forums, part of the General Programming Boards category; I'm translating a small python script into C++, for a fellow internaut. Though the script is less than 100 lines ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    21

    Help with a math algorithm.

    I'm translating a small python script into C++, for a fellow internaut.
    Though the script is less than 100 lines long, I haven't finished it. In fact I'm stuck a the very beginning.
    This snippet is the culprit:
    Code:
    if level < 10: 
            base_precision = 24 + (level - 1) * 4 
        elif level < 20: 
            base_precision = 24 + 8 * 4 + (level - 9) * 6 
        elif level < 30: 
            base_precision = 24 + 8 * 4 + 10 * 6 + (level - 19) * 8 
        elif level < 40: 
            base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + (level - 29) * 10 
        elif level < 50: 
            base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + (level - 39) * 12 
        elif level < 60: 
            base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + 10 * 12\ 
                + (level - 49) * 14 
        elif level < 70: 
            base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + 10 * 12 + 10 * 14\ 
                + (level - 59) * 16 
        elif level < 80: 
            base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + 10 * 12 + 10 * 14\ 
                + 10 * 16 + (level - 69) * 18 
        else: 
            base_precision = 24 + (8 * 4) + (10 * 6) + (10 * 8) + (10 * 10) + (10 * 12)\ 
                + (10 * 14) + (10 * 16) + (10 * 18) + 20
    The reason why he used those whimsical calculations elude me. The first thing I thought, was to remove the unnecessary steps.
    Code:
    if (userTraits.level < 10)
          basePrecision = (userTraits.level -1) * 4 + 24;
       else if (userTraits.level < 20)
          basePrecision = (userTraits.level -9) * 6 + 56;
       else if (userTraits.level < 30)
          basePrecision = (userTraits.level -19) * 8 + 116;
    //........
    else
          basePrecision = 916;
    That was simple enough, and it worked. But I was struck with the idea that there is a pattern in those numbers.
    After puling my hair for while I finally I came up with this formula:
    Code:
    basePrecision = (level * 0.1 + 3.2) * level + 20
    It works alright for the most part, but it's a little off for certain values of level. e.g: if level = 34, basePrecision should be 246, but my formula returns 244.4
    For such a small difference I thought playing around with ceil() or floor() would even it out. The problem is that it does, but it offsets other correct values.

    My math skills are next to nonexistent. Perhaps you guys can see something there that I didn't, and come up with a more accurate formula.

    Any help is appreciated.
    Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I don't think you can find a reasonable approximation in a single formula. But you might come up with a loop.
    The pattern is:
    base_precision = 24
    for each level below 10, add 4
    for each additional level below 20, add 6
    for each additional level below 30, add 8
    etc.

    Something like:
    Code:
    int tlevel = level;
    int base_precision = 24;
    int group = 1;
    while (tlevel >= 10) {
      base_precision += 20 * group;
      tlevel -= 10;
      ++group;
    }
    base_precision += 2 * group * tlevel;
    Except that this contains some off-by-one errors for the thresholds.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    The bigger question is whether or not the original code is more correct that either of the simplifications listed in the thread.

    But another approach is a to have a lookup table of base values plus a variable for the level you happen to be in
    Code:
    int base[] = {24, 24 + 8 * 4, 24 + 8 * 4 + 10 * 6, 24 + 8 * 4 + 10 * 6 + 10 * 8, ...}
    
    int val = base[level / 10] + (level % 10) * (4 *  2* level/10);
    Or something like that.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Replace 4 * 2 * level / 10 with 4 + 2 * level/10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't use <math.h>
    By zakiuz in forum C Programming
    Replies: 6
    Last Post: 09-08-2011, 06:18 PM
  2. math.h
    By tsutharssan in forum C Programming
    Replies: 3
    Last Post: 07-03-2009, 02:24 PM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 02:14 AM
  4. More a math question than an algorithm
    By Gustaff in forum C Programming
    Replies: 1
    Last Post: 01-28-2003, 12:10 PM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21