-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fixes incorrect rounding in rejection method. #1159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StephanTLavavej
merged 15 commits into
microsoft:master
from
MattStephanson:random_rejection_rounding
Oct 3, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
63fdfc9
Fixes incorrect rounding in rejection method.
MattStephanson 4353d43
Fix formatting
MattStephanson 58883ed
Fix out-of-range casts from double to integer types
MattStephanson c575cb9
Tests for #1001 and #1123
MattStephanson 4999c1c
Merge branch 'master' into random_rejection_rounding
MattStephanson d703774
clang-format
MattStephanson 8e31f45
buildfix and code review comments
MattStephanson 831f65f
buildfix - BSR for all integer widths
MattStephanson 89c708e
Merge branch 'master' into random_rejection_rounding
MattStephanson 1d8d743
Restore line endings from merge conflict
MattStephanson b260904
buildfix
MattStephanson f6d395a
Apply suggestions from code review
MattStephanson 3fa04cc
suggestions from code review
MattStephanson 13a2527
Apply suggestions from code review
StephanTLavavej 4afbecc
Fix /clr:pure compiler error.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_matrix.lst |
43 changes: 43 additions & 0 deletions
43
tests/std/tests/GH_001001_random_rejection_rounding/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <map> | ||
| #include <random> | ||
|
|
||
| void Test_GH1001() { | ||
| constexpr int N{1000}; | ||
| constexpr double p{.001238}; | ||
| constexpr int seed{12345}; | ||
| constexpr int iters{1'000'000}; | ||
| std::map<int, int> frequency; | ||
|
|
||
| std::mt19937 mt_rand(seed); | ||
|
|
||
| std::binomial_distribution<int> distribution(N, p); | ||
|
|
||
| for (int i = 0; i < iters; ++i) { | ||
| ++frequency[distribution(mt_rand)]; | ||
| } | ||
|
|
||
| double mean_x{0.0}; | ||
| for (const auto& valueCountPair : frequency) { | ||
| mean_x += valueCountPair.first * static_cast<double>(valueCountPair.second) / iters; | ||
| } | ||
| const double p0_x{static_cast<double>(frequency[0]) / iters}; | ||
| const double p1_x{static_cast<double>(frequency[1]) / iters}; | ||
|
|
||
| const double p0{std::pow(1.0 - p, static_cast<double>(N))}; | ||
| const double p1{1000.0 * p * std::pow(1.0 - p, static_cast<double>(N - 1))}; | ||
| const double mean{p * N}; | ||
|
|
||
| assert(std::abs(mean_x / mean - 1.0) < 0.01); | ||
| assert(std::abs(p0_x / p0 - 1.0) < 0.01); | ||
| assert(std::abs(p1_x / p1 - 1.0) < 0.01); | ||
| } | ||
|
|
||
| int main() { | ||
| Test_GH1001(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_matrix.lst |
86 changes: 86 additions & 0 deletions
86
tests/std/tests/GH_001123_random_cast_out_of_range/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
MattStephanson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <random> | ||
|
|
||
| #pragma warning(disable : 4984) // if constexpr is a C++17 language extension | ||
| #ifdef __clang__ | ||
| #pragma clang diagnostic ignored "-Wc++17-extensions" | ||
| #endif // __clang__ | ||
|
|
||
| template <class T> | ||
| using lim = std::numeric_limits<T>; | ||
|
|
||
| template <class IntType, class FltType> | ||
| void CheckUpperBound(IntType i, FltType fmax) { | ||
| const auto x{std::_Float_upper_bound<FltType>(i)}; | ||
| const auto y{std::nextafter(x, FltType{0})}; // lower bound, <= i | ||
|
|
||
| assert(y < fmax); | ||
| assert(static_cast<IntType>(y) <= i); | ||
| assert(x <= fmax); | ||
| if (x < fmax) { | ||
| assert(static_cast<IntType>(x) > i); | ||
| } | ||
| } | ||
|
|
||
| template <class IntType, class FltType> | ||
| void TestUpperBoundExhaustive() { | ||
| const auto fmax{exp2(static_cast<FltType>(lim<IntType>::digits))}; | ||
| IntType i{0}; | ||
| do { | ||
| CheckUpperBound(i, fmax); | ||
| } while (++i != IntType{0}); | ||
| } | ||
|
|
||
| template <class T> | ||
| constexpr T FillLsb(int n) { | ||
| if (n <= 0) { | ||
| return 0; | ||
| } | ||
| T x{T{1} << (n - 1)}; | ||
| return (x - 1) ^ x; | ||
| } | ||
|
|
||
| template <class IntType, class FltType> | ||
| void TestUpperBoundSelective() { | ||
| const auto fmax{exp2(static_cast<FltType>(lim<IntType>::digits))}; | ||
| CheckUpperBound(IntType{0}, fmax); | ||
| CheckUpperBound(IntType{1}, fmax); | ||
| CheckUpperBound(lim<IntType>::max(), fmax); | ||
|
|
||
| constexpr int diff{lim<IntType>::digits - lim<FltType>::digits}; | ||
| if constexpr (diff > 0) { | ||
| // crossover from ulp < 1 to ulp = 1 | ||
| constexpr auto a{FillLsb<IntType>(lim<FltType>::digits - 1)}; | ||
| CheckUpperBound(a - 1, fmax); | ||
| CheckUpperBound(a, fmax); | ||
|
|
||
| // crossover from ulp = 1 to ulp > 1 | ||
| constexpr auto b{FillLsb<IntType>(lim<FltType>::digits)}; | ||
| CheckUpperBound(b, fmax); | ||
| CheckUpperBound(b + 1, fmax); | ||
| CheckUpperBound(b + 2, fmax); | ||
|
|
||
| // saturation at the largest representable IntType | ||
| constexpr auto c{FillLsb<IntType>(lim<FltType>::digits) << diff}; | ||
| CheckUpperBound(c - 1, fmax); | ||
| CheckUpperBound(c, fmax); | ||
| CheckUpperBound(c + 1, fmax); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| TestUpperBoundExhaustive<std::uint8_t, float>(); | ||
| TestUpperBoundExhaustive<std::uint16_t, float>(); | ||
| TestUpperBoundSelective<std::uint32_t, float>(); | ||
|
|
||
| TestUpperBoundExhaustive<unsigned short, double>(); | ||
| TestUpperBoundSelective<unsigned int, double>(); | ||
| TestUpperBoundSelective<unsigned long, double>(); | ||
| TestUpperBoundSelective<unsigned long long, double>(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.