\set ECHO none NOTICE: Percentiles are monotone and in range NOTICE: Scalar and array percentiles agree NOTICE: Percentiles are monotone and in range NOTICE: Scalar and array percentiles agree NOTICE: Percentiles are monotone and in range NOTICE: Scalar and array percentiles agree NOTICE: Percentiles are monotone and in range NOTICE: Scalar and array percentiles agree NOTICE: Percentiles are monotone and in range NOTICE: Scalar and array percentiles agree NOTICE: Percentiles are monotone and in range NOTICE: Scalar and array percentiles agree NOTICE: Raw and weighted percentiles are monotone NOTICE: Raw and weighted percentiles are monotone NOTICE: Inverse interpolation is monotone NOTICE: Scalar and array inverse percentiles agree -- At p = 0.75 - 2^-53, q = 1 - 2^-52 and the result rounds to -5. -- Subtracting nearly equal magnitudes used to return -4 instead. SELECT tdigest_percentile(v, 10000, 0.7499999999999999::double precision) AS raw_scalar, tdigest_percentile(v, 10000, ARRAY[0.7499999999999999]::double precision[]) AS raw_array, tdigest_percentile(v, 1::bigint, 10000, 0.7499999999999999::double precision) AS weighted_scalar, tdigest_percentile(v, 1::bigint, 10000, ARRAY[0.7499999999999999]::double precision[]) AS weighted_array FROM (VALUES (-18014398509481984::double precision), (-1::double precision)) AS input(v); raw_scalar | raw_array | weighted_scalar | weighted_array ------------+-----------+-----------------+---------------- -5 | {-5} | -5 | {-5} (1 row) -- Also cover stored digests and count rounding that makes q exactly 1. -- The endpoint case used to return -4, inside the bracket but not equal to -3. WITH inputs(label, d, p) AS ( VALUES ( 'precision', 'flags 1 count 2 compression 10000 centroids 2 (-18014398509481984, 1) (-1, 1)'::tdigest, 0.7499999999999999::double precision ), ( 'endpoint', 'flags 1 count 9007199254743615 compression 10000 centroids 3 (-10000000000000000, 9007199254740839) (-3, 290) (-1, 2486)'::tdigest, 0.9999999999997077::double precision ) ) SELECT label, tdigest_percentile(d, p) AS scalar_result, tdigest_percentile(d, ARRAY[p]) AS array_result FROM inputs GROUP BY label ORDER BY label; label | scalar_result | array_result -----------+---------------+-------------- endpoint | -3 | {-3} precision | -5 | {-5} (2 rows)