try add some numeric limits checks

This commit is contained in:
Bharat Nallan Chakravarthy 2022-06-23 20:21:50 +05:30
parent 4e7f8b0935
commit 33704888e5
3 changed files with 51 additions and 14 deletions

View File

@ -166,13 +166,32 @@ struct QuantileApproximateWeighted
if (level > xr)
yl = yr;
UnderlyingType dy = yr - yl;
auto dx = xr - xl;
UnderlyingType dy;
if (yr - yl > std::numeric_limits<UnderlyingType>::max())
dy = std::numeric_limits<UnderlyingType>::max();
else if (yr - yl < std::numeric_limits<UnderlyingType>::min())
dy = std::numeric_limits<UnderlyingType>::min();
else
dy = yr - yl;
Float64 dx;
if (xr - xl > std::numeric_limits<Float64>::max())
dx = std::numeric_limits<Float64>::max();
else if (xr - xl < std::numeric_limits<Float64>::min())
dx = std::numeric_limits<Float64>::min();
else
dx = xr - xl;
dx = dx == 0 ? 1 : dx;
UnderlyingType dydx = dy / dx;
g = yl + dydx * (level - xl);
if ((yl + (dy / dx) * (level - xl)) > std::numeric_limits<UnderlyingType>::max())
g = std::numeric_limits<UnderlyingType>::max();
else if (yl + (dy / dx) * (level - xl) < std::numeric_limits<UnderlyingType>::min())
g = std::numeric_limits<UnderlyingType>::min();
else
g = yl + (dy / dx) * (level - xl);
return g;
}
@ -269,14 +288,32 @@ struct QuantileApproximateWeighted
if (level > xr)
yl = yr;
UnderlyingType dy;
UnderlyingType dy = yr - yl;
auto dx = xr - xl;
if (yr - yl > std::numeric_limits<UnderlyingType>::max())
dy = std::numeric_limits<UnderlyingType>::max();
else if (yr - yl < std::numeric_limits<UnderlyingType>::min())
dy = std::numeric_limits<UnderlyingType>::min();
else
dy = yr - yl;
Float64 dx;
if (xr - xl > std::numeric_limits<Float64>::max())
dx = std::numeric_limits<Float64>::max();
else if (xr - xl < std::numeric_limits<Float64>::min())
dx = std::numeric_limits<Float64>::min();
else
dx = xr - xl;
dx = dx == 0 ? 1 : dx;
UnderlyingType dydx = dy / dx;
g = yl + dydx * (level - xl);
if ((yl + (dy / dx) * (level - xl)) > std::numeric_limits<UnderlyingType>::max())
g = std::numeric_limits<UnderlyingType>::max();
else if ((yl + (dy / dx) * (level - xl)) < std::numeric_limits<UnderlyingType>::min())
g = std::numeric_limits<UnderlyingType>::min();
else
g = yl + (dy / dx) * (level - xl);
result[indices[level_index]] = g;
++level_index;

View File

@ -1,4 +1,4 @@
10 [1,1,1,1,10,10,10,10,100,100,100]
10 [1,1,2,4,7,10,10,10,10,100,100]
10 [1,1,2,4,7,10,35,61,87,100,100]
100 100
61 61

View File

@ -15,10 +15,10 @@ quantileApproximateWeighted
-25.5 -8.49999999 -5.1 Decimal(38, 8)
0 0 0
9.7 3.23333333 1.94
19.8999 6.63333332 3.97999999
30.0999 10.03333333 6.01999999
40.2999 13.43333332 8.05999999
19.9 6.63333332 3.98
30.1 10.03333333 6.02
40.3 13.43333332 8.06
50 16.66666666 10
[-50,-40.4,-30.3,-20.2,-10.1,0,10.0999,20.1999,30.2999,40.3999,50]
[-50,-40.4,-30.3,-20.2,-10.1,0,10.1,20.2,30.3,40.4,50]
[-16.66666666,-13.46666666,-10.09999999,-6.73333332,-3.36666666,0,3.36666666,6.73333332,10.09999999,13.46666666,16.66666666]
[-10,-8.08,-6.06,-4.04,-2.02,0,2.01999999,4.03999999,6.05999999,8.07999999,10]
[-10,-8.08,-6.06,-4.04,-2.02,0,2.02,4.04,6.06,8.08,10]