Skip to content

Commit 2d3dbe5

Browse files
committed
find the exact lower bound causes overflow
1 parent 6f7eb15 commit 2d3dbe5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

stl/inc/random

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,11 @@ public:
188188
for (_Kx = 0; _Kx < _Mx; ++_Kx) { // scramble and add any vector contributions
189189
result_type _Rx1 =
190190
1664525 * _Xor27(_UFirst[_Kx % _Nx] ^ _UFirst[(_Kx + _Px) % _Nx] ^ _UFirst[(_Kx - 1) % _Nx]);
191-
result_type _Rx2 = static_cast<result_type>(
192-
(_Rx1 + (_Kx == 0 ? _Sx : _Kx <= _Sx ? _Kx % _Nx + _Myvec[(_Kx - 1) % _Sx] : _Kx % _Nx)) & _Mask);
191+
result_type _Rx2 = static_cast<result_type>((_Rx1
192+
+ (_Kx == 0 ? _Sx
193+
: _Kx <= _Sx ? _Kx % _Nx + _Myvec[(_Kx - 1) % _Sx]
194+
: _Kx % _Nx))
195+
& _Mask);
193196

194197
_UFirst[(_Kx + _Px) % _Nx] = (_UFirst[(_Kx + _Px) % _Nx] + _Rx1) & _Mask;
195198
_UFirst[(_Kx + _Qx) % _Nx] = (_UFirst[(_Kx + _Qx) % _Nx] + _Rx2) & _Mask;
@@ -2929,14 +2932,17 @@ private:
29292932
_Res = _Xx2;
29302933
_Valid = false;
29312934
} else { // generate two values, store one, return one
2935+
const _Ty _MinSx{static_cast<_Ty>(7.8159598771420286e-306)};
2936+
const _Ty _MaxSx{static_cast<_Ty>(1)};
29322937
_Ty _Vx1;
29332938
_Ty _Vx2;
29342939
_Ty _Sx;
29352940
for (;;) { // reject bad values
29362941
_Vx1 = 2 * _NRAND(_Eng, _Ty) - 1;
29372942
_Vx2 = 2 * _NRAND(_Eng, _Ty) - 1;
29382943
_Sx = _Vx1 * _Vx1 + _Vx2 * _Vx2;
2939-
if (_Sx < 1 && _Sx != 0) {
2944+
if (_MinSx < _Sx && _Sx < _MaxSx) {
2945+
// Good _Sx value! It will not cause overflow nor generating NaN/Inf on the next calculations.
29402946
break;
29412947
}
29422948
}

0 commit comments

Comments
 (0)