Rs*rs*rs....



  • function xp(rs:double; i:integer) : double;
      begin
      case i of
        0 : result := 1;
        1 : result := rs;
        2 : result := rsrs;
        3 : result := rs
    rsrs;
        4 : result := rs
    rsrsrs;
        5 : result := rsrsrsrsrs;
        6 : result := rsrsrsrsrsrs;
        7 : result := rs
    rsrsrsrsrsrs;
        8 : result := rs
    rsrsrsrsrsrsrs;
        9 : result := rsrsrsrsrsrsrsrsrs;
        10: result := rsrsrsrsrsrsrsrsrsrs;
        11: result := rs
    rsrsrsrsrsrsrsrsrsrs;
        12: result := rs
    rsrsrsrsrsrsrsrsrsrsrs;
        else
          result := exp(i * ln(rs));
        end;
      end;

    Sigh...



  • Wow. That would have really interesting effects on a loop along the lines of

    sum = 0;
    for (i = 0; i < n; i++)
    begin
        sum += xp(-2, n) * foo(n);
    end;
    


  • This would be much cooler in a language with fall-through case statements:

    public static double xp(double rs, int i) {
      double result = 1d;
      switch (i) {
        case 12:
          result *= rs;
        case 11:
          result *= rs;
        case 10:
          result *= rs;
        case 9:
          result *= rs;
        case 8:
          result *= rs;
        case 7:
          result *= rs;
        case 6:
          result *= rs;
        case 5:
          result *= rs;
        case 4:
          result *= rs;
        case 3:
          result *= rs;
        case 2:
          result *= rs;
        case 1:
          result *= rs;
        case 0:
          return result;
        default:
          return Math.exp(i * Math.log(rs));				
      }
    }
    


  •  This would be much cooler in a language with the ** operator.



  • Much more optimized solution in C:

    double xp(double rs, int i)
    {
    double v =1;
    while (i>0)
    {
    switch (i)
    {
    case 9: v *= rs*rs*rs*rs;
    case 05: v *= rs*rs*rs*rs;
    case 01: v *= rs;
    break;
    case 10: v *= rs*rs*rs*rs;
    case 06: v *= rs*rs*rs*rs;
    case 02: v *= rs*rs;
    break;
    case 11: v *= rs*rs*rs*rs;
    case 07: v *= rs*rs*rs*rs;
    case 03: v *= rs*rs*rs;
    break;
    case 12: v *= rs*rs*rs*rs;
    case 8: v *= rs*rs*rs*rs;
    case 4: v *= rs*rs*rs*rs;
    break;
    default: v *= rs*rs*rs*rs*rs*rs*rs*rs*rs*rs*rs*rs*rs;
    }
    i -= 13;
    }
    return v;
    }

     



  • @geocities said:

    Much more optimized solution in C:


    Sorry, you haven't been around for long enough for me to be sure: is that a joke, a troll, or a serious, albeit misguided, attempt to optimise?



  • @pjt33 said:

    @geocities said:

    Much more optimized solution in C:


    Sorry, you haven't been around for long enough for me to be sure: is that a joke, a troll, or a serious, albeit misguided, attempt to optimise?
    Looks like a piss-take of Duff's Device. I'm going for 'joke.'



  • @PJH said:

    Looks like a piss-take of Duff's Device.
     

    Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against."

    I like it.



  • @geocities said:

    Much more optimized solution in C:

    double xp(double rs, int i)
    {
      double v = 1;
      double rs2 = rs*rs;
      double rs3 = rs2*rs;
      double rs4 = rs3*rs;
      double rs13 = rs4*rs4*rs4*rs;
      while (i>0)
      {
        switch (i) 
        {
          case  9:  v *= rs4;
          case 05:  v *= rs4;
          case 01:  v *= rs;
            break;
          case 10:  v *= rs4;
          case 06:  v *= rs4;
          case 02:  v *= rs2;
            break;
          case 11:  v *= rs4;
          case 07:  v *= rs4;
          case 03:  v *= rs3;
            break;
          case 12:  v *= rs4;
          case  8:  v *= rs4;
          case  4:  v *= rs4;
            break;
          default:  v *= rs13;
         }
        i -= 13;
      }
      return v;
    }
     

    E(ven)F(urther)O(optimized)TFY



  • OBVIOUSLY if you do things more than three times, you should use recursion:

     

    @Weps said:

    function xp(rs:double; i:integer) : double;
      begin
      case i of
        0 : result := 1;
        1 : result := rs;
        2 : result := rsrs;
        3 : result := rs
    rsrs;   
    else
          result := rs
    rs*rs + xp(i - 3, rs);
        end;
      end;

     



  • @pjt33 said:

    @geocities said:

    Much more optimized solution in C:


    Sorry, you haven't been around for long enough for me to be sure: is that a joke, a troll, or a serious, albeit misguided, attempt to optimise?



    Optimized coding in C is an art that only the most professional and excellent coders understand. I am not surprised that regular programmers such as you (please do not take this as an insult - not everyone can be a top coder!) are bewildered when they see "nonstandard" and "out-of-the-box" thinking.

    That is OK. This code is not for you. It is part of an exquisite dialog between a genius and a compiler. It is a piece of poetry understood only by the author and other genitus.

     

    Sincerely, 

     



  •  Ok, so a troll then. Thanks for clearing that up.



  •  Now that's what I call a power user.



  • @barfoo said:

     Now that's what I call a power user.

    ba-dum-TSCH



  • @geocities said:

    Much more optimized solution in C:

    double xp(double rs, int i)
    {
    double v =1;
    while (i>0)
    {
    switch (i)
    {
    case 011: v *= rs*rs*rs*rs;
    case 05: v *= rs*rs*rs*rs;
    case 01: v *= rs;
    break;
    case 012: v *= rs*rs*rs*rs;
    case 06: v *= rs*rs*rs*rs;
    case 02: v *= rs*rs;
    break;
    case 013: v *= rs*rs*rs*rs;
    case 07: v *= rs*rs*rs*rs;
    case 03: v *= rs*rs*rs;
    break;
    case 014: v *= rs*rs*rs*rs;
    case 010: v *= rs*rs*rs*rs;
    case 04: v *= rs*rs*rs*rs;
    break;
    default: v *= rs*rs*rs*rs*rs*rs*rs*rs*rs*rs*rs*rs*rs;
    }
    i -= 13;
    }
    return v;
    }

     

    It's a bit risky mixing bases in the case label values like that.  FTFY.




  • double xp(double rs, int i)
    {
          unsigned n = (i>=0)?i:-i;
          double result = 1;
          while (n)
              {
                     if (n & 1)  result *= rs;
                    rs *= rs;
                   n >>= 1; 
              }
           return (i>=0)?result : 1./ result;
    }



  • @Weps said:

    else
          result := exp(i * ln(rs));
     

    xp(-1.0,-1);

     

    Fun!



  • function xp(rs:double; i:integer) : double;

    begin

    case i of

    0 : result := 1;

    1 : result := rs;

    2 : result := rsrs;

    3 : result := rs
    rsrs;

    4 : result := rs
    rsrsrs;

    5 : result := rsrsrsrsrs;

    6 : result := rsrsrsrsrsrs;

    7 : result := rs
    rsrsrsrsrsrs;

    8 : result := rs
    rsrsrsrsrsrsrs;

    9 : result := rsrsrsrsrsrsrsrsrs;

    10: result := rsrsrsrsrsrsrsrsrsrs;

    11: result := rs
    rsrsrsrsrsrsrsrsrsrs;

    12: result := rs
    rsrsrsrsrsrsrsrsrsrsrs;

    else

    result := exp(i * ln(rs));

    end;

    while result > 1000

    begin

    result := result - 1000;

    level := level + 1;

    hp := hp + die.roll(1, 6)
    ;
    mp := mp + die.roll(1, 4);

    str := str + 1;

    con := con + 1;

    int := int + 1;

    end;

    end;


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.