4

Anyone know of a good implementation of this whose license is compatible with non-free iPhone apps?

As suggested in this question, Boost looks absolutely wonderful. But as best I can tell, it is only available in C++.

Cumulative Normal Distribution Function in C/C++

2 Answers 2

12

No need for anything fancy. Any platform with a good C99 math library (like the iphone) already has everything you need -- specifically the erfc function, which is a slight variant:

#import <math.h>

double cumulativeNormal(double x) {
    return 0.5 * erfc(-x * M_SQRT1_2);
}

Note this is for the standard normal distribution (i.e. mean = 0, variance = 1), you'll need to use the appropriate scaling -- so for some mean and variance, it will be:

return 0.5 * erfc(((mean - x)/sqrt(variance)) * M_SQRT1_2);

If you don't need double precision, change the doubles to floats, use the erfcf function, and change the literals to single-precision literals.

Remember, Objective-C is an extremely light extension to C. Every C function is an Objective-C function, with no wrapping or other shenanigans required.

2

So you specifically want an objective-c solution, correct? Because, just in case you were not already aware, you can use C++ libraries within your project.

2
  • Wow, I didn't know that, I wish I could accept both answers, as both are great. May 7, 2010 at 4:26
  • No worries. Stephen provided an original answer, I just linked to another question that might have helped you :) May 7, 2010 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.