Search code examples
cmathnotation

Converting academic mathematical notation to C code


Those of you that are even moderately knowledgable of math will likely laugh, but I don't remember what much of the notation rules in math and I need assistance converting this into C code. Your help is greatly appreciated:

                                         214
          10,000 {(10,000 × [1+.0599/365]   )} +300
answer = ────────────────────────────────────────────
                                   214
                     .1+(1+(i/365))

Solution

  • Are you looking for a program to translate that for you, or just a one-off conversion? If it's just a one off conversion, it's not that exciting.

    Assuming I've read your syntax correctly:

    double ans = 10000 * (10000 * pow(1.0 + 0.0599 / 365, 214) + 300;
    ans /= (0.1 + pow(1.0 + (i / 365.0), 214));
    

    I will say, though, that you may have an issue with raising things to that high of an exponent and dividing. More likely you will have to translate to logs and do your math in the log space, and convert afterwards.

    What that might look like:

    double lognumerator = log(10000) + log(10000) + 214 * log(1 + 0.0599 / 365);
    double logdenominator = log(0.1 + exp(214 * log(1.0 + (i / 365.0))));
    double ans = exp(lognumerator - logdenominator) + exp(log(300) - logdenominator);
    

    The use of log may prevent you from hitting underflow, which you may very well hit with these types of computations.