Skip to content

Commit c28a9bb

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

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

stl/inc/random

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,14 +2929,17 @@ private:
29292929
_Res = _Xx2;
29302930
_Valid = false;
29312931
} else { // generate two values, store one, return one
2932+
const _Ty _MinSx{static_cast<_Ty>(7.8159598771420286e-306)};
2933+
const _Ty _MaxSx{static_cast<_Ty>(1)};
29322934
_Ty _Vx1;
29332935
_Ty _Vx2;
29342936
_Ty _Sx;
29352937
for (;;) { // reject bad values
29362938
_Vx1 = 2 * _NRAND(_Eng, _Ty) - 1;
29372939
_Vx2 = 2 * _NRAND(_Eng, _Ty) - 1;
2938-
_Sx = _Vx1 * _Vx1 + _Vx2 * _Vx2;
2939-
if (_Sx < 1 && _Sx != 0) {
2940+
_Sx = _Vx1 * _Vx1 + _Vx2 * _Vx2;
2941+
if (_MinSx < _Sx && _Sx < _MaxSx) {
2942+
// Good _Sx value! It will not cause overflow nor divide by zero nor generating NaN/Inf on the next calculations.
29402943
break;
29412944
}
29422945
}

0 commit comments

Comments
 (0)