pg.ddx.io pgsql-committers@postgresql.org mailing list archive
help / color / mirror / Atom feedpgsql: Fix corner-case errors and loss of precision in numeric_power().
7+ messages / 1 participants
[nested] [flat]
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/4dd5ce2fd9b9b2134886fee7fe4cb36d1988adb2
Modified Files
--------------
src/backend/utils/adt/numeric.c | 81 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 ++++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 15 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
REL_14_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/0d6b87497e32a5b15be8fa3247b8fae48358bb1b
Modified Files
--------------
src/backend/utils/adt/numeric.c | 81 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 ++++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 15 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
REL_13_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/053ec4e0c4e8027bc084d25a846e35ba84ae966c
Modified Files
--------------
src/backend/utils/adt/numeric.c | 82 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 +++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 16 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
REL_12_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/5c62920fa938644935418226872b360f90e74b62
Modified Files
--------------
src/backend/utils/adt/numeric.c | 82 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 +++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 16 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
REL_11_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/dcd0ab6729350c68681f115434054490f523956a
Modified Files
--------------
src/backend/utils/adt/numeric.c | 82 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 +++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 16 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
REL_10_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/39b0bda67007468ded0a2f236672a02c836ab759
Modified Files
--------------
src/backend/utils/adt/numeric.c | 82 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 +++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 16 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
* pgsql: Fix corner-case errors and loss of precision in numeric_power().
@ 2021-07-31 10:35 Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Dean Rasheed @ 2021-07-31 10:35 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers 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 only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to 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 absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:
local_rscale = rscale + (int) val - ln_dweight + 8
The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing 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 to
ensure 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 the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.
Dean Rasheed, reviewed by Yugo Nagata.
Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
Branch
------
REL9_6_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/5cf350ce02278da488e9eb4825a573d86d6d9fcc
Modified Files
--------------
src/backend/utils/adt/numeric.c | 82 ++++++++++++++++++++++++++++-------
src/test/regress/expected/numeric.out | 55 +++++++++++++++++++++++
src/test/regress/sql/numeric.sql | 11 +++++
3 files changed, 132 insertions(+), 16 deletions(-)
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2021-07-31 10:35 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
2021-07-31 10:35 pgsql: Fix corner-case errors and loss of precision in numeric_power(). Dean Rasheed <dean.a.rasheed@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox