Search code examples
excelpseudocode

Translate Excel formula into structured english


I have this formula, with the variables, m,s and e.

IFERROR(IF(m - IF(s*(1/ABS(e))>m;m;s*(1/ABS(e)))>m;m;m-IF(s*(1/ABS(e))>m;m;s*(1/ABS(e))))/100;0)

I want to translate it to structured english, this is what I have, but I doubt its correct:

IF e != 0

  IF m > m
     return m

  ELSE IF s / |e| > m
    return m

  ELSE IF s / e > m
    return m - s / e

  ELSE 
    return s / e

ELSE 
  return 0

?? forgot this one upss.. s / e / 100;

Solution

  • After analyzing the function, I think this simplified version has the same result:

    =IFERROR(IF(MIN(m;s/ABS(e))<0;m;m-MIN(m;s/ABS(e)))/100;0)
    

    And its logic can be explained like this:

    if e = 0
        0
    else if m - (min between s/|e| and m) > m
        m
    else
        m - (min between s/|e| and m)
    

    Even still, I don't understand what that is for. :P

    Edit: I added a simplified Javascript version of Sir Ben's code:

    function whacko (m,s,e) {
        if (e === 0)
            return 0;
    
        var value = m - Math.min(s / Math.abs(e), m);
    
        if (value > m)
            return m;
    
        return value;
    };