Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdacW-0000h2-0A for pgsql-novice@arkaria.postgresql.org; Thu, 21 Oct 2021 16:08:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mdacU-0004UZ-Sp for pgsql-novice@arkaria.postgresql.org; Thu, 21 Oct 2021 16:08:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdZxt-00033c-Sy for pgsql-novice@lists.postgresql.org; Thu, 21 Oct 2021 15:26:45 +0000 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mdZxr-0007fo-LE for pgsql-novice@lists.postgresql.org; Thu, 21 Oct 2021 15:26:44 +0000 Received: by mail-il1-x129.google.com with SMTP id y17so1037522ilb.9 for ; Thu, 21 Oct 2021 08:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=gMhLT8mb8CXu0E4S+3EYfWAKcoDUrt1Yqq46GUBniD0=; b=SloDddIgIP/pGlkTcLvwUhcVxh+ucC7ob+ZwYReZNhkPBE/EKhgQb/R0gW54o9p6jw xZQgfVq7KuoESbVs2QuWTRbWH6FK4eRDZwRElIOtEOG5CxQTuJtmPEPynecFowYU6c7y sYlFZgd50EtjZ443rZX3RrbhK/nTNLJhEpwfR71QejgjqcZzI7cPhI3XT29oH3aFhv0F k+73K/MWfAOAHTBUts/3jhhKIOy65mwa3uW5MLMvOP0gTAysLm5WyVJd/1IosUdl/9fa LHS3ABzqLMR9pMtYUmQlErYnE+f5NvqCFcegqRQNykMuwUgSC5ZOgR9oRIKyWfa9v6/Z Amqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=gMhLT8mb8CXu0E4S+3EYfWAKcoDUrt1Yqq46GUBniD0=; b=TNzbX+4I3Od23i6Yfyu++njzbfu+GPXl/ju4pbXqPXpf8v5VN9nQC7HZNBNOvwCXM6 tlGTwj14BIJQcOdtL+h2cOOCyozgjTdOCqNBt3aYXaoHMXsp2bHbnVG3VP58/UjJe8DM yVUIhuEMuYBdO+n5HJ899qZHb7kPE/OL1gf1sAuz3F8KVhlKv2rJkCuaFZjEG+jYL0GW n7y+zmTEP3oDzf20P2CccvYxoFFtyOqAEQym+mGfZXH1Qj5kAHoFKbByohsBzt8HFP2/ LxpMOJ4nQYz/wSZiYEKcnWSy2yK9+8lUU+TVzuzvL7GO8t39V/qtSzzQ+Mdk2A+Tx3fW IAJg== X-Gm-Message-State: AOAM533+LkfJhFut+qMcVaKuBoJckO5KMorpHs6t8wYHYgv1WvoAEPWa T8a1zPkzPI5/SlKiLDQc0wa5n9nHzcaynMIYzcg= X-Google-Smtp-Source: ABdhPJxkh+PT8QnmV3VX34iO0HPDzf190uGab4tL2brEUi0+En5F4z3Vdtp6CHwG0tDVdXDoor4bOkxewm2dKajH63c= X-Received: by 2002:a05:6e02:8b4:: with SMTP id a20mr4148089ilt.315.1634830002611; Thu, 21 Oct 2021 08:26:42 -0700 (PDT) MIME-Version: 1.0 References: <2404795.1634825164@sss.pgh.pa.us> In-Reply-To: <2404795.1634825164@sss.pgh.pa.us> From: Nuno Sousa Date: Thu, 21 Oct 2021 16:26:26 +0100 Message-ID: Subject: Re: Numeric Scale Differences To: Tom Lane Cc: pgsql-novice@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000092eca705cede8575" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000092eca705cede8575 Content-Type: text/plain; charset="UTF-8" Hi Tom, Thanks for the quick response. In that case, do you think it's worth documenting the implementation artifact behavior on the docs? -- Nuno Sousa On Thu, Oct 21, 2021 at 3:06 PM Tom Lane wrote: > Nuno Sousa writes: > > I've run into an interesting issue when trying to do numeric math. I > > expected the following to return the same number of decimal digits in the > > fractional part: > > > select scale(1/0.6::numeric), scale(1/6::numeric); > > Um ... why did you expect that? The inputs to the divisions have > different scales: > > # select scale(0.6::numeric), scale(6::numeric); > scale | scale > -------+------- > 1 | 0 > (1 row) > > so I find it unsurprising that the outputs do too. Now the fact > that the output scales differ by 4 not 1 is indeed an implementation > artifact. It stems from the numeric type working with base-10000 > digits, so that scales that are multiples of 4 are most efficient, > hence division will always choose such an output scale. > > regards, tom lane > --00000000000092eca705cede8575 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Tom,

Thanks for the quick=C2=A0respo= nse. In that case, do you think it's worth documenting the implementati= on artifact behavior on the docs?
--
Nuno Sousa
<= /div>

On Thu, Oct 21, 2021 at 3:06 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Nuno Sousa <nunofgs@gmail.com> writes:
> I've run into an interesting issue when trying to do numeric math.= I
> expected the following to return the same number of decimal digits in = the
> fractional part:

> select scale(1/0.6::numeric), scale(1/6::numeric);

Um ... why did you expect that?=C2=A0 The inputs to the divisions have
different scales:

# select scale(0.6::numeric), scale(6::numeric);
=C2=A0scale | scale
-------+-------
=C2=A0 =C2=A0 =C2=A01 |=C2=A0 =C2=A0 =C2=A00
(1 row)

so I find it unsurprising that the outputs do too.=C2=A0 Now the fact
that the output scales differ by 4 not 1 is indeed an implementation
artifact.=C2=A0 It stems from the numeric type working with base-10000
digits, so that scales that are multiples of 4 are most efficient,
hence division will always choose such an output scale.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--00000000000092eca705cede8575--