How to convert percentage to z-score of normal distribution in C/C++?

The goal is to say: "These values lie within a band of 95 % of values around the mean in a normal distribution."

Now, I am trying to convert percentage to z-score, so then I can get the precise range of values. Something like <lower bound , upper bound> would be enough.

So I need something like

double z_score(double percentage) {
   // ...
}

// ...

// according to https://en.wikipedia.org/wiki/68–95–99.7_rule
z_score(68.27) == 1
z_score(95.45) == 2
z_score(99.73) == 3

I found an article explaining how to do it with a function from boost library, but

double z_score( double percentage ) {
    return - sqrt( 2 ) / boost::math::erfc_inv( 2 * percentage / 100 );
}

does not work properly and it returns weird values.

z_score(95) == 1.21591 // instead of 1.96

Also the boost library is kinda heavy and I plan to use it for Ruby gem, so it should be as lightweight as possible.

Does anyone have an idea?

Source: Windows Questions C++

LEAVE A COMMENT