forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit4dd5ce2
committed
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raisingnumbers to very large powers.Firstly, when raising a negative number to a very large integer power,the result should be well-defined, but the previous code would onlycope if the exponent was small enough to go through power_var_int().Otherwise it would throw an internal error, attempting to take thelogarithm of a negative number. Fix this by adding suitable handlingto the general case in power_var() to cope with negative bases,checking for integer powers there.Next, when raising a (positive or negative) number whose absolutevalue is slightly less than 1 to a very large power, the result shouldapproach zero as the power is increased. However, in some cases, forsufficiently large powers, this would lose all precision and return 1instead of 0. This was due to the way that the local_rscale was beingcalculated for the final full-precision calculation: local_rscale = rscale + (int) val - ln_dweight + 8The first two terms on the right hand side are meant to give thenumber of significant digits required in the result ("val" being theestimated result weight). However, this failed to account for the factthat rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE(1000), and the result weight might be less then -1000, causing theirsum to be negative, leading to a loss of precision. Fix this byforcing the number of significant digits calculated to be nonnegative.It's OK for it to be zero (when the result weight is less than -1000),since the local_rscale value then includes a few extra digits toensure an accurate result.Finally, add additional underflow checks to exp_var() and power_var(),so that they consistently return zero for cases like this where theresult is indistinguishable from zero. Some paths through this codealready returned zero in such cases, but others were throwing overflowerrors.Dean Rasheed, reviewed by Yugo Nagata.Discussion:http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com1 parent317632f commit4dd5ce2
File tree
3 files changed
+132
-15
lines changed- src
- backend/utils/adt
- test/regress
- expected
- sql
3 files changed
+132
-15
lines changedLines changed: 66 additions & 15 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3993 | 3993 |
| |
3994 | 3994 |
| |
3995 | 3995 |
| |
3996 |
| - | |
| 3996 | + | |
| 3997 | + | |
| 3998 | + | |
3997 | 3999 |
| |
3998 | 4000 |
| |
3999 | 4001 |
| |
| |||
4003 | 4005 |
| |
4004 | 4006 |
| |
4005 | 4007 |
| |
4006 |
| - | |
4007 |
| - | |
4008 |
| - | |
4009 |
| - | |
4010 |
| - | |
4011 | 4008 |
| |
4012 | 4009 |
| |
4013 | 4010 |
| |
| |||
9822 | 9819 |
| |
9823 | 9820 |
| |
9824 | 9821 |
| |
9825 |
| - | |
| 9822 | + | |
9826 | 9823 |
| |
9827 | 9824 |
| |
9828 |
| - | |
9829 |
| - | |
9830 |
| - | |
| 9825 | + | |
| 9826 | + | |
| 9827 | + | |
| 9828 | + | |
| 9829 | + | |
| 9830 | + | |
| 9831 | + | |
| 9832 | + | |
| 9833 | + | |
9831 | 9834 |
| |
9832 | 9835 |
| |
9833 | 9836 |
| |
| |||
10185 | 10188 |
| |
10186 | 10189 |
| |
10187 | 10190 |
| |
| 10191 | + | |
| 10192 | + | |
10188 | 10193 |
| |
10189 | 10194 |
| |
10190 | 10195 |
| |
10191 | 10196 |
| |
| 10197 | + | |
10192 | 10198 |
| |
10193 | 10199 |
| |
10194 | 10200 |
| |
| |||
10228 | 10234 |
| |
10229 | 10235 |
| |
10230 | 10236 |
| |
| 10237 | + | |
10231 | 10238 |
| |
10232 | 10239 |
| |
10233 | 10240 |
| |
| 10241 | + | |
| 10242 | + | |
| 10243 | + | |
| 10244 | + | |
| 10245 | + | |
| 10246 | + | |
| 10247 | + | |
| 10248 | + | |
| 10249 | + | |
| 10250 | + | |
| 10251 | + | |
| 10252 | + | |
| 10253 | + | |
| 10254 | + | |
| 10255 | + | |
| 10256 | + | |
| 10257 | + | |
| 10258 | + | |
| 10259 | + | |
| 10260 | + | |
| 10261 | + | |
| 10262 | + | |
| 10263 | + | |
| 10264 | + | |
| 10265 | + | |
| 10266 | + | |
| 10267 | + | |
| 10268 | + | |
| 10269 | + | |
| 10270 | + | |
10234 | 10271 |
| |
10235 | 10272 |
| |
10236 | 10273 |
| |
| |||
10261 | 10298 |
| |
10262 | 10299 |
| |
10263 | 10300 |
| |
10264 |
| - | |
| 10301 | + | |
10265 | 10302 |
| |
10266 |
| - | |
10267 |
| - | |
10268 |
| - | |
| 10303 | + | |
| 10304 | + | |
| 10305 | + | |
| 10306 | + | |
| 10307 | + | |
| 10308 | + | |
| 10309 | + | |
| 10310 | + | |
| 10311 | + | |
10269 | 10312 |
| |
10270 | 10313 |
| |
10271 | 10314 |
| |
| |||
10276 | 10319 |
| |
10277 | 10320 |
| |
10278 | 10321 |
| |
| 10322 | + | |
| 10323 | + | |
| 10324 | + | |
| 10325 | + | |
10279 | 10326 |
| |
10280 |
| - | |
| 10327 | + | |
10281 | 10328 |
| |
10282 | 10329 |
| |
10283 | 10330 |
| |
| |||
10288 | 10335 |
| |
10289 | 10336 |
| |
10290 | 10337 |
| |
| 10338 | + | |
| 10339 | + | |
| 10340 | + | |
10291 | 10341 |
| |
10292 | 10342 |
| |
| 10343 | + | |
10293 | 10344 |
| |
10294 | 10345 |
| |
10295 | 10346 |
| |
|
Lines changed: 55 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2396 | 2396 |
| |
2397 | 2397 |
| |
2398 | 2398 |
| |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
2399 | 2405 |
| |
2400 | 2406 |
| |
2401 | 2407 |
| |
| |||
2409 | 2415 |
| |
2410 | 2416 |
| |
2411 | 2417 |
| |
| 2418 | + | |
| 2419 | + | |
| 2420 | + | |
| 2421 | + | |
| 2422 | + | |
| 2423 | + | |
| 2424 | + | |
| 2425 | + | |
| 2426 | + | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
2412 | 2455 |
| |
2413 | 2456 |
| |
2414 | 2457 |
| |
| |||
2545 | 2588 |
| |
2546 | 2589 |
| |
2547 | 2590 |
| |
| 2591 | + | |
| 2592 | + | |
| 2593 | + | |
| 2594 | + | |
| 2595 | + | |
| 2596 | + | |
| 2597 | + | |
| 2598 | + | |
| 2599 | + | |
| 2600 | + | |
| 2601 | + | |
| 2602 | + | |
2548 | 2603 |
| |
2549 | 2604 |
| |
2550 | 2605 |
| |
|
Lines changed: 11 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1126 | 1126 |
| |
1127 | 1127 |
| |
1128 | 1128 |
| |
| 1129 | + | |
1129 | 1130 |
| |
1130 | 1131 |
| |
1131 | 1132 |
| |
1132 | 1133 |
| |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
1133 | 1142 |
| |
1134 | 1143 |
| |
1135 | 1144 |
| |
| |||
1172 | 1181 |
| |
1173 | 1182 |
| |
1174 | 1183 |
| |
| 1184 | + | |
| 1185 | + | |
1175 | 1186 |
| |
1176 | 1187 |
| |
1177 | 1188 |
| |
|
0 commit comments
Comments
(0)