Marshall Clow found some divide-by-zero warnings with UBSan in rand's binomial_distribution test. This eliminates the divide-by-zeros and describes in comments the numerical difficulties the test is having. Each of the problematic tests are exploring edge cases of the distribution.

llvm-svn: 177826
This commit is contained in:
Howard Hinnant 2013-03-23 19:29:45 +00:00
parent b5b9110b51
commit a60ae88db2
1 changed files with 65 additions and 25 deletions

View File

@ -177,15 +177,23 @@ int main()
}
var /= u.size();
double dev = std::sqrt(var);
skew /= u.size() * dev * var;
kurtosis /= u.size() * var * var;
kurtosis -= 3;
// In this case:
// skew computes to 0./0. == nan
// kurtosis computes to 0./0. == nan
// x_skew == inf
// x_kurtosis == inf
// These tests are commented out because UBSan warns about division by 0
// skew /= u.size() * dev * var;
// kurtosis /= u.size() * var * var;
// kurtosis -= 3;
double x_mean = d.t() * d.p();
double x_var = x_mean*(1-d.p());
double x_skew = (1-2*d.p()) / std::sqrt(x_var);
double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
// double x_skew = (1-2*d.p()) / std::sqrt(x_var);
// double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
assert(mean == x_mean);
assert(var == x_var);
// assert(skew == x_skew);
// assert(kurtosis == x_kurtosis);
}
{
typedef std::binomial_distribution<> D;
@ -215,15 +223,23 @@ int main()
}
var /= u.size();
double dev = std::sqrt(var);
skew /= u.size() * dev * var;
kurtosis /= u.size() * var * var;
kurtosis -= 3;
// In this case:
// skew computes to 0./0. == nan
// kurtosis computes to 0./0. == nan
// x_skew == -inf
// x_kurtosis == inf
// These tests are commented out because UBSan warns about division by 0
// skew /= u.size() * dev * var;
// kurtosis /= u.size() * var * var;
// kurtosis -= 3;
double x_mean = d.t() * d.p();
double x_var = x_mean*(1-d.p());
double x_skew = (1-2*d.p()) / std::sqrt(x_var);
double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
// double x_skew = (1-2*d.p()) / std::sqrt(x_var);
// double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
assert(mean == x_mean);
assert(var == x_var);
// assert(skew == x_skew);
// assert(kurtosis == x_kurtosis);
}
{
typedef std::binomial_distribution<> D;
@ -333,15 +349,23 @@ int main()
}
var /= u.size();
double dev = std::sqrt(var);
skew /= u.size() * dev * var;
kurtosis /= u.size() * var * var;
kurtosis -= 3;
// In this case:
// skew computes to 0./0. == nan
// kurtosis computes to 0./0. == nan
// x_skew == inf
// x_kurtosis == inf
// These tests are commented out because UBSan warns about division by 0
// skew /= u.size() * dev * var;
// kurtosis /= u.size() * var * var;
// kurtosis -= 3;
double x_mean = d.t() * d.p();
double x_var = x_mean*(1-d.p());
double x_skew = (1-2*d.p()) / std::sqrt(x_var);
double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
// double x_skew = (1-2*d.p()) / std::sqrt(x_var);
// double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
assert(mean == x_mean);
assert(var == x_var);
// assert(skew == x_skew);
// assert(kurtosis == x_kurtosis);
}
{
typedef std::binomial_distribution<> D;
@ -371,15 +395,23 @@ int main()
}
var /= u.size();
double dev = std::sqrt(var);
skew /= u.size() * dev * var;
kurtosis /= u.size() * var * var;
kurtosis -= 3;
// In this case:
// skew computes to 0./0. == nan
// kurtosis computes to 0./0. == nan
// x_skew == inf
// x_kurtosis == inf
// These tests are commented out because UBSan warns about division by 0
// skew /= u.size() * dev * var;
// kurtosis /= u.size() * var * var;
// kurtosis -= 3;
double x_mean = d.t() * d.p();
double x_var = x_mean*(1-d.p());
double x_skew = (1-2*d.p()) / std::sqrt(x_var);
double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
// double x_skew = (1-2*d.p()) / std::sqrt(x_var);
// double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
assert(mean == x_mean);
assert(var == x_var);
// assert(skew == x_skew);
// assert(kurtosis == x_kurtosis);
}
{
typedef std::binomial_distribution<> D;
@ -409,14 +441,22 @@ int main()
}
var /= u.size();
double dev = std::sqrt(var);
skew /= u.size() * dev * var;
kurtosis /= u.size() * var * var;
kurtosis -= 3;
// In this case:
// skew computes to 0./0. == nan
// kurtosis computes to 0./0. == nan
// x_skew == -inf
// x_kurtosis == inf
// These tests are commented out because UBSan warns about division by 0
// skew /= u.size() * dev * var;
// kurtosis /= u.size() * var * var;
// kurtosis -= 3;
double x_mean = d.t() * d.p();
double x_var = x_mean*(1-d.p());
double x_skew = (1-2*d.p()) / std::sqrt(x_var);
double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
// double x_skew = (1-2*d.p()) / std::sqrt(x_var);
// double x_kurtosis = (1-6*d.p()*(1-d.p())) / x_var;
assert(mean == x_mean);
assert(var == x_var);
// assert(skew == x_skew);
// assert(kurtosis == x_kurtosis);
}
}