Re: Float output formatting options - Mailing list pgsql-hackers

From Pedro M. Ferreira
Subject Re: Float output formatting options
Date
Msg-id 3DC6A57E.8090207@ualg.pt
Whole thread Raw
In response to Float output formatting options  ("Pedro M. Ferreira" <pfrazao@ualg.pt>)
Responses Re: Float output formatting options  (Tom Lane <tgl@sss.pgh.pa.us>)
Geometry regression tests (was Re: Float output formatting options)  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Tom Lane wrote:
> "Pedro M. Ferreira" <pfrazao@ualg.pt> writes:
 >>
>>If extra_float_digits==-13 and we are outputing a float4 this results in
>>a negative value for FLT_DIG+extra_float_digits.
>
> You would want to clamp the values passed to %g to not less than 1.
> I'd favor code like
>     int    ndig = FLT_DIG + extra_float_digits;
>     if (ndig < 1)
>         ndig = 1;
>     sprintf(ascii, "%.*g", ndig, num);
>
> Probably best to do it this way with float8 too; otherwise we're
> essentially wiring in the assumption that we know what DBL_DIG is.
> Which is exactly what we're trying to avoid doing.

Good.
Corrected this, compiled and tested it. Works fine.

I am attaching the diff's made with diff -u. Sources were from Debian
source package Version 7.2.1-2woody2.

Best regards,
Pedro

>
>             regards, tom lane
>
>


--
----------------------------------------------------------------------
Pedro Miguel Frazao Fernandes Ferreira
Universidade do Algarve
Faculdade de Ciencias e Tecnologia
Campus de Gambelas
8000-117 Faro
Portugal
Tel./Fax:  (+351) 289 800950 / 289 819403
http://w3.ualg.pt/~pfrazao
--- old/postgresql-7.2.1/src/backend/utils/adt/float.c  Tue Dec 11 02:02:12 2001
+++ postgresql-7.2.1/src/backend/utils/adt/float.c      Mon Nov  4 15:50:19 2002
@@ -65,6 +65,11 @@
 #include "utils/array.h"
 #include "utils/builtins.h"

+/*
+ *     Configuration options for float4 and float8 extra digits in output format
+ */
+
+int extra_float_digits;

 #if !(NeXT && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_2)
  /* NS3.3 has conflicting declarations of these in <math.h> */
@@ -228,6 +233,7 @@
        float4          num = PG_GETARG_FLOAT4(0);
        char       *ascii = (char *) palloc(MAXFLOATWIDTH + 1);
        int                     infflag;
+       int     ndig = FLT_DIG + extra_float_digits;

        if (isnan(num))
                PG_RETURN_CSTRING(strcpy(ascii, "NaN"));
@@ -237,7 +243,10 @@
        if (infflag < 0)
                PG_RETURN_CSTRING(strcpy(ascii, "-Infinity"));

-       sprintf(ascii, "%.*g", FLT_DIG, num);
+       if (ndig < 1)
+         ndig = 1;
+
+       sprintf(ascii, "%.*g", ndig, num);
        PG_RETURN_CSTRING(ascii);
 }

@@ -290,6 +299,7 @@
        float8          num = PG_GETARG_FLOAT8(0);
        char       *ascii = (char *) palloc(MAXDOUBLEWIDTH + 1);
        int                     infflag;
+       int     ndig = DBL_DIG + extra_float_digits;

        if (isnan(num))
                PG_RETURN_CSTRING(strcpy(ascii, "NaN"));
@@ -299,7 +309,10 @@
        if (infflag < 0)
                PG_RETURN_CSTRING(strcpy(ascii, "-Infinity"));

-       sprintf(ascii, "%.*g", DBL_DIG, num);
+        if (ndig < 1)
+               ndig = 1;
+
+       sprintf(ascii, "%.*g", ndig, num);
        PG_RETURN_CSTRING(ascii);
 }

--- old/postgresql-7.2.1/src/backend/utils/misc/guc.c   Tue Oct 30 05:38:56 2001
+++ postgresql-7.2.1/src/backend/utils/misc/guc.c       Mon Nov  4 15:41:24 2002
@@ -49,6 +49,10 @@
 extern int     CommitSiblings;
 extern bool FixBTree;

+/* For float4 and float8 extra digits in output format */
+
+extern int extra_float_digits;
+
 #ifdef ENABLE_SYSLOG
 extern char *Syslog_facility;
 extern char *Syslog_ident;
@@ -502,6 +506,14 @@
                "commit_siblings", PGC_USERSET, &CommitSiblings,
                5, 1, 1000, NULL, NULL
        },
+
+       /*
+        * For float4 and float8 extra digits in output format
+        */
+       {
+               "float_extra_digits", PGC_USERSET, &extra_float_digits,
+               0, -13, 2, NULL, NULL
+       },

        {
                NULL, 0, NULL, 0, 0, 0, NULL, NULL
--- old/postgresql-7.2.1/src/bin/psql/tab-complete.c    Mon Nov  5 17:46:31 2001
+++ postgresql-7.2.1/src/bin/psql/tab-complete.c        Mon Nov  4 15:38:52 2002
@@ -266,6 +266,8 @@

                "default_transaction_isolation",

+               "float_extra_digits",
+
                NULL
        };

--- old/postgresql-7.2.1/src/backend/utils/misc/postgresql.conf.sample  Fri Jan  4 05:50:25 2002
+++ postgresql-7.2.1/src/backend/utils/misc/postgresql.conf.sample      Mon Nov  4 15:39:55 2002
@@ -182,3 +182,8 @@
 #password_encryption = false
 #sql_inheritance = true
 #transform_null_equals = false
+
+#
+# floating point extra digits in output formatting
+#
+#extra_float_digits = 0                #min -13, max 2

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Float output formatting options
Next
From: Tom Lane
Date:
Subject: Re: Float output formatting options