Thread

Commits

  1. Fix division-by-zero error in to_char() with 'EEEE' format.

  1. Division by zero error in to_char(num, '9.9EEEE')

    Dean Rasheed <dean.a.rasheed@gmail.com> — 2021-07-30T07:26:14Z

    While testing the numeric_power() patch in [1], I found this problem
    trying to use to_char() to format very small numbers:
    
    SELECT to_char(1.2e-1001, '9.9EEEE'); -- OK
      to_char
    ------------
      1.2e-1001
    
    SELECT to_char(1.2e-1002, '9.9EEEE'); -- fails
    ERROR:  division by zero
    
    It turns out that the problem is in get_str_from_var_sci() which
    attempts to divide the input by 1e-1002 to get the significand.
    However, it is using power_var_int() to compute 1e-1002, which has a
    maximum rscale of NUMERIC_MAX_DISPLAY_SCALE (1000), so it returns 0,
    which is the correct answer to that scale, and then
    get_str_from_var_sci() attempts to divide by that.
    
    Rather than messing with power_var_int(), I think the simplest
    solution is to just introduce a new local function, as in the attached
    patch. This directly constructs 10^n, for integer n, which is pretty
    trivial, and doesn't need any numeric multiplication or rounding.
    
    Regards,
    Dean
    
    [1] https://www.postgresql.org/message-id/CAEZATCUWUV_BP41Ob7QY12oF%2BqDxjTWfDpkdkcOOuojrDvOLxw%40mail.gmail.com
    
  2. Re: Division by zero error in to_char(num, '9.9EEEE')

    Dean Rasheed <dean.a.rasheed@gmail.com> — 2021-08-04T07:38:49Z

    On Fri, 30 Jul 2021 at 08:26, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
    >
    > SELECT to_char(1.2e-1002, '9.9EEEE'); -- fails
    > ERROR:  division by zero
    >
    > I think the simplest
    > solution is to just introduce a new local function, as in the attached
    > patch. This directly constructs 10^n, for integer n, which is pretty
    > trivial, and doesn't need any numeric multiplication or rounding.
    >
    
    Unless there are any objections, I intend to push this shortly. I
    think it's a fairly straightforward bug fix, and I want to be able to
    use to_char() in some new numeric regression tests.
    
    Regards,
    Dean