Re: [PATCH] Normalize numeric/bigint scientific notation after rounding

From: Jianqiao Han <hanjianqiao(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Normalize numeric/bigint scientific notation after rounding
Date: 2026-11-16 00:00:00
Message-ID: CAPOa_Z=8-LXePZdtnKnhVG0O95p+WMoGri-707fymZJTh8Cy9A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, 14 Nov 2026 at 02:28, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> =?UTF-8?B?6Z+p5bu65qGl?= <hanjianqiao(at)gmail(dot)com> writes:
> > While working with PG, I found that to_char returns value that is
> > right but the format is wrong:
> > SELECT to_char(999::numeric, '9.9EEEE');
> > Result is: 10.0e+02, and I think the expected output should be: 1.0e+03.
> > With help from ChatGPT, I figured that get_str_from_var_sci()
> > determines the exponent
> > before rounding the significand. If rounding increases the
> > significand's absolute value to
> > 10, the original exponent is still used. The attached patch adds a
> > check for this,and adjusts
> > the significand and the exponent.
>
> Good point. The patch feels a little brute-force, but I don't see
> a simple way to do better, and it's probably not worth a huge
> amount of trouble to make this edge case faster.
>
> Is there any case in which we have the reverse problem, ie rounding
> down causes the leading digit to become 0?
>
> regards, tom lane

Thanks for the review. The patch handles the tmp_var calculated by
following two function calls:

power_ten_int(exponent, &tmp_var);
// rscale is always >= 0
// for a non-zero var: var / 10^exponent is in [1, 10), with round=true the
// range is [1, 10] ie. 9.99 will be rounded to 10.0
div_var(var, &tmp_var, &tmp_var, rscale, true, true);

And the patch increases exponent only when tmp_var.digits[0] >= 10.
So for a non-zero input the tmp_var.digits[0] within range [1, 10] will
not be rounded to zero, and I cannot think any other type of reverse
problems will occur here.

Regards,
Jianqiao Han

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ayoub Kazar 2026-11-16 00:27:21 Re: CREATE SCHEMA still resolves unqualified type names in pg_temp
Previous Message Zsolt Parragi 2026-11-15 21:22:59 Re: Re: Assertion failure in WAL summarizer