LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - float.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: c38de3ae6098ccf1904109f2e550eb4777f66024 vs e352ac8c5c90ed6ce792f044c22145578542a330 Lines: 83.8 % 1436 1203 1 232 5 1198 1 5
Current Date: 2026-11-05 14:14:01 +0100 Functions: 91.2 % 160 146 14 2 144 2
Baseline: lcov-20261105-baseline Branches: 65.6 % 912 598 314 598
Baseline Date: 2026-11-05 14:14:01 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 83.3 % 6 5 1 5
(30,360] days: 93.3 % 105 98 7 98
(360..) days: 83.0 % 1325 1100 225 1100
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 100.0 % 3 3 3
(360..) days: 91.0 % 155 141 14 141
Branch coverage date bins:
(30,360] days: 71.2 % 80 57 23 57
(360..) days: 65.0 % 832 541 291 541

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * float.c
                                  4                 :                :  *    Functions for the built-in floating-point types.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/utils/adt/float.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include <ctype.h>
                                 18                 :                : #include <float.h>
                                 19                 :                : #include <math.h>
                                 20                 :                : #include <limits.h>
                                 21                 :                : 
                                 22                 :                : #include "catalog/pg_type.h"
                                 23                 :                : #include "common/int.h"
                                 24                 :                : #include "common/shortest_dec.h"
                                 25                 :                : #include "libpq/pqformat.h"
                                 26                 :                : #include "utils/array.h"
                                 27                 :                : #include "utils/float.h"
                                 28                 :                : #include "utils/fmgrprotos.h"
                                 29                 :                : #include "utils/sortsupport.h"
                                 30                 :                : 
                                 31                 :                : 
                                 32                 :                : /*
                                 33                 :                :  * Reject building with gcc's -ffast-math switch.  It breaks our handling of
                                 34                 :                :  * float Infinity and NaN values (via -ffinite-math-only), causes results to
                                 35                 :                :  * be less accurate than expected (via -funsafe-math-optimizations and
                                 36                 :                :  * -fexcess-precision=fast), and causes some math error reports to be missed
                                 37                 :                :  * (via -fno-math-errno).  Unfortunately we can't easily detect cases where
                                 38                 :                :  * those options were given individually, but this at least catches the most
                                 39                 :                :  * obvious case.
                                 40                 :                :  *
                                 41                 :                :  * We test this only here, not in any header file, to allow extensions to use
                                 42                 :                :  * -ffast-math if they need to.  But the inline functions in float.h will
                                 43                 :                :  * misbehave in such an extension, so its authors had better be careful.
                                 44                 :                :  */
                                 45                 :                : #ifdef __FAST_MATH__
                                 46                 :                : #error -ffast-math is known to break this code
                                 47                 :                : #endif
                                 48                 :                : 
                                 49                 :                : /*
                                 50                 :                :  * Configurable GUC parameter
                                 51                 :                :  *
                                 52                 :                :  * If >0, use shortest-decimal format for output; this is both the default and
                                 53                 :                :  * allows for compatibility with clients that explicitly set a value here to
                                 54                 :                :  * get round-trip-accurate results. If 0 or less, then use the old, slow,
                                 55                 :                :  * decimal rounding method.
                                 56                 :                :  */
                                 57                 :                : int         extra_float_digits = 1;
                                 58                 :                : 
                                 59                 :                : /* Cached constants for degree-based trig functions */
                                 60                 :                : static bool degree_consts_set = false;
                                 61                 :                : static float8 sin_30 = 0;
                                 62                 :                : static float8 one_minus_cos_60 = 0;
                                 63                 :                : static float8 asin_0_5 = 0;
                                 64                 :                : static float8 acos_0_5 = 0;
                                 65                 :                : static float8 atan_1_0 = 0;
                                 66                 :                : static float8 tan_45 = 0;
                                 67                 :                : static float8 cot_45 = 0;
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * These are intentionally not static; don't "fix" them.  They will never
                                 71                 :                :  * be referenced by other files, much less changed; but we don't want the
                                 72                 :                :  * compiler to know that, else it might try to precompute expressions
                                 73                 :                :  * involving them.  See comments for init_degree_constants().
                                 74                 :                :  *
                                 75                 :                :  * The additional extern declarations are to silence
                                 76                 :                :  * -Wmissing-variable-declarations.
                                 77                 :                :  */
                                 78                 :                : extern float8 degree_c_thirty;
                                 79                 :                : extern float8 degree_c_forty_five;
                                 80                 :                : extern float8 degree_c_sixty;
                                 81                 :                : extern float8 degree_c_one_half;
                                 82                 :                : extern float8 degree_c_one;
                                 83                 :                : float8      degree_c_thirty = 30.0;
                                 84                 :                : float8      degree_c_forty_five = 45.0;
                                 85                 :                : float8      degree_c_sixty = 60.0;
                                 86                 :                : float8      degree_c_one_half = 0.5;
                                 87                 :                : float8      degree_c_one = 1.0;
                                 88                 :                : 
                                 89                 :                : /* Local function prototypes */
                                 90                 :                : static double sind_q1(double x);
                                 91                 :                : static double cosd_q1(double x);
                                 92                 :                : static void init_degree_constants(void);
                                 93                 :                : 
                                 94                 :                : 
                                 95                 :                : /*
                                 96                 :                :  * We use these out-of-line ereport() calls to report float overflow,
                                 97                 :                :  * underflow, and zero-divide, because following our usual practice of
                                 98                 :                :  * repeating them at each call site would lead to a lot of code bloat.
                                 99                 :                :  *
                                100                 :                :  * This does mean that you don't get a useful error location indicator.
                                101                 :                :  */
                                102                 :                : pg_noinline void
 2457 tgl@sss.pgh.pa.us         103                 :CBC          32 : float_overflow_error(void)
                                104                 :                : {
                                105         [ +  - ]:             32 :     ereport(ERROR,
                                106                 :                :             (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                107                 :                :              errmsg("value out of range: overflow")));
                                108                 :                : }
                                109                 :                : 
                                110                 :                : pg_noinline void
                                111                 :              8 : float_underflow_error(void)
                                112                 :                : {
                                113         [ +  - ]:              8 :     ereport(ERROR,
                                114                 :                :             (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                115                 :                :              errmsg("value out of range: underflow")));
                                116                 :                : }
                                117                 :                : 
                                118                 :                : pg_noinline void
                                119                 :              4 : float_zero_divide_error(void)
                                120                 :                : {
                                121         [ +  - ]:              4 :     ereport(ERROR,
                                122                 :                :             (errcode(ERRCODE_DIVISION_BY_ZERO),
                                123                 :                :              errmsg("division by zero")));
                                124                 :                : }
                                125                 :                : 
                                126                 :                : float8
  221 peter@eisentraut.org      127                 :             24 : float_overflow_error_ext(struct Node *escontext)
                                128                 :                : {
                                129         [ +  - ]:             24 :     ereturn(escontext, 0.0,
                                130                 :                :             errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                131                 :                :             errmsg("value out of range: overflow"));
                                132                 :                : }
                                133                 :                : 
                                134                 :                : float8
                                135                 :             12 : float_underflow_error_ext(struct Node *escontext)
                                136                 :                : {
                                137         [ +  - ]:             12 :     ereturn(escontext, 0.0,
                                138                 :                :             errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                139                 :                :             errmsg("value out of range: underflow"));
                                140                 :                : }
                                141                 :                : 
                                142                 :                : float8
                                143                 :             44 : float_zero_divide_error_ext(struct Node *escontext)
                                144                 :                : {
                                145         [ +  - ]:             44 :     ereturn(escontext, 0.0,
                                146                 :                :             (errcode(ERRCODE_DIVISION_BY_ZERO),
                                147                 :                :              errmsg("division by zero")));
                                148                 :                : }
                                149                 :                : 
                                150                 :                : 
                                151                 :                : /*
                                152                 :                :  * Returns -1 if 'val' represents negative infinity, 1 if 'val'
                                153                 :                :  * represents (positive) infinity, and 0 otherwise. On some platforms,
                                154                 :                :  * this is equivalent to the isinf() macro, but not everywhere: C99
                                155                 :                :  * does not specify that isinf() needs to distinguish between positive
                                156                 :                :  * and negative infinity.
                                157                 :                :  */
                                158                 :                : int
 8271 neilc@samurai.com         159                 :UBC           0 : is_infinite(double val)
                                160                 :                : {
 8103 bruce@momjian.us          161   [ #  #  #  # ]:              0 :     int         inf = isinf(val);
                                162                 :                : 
 8271 neilc@samurai.com         163         [ #  # ]:              0 :     if (inf == 0)
                                164                 :              0 :         return 0;
 7247 bruce@momjian.us          165         [ #  # ]:              0 :     else if (val > 0)
 8271 neilc@samurai.com         166                 :              0 :         return 1;
                                167                 :                :     else
 7247 bruce@momjian.us          168                 :              0 :         return -1;
                                169                 :                : }
                                170                 :                : 
                                171                 :                : 
                                172                 :                : /* ========== USER I/O ROUTINES ========== */
                                173                 :                : 
                                174                 :                : 
                                175                 :                : /*
                                176                 :                :  *      float4in        - converts "num" to float4
                                177                 :                :  *
                                178                 :                :  * Note that this code now uses strtof(), where it used to use strtod().
                                179                 :                :  *
                                180                 :                :  * The motivation for using strtof() is to avoid a double-rounding problem:
                                181                 :                :  * for certain decimal inputs, if you round the input correctly to a double,
                                182                 :                :  * and then round the double to a float, the result is incorrect in that it
                                183                 :                :  * does not match the result of rounding the decimal value to float directly.
                                184                 :                :  *
                                185                 :                :  * One of the best examples is 7.038531e-26:
                                186                 :                :  *
                                187                 :                :  * 0xAE43FDp-107 = 7.03853069185120912085...e-26
                                188                 :                :  *      midpoint   7.03853100000000022281...e-26
                                189                 :                :  * 0xAE43FEp-107 = 7.03853130814879132477...e-26
                                190                 :                :  *
                                191                 :                :  * making 0xAE43FDp-107 the correct float result, but if you do the conversion
                                192                 :                :  * via a double, you get
                                193                 :                :  *
                                194                 :                :  * 0xAE43FD.7FFFFFF8p-107 = 7.03853099999999907487...e-26
                                195                 :                :  *               midpoint   7.03853099999999964884...e-26
                                196                 :                :  * 0xAE43FD.80000000p-107 = 7.03853100000000022281...e-26
                                197                 :                :  * 0xAE43FD.80000008p-107 = 7.03853100000000137076...e-26
                                198                 :                :  *
                                199                 :                :  * so the value rounds to the double exactly on the midpoint between the two
                                200                 :                :  * nearest floats, and then rounding again to a float gives the incorrect
                                201                 :                :  * result of 0xAE43FEp-107.
                                202                 :                :  *
                                203                 :                :  */
                                204                 :                : Datum
 9592 tgl@sss.pgh.pa.us         205                 :CBC      428678 : float4in(PG_FUNCTION_ARGS)
                                206                 :                : {
                                207                 :         428678 :     char       *num = PG_GETARG_CSTRING(0);
                                208                 :                : 
 1415 andrew@dunslane.net       209                 :         428678 :     PG_RETURN_FLOAT4(float4in_internal(num, NULL, "real", num,
                                210                 :                :                                        fcinfo->context));
                                211                 :                : }
                                212                 :                : 
                                213                 :                : /*
                                214                 :                :  * float4in_internal - guts of float4in()
                                215                 :                :  *
                                216                 :                :  * This is exposed for use by functions that want a reasonably
                                217                 :                :  * platform-independent way of inputting floats. The behavior is
                                218                 :                :  * essentially like strtof + ereturn on error.
                                219                 :                :  *
                                220                 :                :  * Uses the same API as float8in_internal below, so most of its
                                221                 :                :  * comments also apply here, except regarding use in geometric types.
                                222                 :                :  */
                                223                 :                : float4
   17 peter@eisentraut.org      224                 :GNC      433909 : float4in_internal(const char *num, char **endptr_p,
                                225                 :                :                   const char *type_name, const char *orig_string,
                                226                 :                :                   struct Node *escontext)
                                227                 :                : {
                                228                 :                :     float       val;
                                229                 :                :     const char *endptr;
                                230                 :                : 
                                231                 :                :     /*
                                232                 :                :      * endptr points to the first character _after_ the sequence we recognized
                                233                 :                :      * as a valid floating point number. orig_string points to the original
                                234                 :                :      * input string.
                                235                 :                :      */
                                236                 :                : 
                                237                 :                :     /* skip leading whitespace */
 4842 tgl@sss.pgh.pa.us         238   [ +  +  +  + ]:CBC      434049 :     while (*num != '\0' && isspace((unsigned char) *num))
                                239                 :            140 :         num++;
                                240                 :                : 
                                241                 :                :     /*
                                242                 :                :      * Check for an empty-string input to begin with, to avoid the vagaries of
                                243                 :                :      * strtod() on different platforms.
                                244                 :                :      */
 8271 neilc@samurai.com         245         [ +  + ]:         433909 :     if (*num == '\0')
 1415 andrew@dunslane.net       246         [ +  - ]:              8 :         ereturn(escontext, 0,
                                247                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                248                 :                :                  errmsg("invalid input syntax for type %s: \"%s\"",
                                249                 :                :                         type_name, orig_string)));
                                250                 :                : 
 7643 bruce@momjian.us          251                 :         433901 :     errno = 0;
   17 peter@eisentraut.org      252                 :GNC      433901 :     val = strtof(num, unconstify(char **, &endptr));
                                253                 :                : 
                                254                 :                :     /* did we not see anything that looks like a double? */
 8253 tgl@sss.pgh.pa.us         255   [ +  +  +  + ]:CBC      433901 :     if (endptr == num || errno != 0)
                                256                 :                :     {
 5261 bruce@momjian.us          257                 :            112 :         int         save_errno = errno;
                                258                 :                : 
                                259                 :                :         /*
                                260                 :                :          * C99 requires that strtof() accept NaN, [+-]Infinity, and [+-]Inf,
                                261                 :                :          * but not all platforms support all of these (and some accept them
                                262                 :                :          * but set ERANGE anyway...)  Therefore, we check for these inputs
                                263                 :                :          * ourselves if strtof() fails.
                                264                 :                :          *
                                265                 :                :          * Note: C99 also requires hexadecimal input as well as some extended
                                266                 :                :          * forms of NaN, but we consider these forms unportable and don't try
                                267                 :                :          * to support them.  You can use 'em if your strtof() takes 'em.
                                268                 :                :          */
 8217 tgl@sss.pgh.pa.us         269         [ -  + ]:            112 :         if (pg_strncasecmp(num, "NaN", 3) == 0)
                                270                 :                :         {
 8270 tgl@sss.pgh.pa.us         271                 :UBC           0 :             val = get_float4_nan();
 8271 neilc@samurai.com         272                 :              0 :             endptr = num + 3;
                                273                 :                :         }
 8217 tgl@sss.pgh.pa.us         274         [ -  + ]:CBC         112 :         else if (pg_strncasecmp(num, "Infinity", 8) == 0)
                                275                 :                :         {
 8270 tgl@sss.pgh.pa.us         276                 :UBC           0 :             val = get_float4_infinity();
 8271 neilc@samurai.com         277                 :              0 :             endptr = num + 8;
                                278                 :                :         }
 4842 tgl@sss.pgh.pa.us         279         [ -  + ]:CBC         112 :         else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
                                280                 :                :         {
 4842 tgl@sss.pgh.pa.us         281                 :UBC           0 :             val = get_float4_infinity();
                                282                 :              0 :             endptr = num + 9;
                                283                 :                :         }
 8217 tgl@sss.pgh.pa.us         284         [ -  + ]:CBC         112 :         else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
                                285                 :                :         {
 8103 bruce@momjian.us          286                 :UBC           0 :             val = -get_float4_infinity();
 8271 neilc@samurai.com         287                 :              0 :             endptr = num + 9;
                                288                 :                :         }
 4842 tgl@sss.pgh.pa.us         289         [ -  + ]:CBC         112 :         else if (pg_strncasecmp(num, "inf", 3) == 0)
                                290                 :                :         {
 4842 tgl@sss.pgh.pa.us         291                 :UBC           0 :             val = get_float4_infinity();
                                292                 :              0 :             endptr = num + 3;
                                293                 :                :         }
 4842 tgl@sss.pgh.pa.us         294         [ -  + ]:CBC         112 :         else if (pg_strncasecmp(num, "+inf", 4) == 0)
                                295                 :                :         {
 4842 tgl@sss.pgh.pa.us         296                 :UBC           0 :             val = get_float4_infinity();
                                297                 :              0 :             endptr = num + 4;
                                298                 :                :         }
 4842 tgl@sss.pgh.pa.us         299         [ -  + ]:CBC         112 :         else if (pg_strncasecmp(num, "-inf", 4) == 0)
                                300                 :                :         {
 4842 tgl@sss.pgh.pa.us         301                 :UBC           0 :             val = -get_float4_infinity();
                                302                 :              0 :             endptr = num + 4;
                                303                 :                :         }
 5391 tgl@sss.pgh.pa.us         304         [ +  + ]:CBC         112 :         else if (save_errno == ERANGE)
                                305                 :                :         {
                                306                 :                :             /*
                                307                 :                :              * Some platforms return ERANGE for denormalized numbers (those
                                308                 :                :              * that are not zero, but are too close to zero to have full
                                309                 :                :              * precision).  We'd prefer not to throw error for that, so try to
                                310                 :                :              * detect whether it's a "real" out-of-range condition by checking
                                311                 :                :              * to see if the result is zero or huge.
                                312                 :                :              */
 2822 rhodiumtoad@postgres      313   [ +  +  +  + ]:             47 :             if (val == 0.0 ||
                                314                 :                : #if !defined(HUGE_VALF)
                                315                 :                :                 isinf(val)
                                316                 :                : #else
                                317         [ +  + ]:             12 :                 (val >= HUGE_VALF || val <= -HUGE_VALF)
                                318                 :                : #endif
                                319                 :                :                 )
                                320                 :                :             {
                                321                 :                :                 /* see comments in float8in_internal for rationale */
 1415 andrew@dunslane.net       322                 :             43 :                 char       *errnumber = pstrdup(num);
                                323                 :                : 
                                324                 :             43 :                 errnumber[endptr - num] = '\0';
                                325                 :                : 
                                326         [ +  + ]:             43 :                 ereturn(escontext, 0,
                                327                 :                :                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                328                 :                :                          errmsg("\"%s\" is out of range for type real",
                                329                 :                :                                 errnumber)));
                                330                 :                :             }
                                331                 :                :         }
                                332                 :                :         else
                                333         [ +  + ]:             65 :             ereturn(escontext, 0,
                                334                 :                :                     (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                335                 :                :                      errmsg("invalid input syntax for type %s: \"%s\"",
                                336                 :                :                             type_name, orig_string)));
                                337                 :                :     }
                                338                 :                : 
                                339                 :                :     /* skip trailing whitespace */
 8253 tgl@sss.pgh.pa.us         340   [ +  +  +  + ]:         433925 :     while (*endptr != '\0' && isspace((unsigned char) *endptr))
 8274 neilc@samurai.com         341                 :            132 :         endptr++;
                                342                 :                : 
                                343                 :                :     /* report stopping point if wanted, else complain if not end of string */
 1415 andrew@dunslane.net       344         [ -  + ]:         433793 :     if (endptr_p)
   17 peter@eisentraut.org      345                 :UNC           0 :         *endptr_p = unconstify(char *, endptr);
 1415 andrew@dunslane.net       346         [ +  + ]:CBC      433793 :     else if (*endptr != '\0')
                                347         [ +  - ]:             24 :         ereturn(escontext, 0,
                                348                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                349                 :                :                  errmsg("invalid input syntax for type %s: \"%s\"",
                                350                 :                :                         type_name, orig_string)));
                                351                 :                : 
                                352                 :         433769 :     return val;
                                353                 :                : }
                                354                 :                : 
                                355                 :                : /*
                                356                 :                :  *      float4out       - converts a float4 number to a string
                                357                 :                :  *                        using a standard output format
                                358                 :                :  */
                                359                 :                : Datum
 9592 tgl@sss.pgh.pa.us         360                 :         175513 : float4out(PG_FUNCTION_ARGS)
                                361                 :                : {
                                362                 :         175513 :     float4      num = PG_GETARG_FLOAT4(0);
 2950                           363                 :         175513 :     char       *ascii = (char *) palloc(32);
                                364                 :         175513 :     int         ndig = FLT_DIG + extra_float_digits;
                                365                 :                : 
 2822 rhodiumtoad@postgres      366         [ +  + ]:         175513 :     if (extra_float_digits > 0)
                                367                 :                :     {
                                368                 :         169063 :         float_to_shortest_decimal_buf(num, ascii);
                                369                 :         169063 :         PG_RETURN_CSTRING(ascii);
                                370                 :                :     }
                                371                 :                : 
 2950 tgl@sss.pgh.pa.us         372                 :           6450 :     (void) pg_strfromd(ascii, 32, ndig, num);
 9592                           373                 :           6450 :     PG_RETURN_CSTRING(ascii);
                                374                 :                : }
                                375                 :                : 
                                376                 :                : /*
                                377                 :                :  *      float4recv          - converts external binary format to float4
                                378                 :                :  */
                                379                 :                : Datum
 8581 tgl@sss.pgh.pa.us         380                 :UBC           0 : float4recv(PG_FUNCTION_ARGS)
                                381                 :                : {
                                382                 :              0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
                                383                 :                : 
                                384                 :              0 :     PG_RETURN_FLOAT4(pq_getmsgfloat4(buf));
                                385                 :                : }
                                386                 :                : 
                                387                 :                : /*
                                388                 :                :  *      float4send          - converts float4 to binary format
                                389                 :                :  */
                                390                 :                : Datum
 8581 tgl@sss.pgh.pa.us         391                 :CBC        4345 : float4send(PG_FUNCTION_ARGS)
                                392                 :                : {
                                393                 :           4345 :     float4      num = PG_GETARG_FLOAT4(0);
                                394                 :                :     StringInfoData buf;
                                395                 :                : 
                                396                 :           4345 :     pq_begintypsend(&buf);
                                397                 :           4345 :     pq_sendfloat4(&buf, num);
                                398                 :           4345 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
                                399                 :                : }
                                400                 :                : 
                                401                 :                : /*
                                402                 :                :  *      float8in        - converts "num" to float8
                                403                 :                :  */
                                404                 :                : Datum
 9592                           405                 :         455996 : float8in(PG_FUNCTION_ARGS)
                                406                 :                : {
                                407                 :         455996 :     char       *num = PG_GETARG_CSTRING(0);
                                408                 :                : 
 1427                           409                 :         455996 :     PG_RETURN_FLOAT8(float8in_internal(num, NULL, "double precision", num,
                                410                 :                :                                        fcinfo->context));
                                411                 :                : }
                                412                 :                : 
                                413                 :                : /*
                                414                 :                :  * float8in_internal - guts of float8in()
                                415                 :                :  *
                                416                 :                :  * This is exposed for use by functions that want a reasonably
                                417                 :                :  * platform-independent way of inputting doubles.  The behavior is
                                418                 :                :  * essentially like strtod + ereturn on error, but note the following
                                419                 :                :  * differences:
                                420                 :                :  * 1. Both leading and trailing whitespace are skipped.
                                421                 :                :  * 2. If endptr_p is NULL, we report error if there's trailing junk.
                                422                 :                :  * Otherwise, it's up to the caller to complain about trailing junk.
                                423                 :                :  * 3. In event of a syntax error, the report mentions the given type_name
                                424                 :                :  * and prints orig_string as the input; this is meant to support use of
                                425                 :                :  * this function with types such as "box" and "point", where what we are
                                426                 :                :  * parsing here is just a substring of orig_string.
                                427                 :                :  *
                                428                 :                :  * If escontext points to an ErrorSaveContext node, that is filled instead
                                429                 :                :  * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
                                430                 :                :  * to detect errors.
                                431                 :                :  */
                                432                 :                : float8
   17 peter@eisentraut.org      433                 :GNC      664151 : float8in_internal(const char *num, char **endptr_p,
                                434                 :                :                   const char *type_name, const char *orig_string,
                                435                 :                :                   struct Node *escontext)
                                436                 :                : {
                                437                 :                :     double      val;
                                438                 :                :     const char *endptr;
                                439                 :                : 
                                440                 :                :     /* skip leading whitespace */
 4842 tgl@sss.pgh.pa.us         441   [ +  +  +  + ]:CBC      665026 :     while (*num != '\0' && isspace((unsigned char) *num))
                                442                 :            875 :         num++;
                                443                 :                : 
                                444                 :                :     /*
                                445                 :                :      * Check for an empty-string input to begin with, to avoid the vagaries of
                                446                 :                :      * strtod() on different platforms.
                                447                 :                :      */
 8271 neilc@samurai.com         448         [ +  + ]:         664151 :     if (*num == '\0')
 1427 tgl@sss.pgh.pa.us         449         [ +  - ]:             12 :         ereturn(escontext, 0,
                                450                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                451                 :                :                  errmsg("invalid input syntax for type %s: \"%s\"",
                                452                 :                :                         type_name, orig_string)));
                                453                 :                : 
 7643 bruce@momjian.us          454                 :         664139 :     errno = 0;
   17 peter@eisentraut.org      455                 :GNC      664139 :     val = strtod(num, unconstify(char **, &endptr));
                                456                 :                : 
                                457                 :                :     /* did we not see anything that looks like a double? */
 8253 tgl@sss.pgh.pa.us         458   [ +  +  +  + ]:CBC      664139 :     if (endptr == num || errno != 0)
                                459                 :                :     {
 5261 bruce@momjian.us          460                 :            190 :         int         save_errno = errno;
                                461                 :                : 
                                462                 :                :         /*
                                463                 :                :          * C99 requires that strtod() accept NaN, [+-]Infinity, and [+-]Inf,
                                464                 :                :          * but not all platforms support all of these (and some accept them
                                465                 :                :          * but set ERANGE anyway...)  Therefore, we check for these inputs
                                466                 :                :          * ourselves if strtod() fails.
                                467                 :                :          *
                                468                 :                :          * Note: C99 also requires hexadecimal input as well as some extended
                                469                 :                :          * forms of NaN, but we consider these forms unportable and don't try
                                470                 :                :          * to support them.  You can use 'em if your strtod() takes 'em.
                                471                 :                :          */
 8217 tgl@sss.pgh.pa.us         472         [ -  + ]:            190 :         if (pg_strncasecmp(num, "NaN", 3) == 0)
                                473                 :                :         {
 8270 tgl@sss.pgh.pa.us         474                 :UBC           0 :             val = get_float8_nan();
 8271 neilc@samurai.com         475                 :              0 :             endptr = num + 3;
                                476                 :                :         }
 8217 tgl@sss.pgh.pa.us         477         [ -  + ]:CBC         190 :         else if (pg_strncasecmp(num, "Infinity", 8) == 0)
                                478                 :                :         {
 8270 tgl@sss.pgh.pa.us         479                 :UBC           0 :             val = get_float8_infinity();
 8271 neilc@samurai.com         480                 :              0 :             endptr = num + 8;
                                481                 :                :         }
 4842 tgl@sss.pgh.pa.us         482         [ -  + ]:CBC         190 :         else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
                                483                 :                :         {
 4842 tgl@sss.pgh.pa.us         484                 :UBC           0 :             val = get_float8_infinity();
                                485                 :              0 :             endptr = num + 9;
                                486                 :                :         }
 8217 tgl@sss.pgh.pa.us         487         [ -  + ]:CBC         190 :         else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
                                488                 :                :         {
 8103 bruce@momjian.us          489                 :UBC           0 :             val = -get_float8_infinity();
 8271 neilc@samurai.com         490                 :              0 :             endptr = num + 9;
                                491                 :                :         }
 4842 tgl@sss.pgh.pa.us         492         [ -  + ]:CBC         190 :         else if (pg_strncasecmp(num, "inf", 3) == 0)
                                493                 :                :         {
 4842 tgl@sss.pgh.pa.us         494                 :UBC           0 :             val = get_float8_infinity();
                                495                 :              0 :             endptr = num + 3;
                                496                 :                :         }
 4842 tgl@sss.pgh.pa.us         497         [ -  + ]:CBC         190 :         else if (pg_strncasecmp(num, "+inf", 4) == 0)
                                498                 :                :         {
 4842 tgl@sss.pgh.pa.us         499                 :UBC           0 :             val = get_float8_infinity();
                                500                 :              0 :             endptr = num + 4;
                                501                 :                :         }
 4842 tgl@sss.pgh.pa.us         502         [ -  + ]:CBC         190 :         else if (pg_strncasecmp(num, "-inf", 4) == 0)
                                503                 :                :         {
 4842 tgl@sss.pgh.pa.us         504                 :UBC           0 :             val = -get_float8_infinity();
                                505                 :              0 :             endptr = num + 4;
                                506                 :                :         }
 5391 tgl@sss.pgh.pa.us         507         [ +  + ]:CBC         190 :         else if (save_errno == ERANGE)
                                508                 :                :         {
                                509                 :                :             /*
                                510                 :                :              * Some platforms return ERANGE for denormalized numbers (those
                                511                 :                :              * that are not zero, but are too close to zero to have full
                                512                 :                :              * precision).  We'd prefer not to throw error for that, so try to
                                513                 :                :              * detect whether it's a "real" out-of-range condition by checking
                                514                 :                :              * to see if the result is zero or huge.
                                515                 :                :              *
                                516                 :                :              * On error, we intentionally complain about double precision not
                                517                 :                :              * the given type name, and we print only the part of the string
                                518                 :                :              * that is the current number.
                                519                 :                :              */
                                520   [ +  +  +  +  :             86 :             if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL)
                                              +  + ]
                                521                 :                :             {
 3872                           522                 :             71 :                 char       *errnumber = pstrdup(num);
                                523                 :                : 
                                524                 :             71 :                 errnumber[endptr - num] = '\0';
 1427                           525         [ +  + ]:             71 :                 ereturn(escontext, 0,
                                526                 :                :                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                527                 :                :                          errmsg("\"%s\" is out of range for type double precision",
                                528                 :                :                                 errnumber)));
                                529                 :                :             }
                                530                 :                :         }
                                531                 :                :         else
                                532         [ +  + ]:            104 :             ereturn(escontext, 0,
                                533                 :                :                     (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                534                 :                :                      errmsg("invalid input syntax for type %s: \"%s\"",
                                535                 :                :                             type_name, orig_string)));
                                536                 :                :     }
                                537                 :                : 
                                538                 :                :     /* skip trailing whitespace */
 8253                           539   [ +  +  +  + ]:         664224 :     while (*endptr != '\0' && isspace((unsigned char) *endptr))
 8274 neilc@samurai.com         540                 :            260 :         endptr++;
                                541                 :                : 
                                542                 :                :     /* report stopping point if wanted, else complain if not end of string */
 3872 tgl@sss.pgh.pa.us         543         [ +  + ]:         663964 :     if (endptr_p)
   17 peter@eisentraut.org      544                 :GNC      195679 :         *endptr_p = unconstify(char *, endptr);
 3872 tgl@sss.pgh.pa.us         545         [ +  + ]:CBC      468285 :     else if (*endptr != '\0')
 1427                           546         [ +  + ]:             28 :         ereturn(escontext, 0,
                                547                 :                :                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                548                 :                :                  errmsg("invalid input syntax for type %s: \"%s\"",
                                549                 :                :                         type_name, orig_string)));
                                550                 :                : 
 3872                           551                 :         663936 :     return val;
                                552                 :                : }
                                553                 :                : 
                                554                 :                : 
                                555                 :                : /*
                                556                 :                :  *      float8out       - converts float8 number to a string
                                557                 :                :  *                        using a standard output format
                                558                 :                :  */
                                559                 :                : Datum
 9592                           560                 :         519167 : float8out(PG_FUNCTION_ARGS)
                                561                 :                : {
                                562                 :         519167 :     float8      num = PG_GETARG_FLOAT8(0);
                                563                 :                : 
 3872                           564                 :         519167 :     PG_RETURN_CSTRING(float8out_internal(num));
                                565                 :                : }
                                566                 :                : 
                                567                 :                : /*
                                568                 :                :  * float8out_internal - guts of float8out()
                                569                 :                :  *
                                570                 :                :  * This is exposed for use by functions that want a reasonably
                                571                 :                :  * platform-independent way of outputting doubles.
                                572                 :                :  * The result is always palloc'd.
                                573                 :                :  */
                                574                 :                : char *
                                575                 :        2362121 : float8out_internal(double num)
                                576                 :                : {
 2950                           577                 :        2362121 :     char       *ascii = (char *) palloc(32);
                                578                 :        2362121 :     int         ndig = DBL_DIG + extra_float_digits;
                                579                 :                : 
 2822 rhodiumtoad@postgres      580         [ +  + ]:        2362121 :     if (extra_float_digits > 0)
                                581                 :                :     {
                                582                 :        2217084 :         double_to_shortest_decimal_buf(num, ascii);
                                583                 :        2217084 :         return ascii;
                                584                 :                :     }
                                585                 :                : 
 2950 tgl@sss.pgh.pa.us         586                 :         145037 :     (void) pg_strfromd(ascii, 32, ndig, num);
 3872                           587                 :         145037 :     return ascii;
                                588                 :                : }
                                589                 :                : 
                                590                 :                : /*
                                591                 :                :  *      float8recv          - converts external binary format to float8
                                592                 :                :  */
                                593                 :                : Datum
 8581                           594                 :             16 : float8recv(PG_FUNCTION_ARGS)
                                595                 :                : {
                                596                 :             16 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
                                597                 :                : 
                                598                 :             16 :     PG_RETURN_FLOAT8(pq_getmsgfloat8(buf));
                                599                 :                : }
                                600                 :                : 
                                601                 :                : /*
                                602                 :                :  *      float8send          - converts float8 to binary format
                                603                 :                :  */
                                604                 :                : Datum
                                605                 :           3437 : float8send(PG_FUNCTION_ARGS)
                                606                 :                : {
                                607                 :           3437 :     float8      num = PG_GETARG_FLOAT8(0);
                                608                 :                :     StringInfoData buf;
                                609                 :                : 
                                610                 :           3437 :     pq_begintypsend(&buf);
                                611                 :           3437 :     pq_sendfloat8(&buf, num);
                                612                 :           3437 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
                                613                 :                : }
                                614                 :                : 
                                615                 :                : 
                                616                 :                : /* ========== PUBLIC ROUTINES ========== */
                                617                 :                : 
                                618                 :                : 
                                619                 :                : /*
                                620                 :                :  *      ======================
                                621                 :                :  *      FLOAT4 BASE OPERATIONS
                                622                 :                :  *      ======================
                                623                 :                :  */
                                624                 :                : 
                                625                 :                : /*
                                626                 :                :  *      float4abs       - returns |arg1| (absolute value)
                                627                 :                :  */
                                628                 :                : Datum
 9592                           629                 :            570 : float4abs(PG_FUNCTION_ARGS)
                                630                 :                : {
                                631                 :            570 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                632                 :                : 
 1489 peter@eisentraut.org      633                 :            570 :     PG_RETURN_FLOAT4(fabsf(arg1));
                                634                 :                : }
                                635                 :                : 
                                636                 :                : /*
                                637                 :                :  *      float4um        - returns -arg1 (unary minus)
                                638                 :                :  */
                                639                 :                : Datum
 9592 tgl@sss.pgh.pa.us         640                 :             10 : float4um(PG_FUNCTION_ARGS)
                                641                 :                : {
                                642                 :             10 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                643                 :                :     float4      result;
                                644                 :                : 
 6469                           645                 :             10 :     result = -arg1;
 7247 bruce@momjian.us          646                 :             10 :     PG_RETURN_FLOAT4(result);
                                647                 :                : }
                                648                 :                : 
                                649                 :                : Datum
 9282 bruce@momjian.us          650                 :UBC           0 : float4up(PG_FUNCTION_ARGS)
                                651                 :                : {
                                652                 :              0 :     float4      arg = PG_GETARG_FLOAT4(0);
                                653                 :                : 
                                654                 :              0 :     PG_RETURN_FLOAT4(arg);
                                655                 :                : }
                                656                 :                : 
                                657                 :                : Datum
 9592 tgl@sss.pgh.pa.us         658                 :CBC          12 : float4larger(PG_FUNCTION_ARGS)
                                659                 :                : {
                                660                 :             12 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                661                 :             12 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                662                 :                :     float4      result;
                                663                 :                : 
 3021 tomas.vondra@postgre      664         [ +  + ]:             12 :     if (float4_gt(arg1, arg2))
 8499 tgl@sss.pgh.pa.us         665                 :              4 :         result = arg1;
                                666                 :                :     else
                                667                 :              8 :         result = arg2;
 9592                           668                 :             12 :     PG_RETURN_FLOAT4(result);
                                669                 :                : }
                                670                 :                : 
                                671                 :                : Datum
 9592 tgl@sss.pgh.pa.us         672                 :UBC           0 : float4smaller(PG_FUNCTION_ARGS)
                                673                 :                : {
                                674                 :              0 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                675                 :              0 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                676                 :                :     float4      result;
                                677                 :                : 
 3021 tomas.vondra@postgre      678         [ #  # ]:              0 :     if (float4_lt(arg1, arg2))
 8499 tgl@sss.pgh.pa.us         679                 :              0 :         result = arg1;
                                680                 :                :     else
                                681                 :              0 :         result = arg2;
 9592                           682                 :              0 :     PG_RETURN_FLOAT4(result);
                                683                 :                : }
                                684                 :                : 
                                685                 :                : /*
                                686                 :                :  *      ======================
                                687                 :                :  *      FLOAT8 BASE OPERATIONS
                                688                 :                :  *      ======================
                                689                 :                :  */
                                690                 :                : 
                                691                 :                : /*
                                692                 :                :  *      float8abs       - returns |arg1| (absolute value)
                                693                 :                :  */
                                694                 :                : Datum
 9592 tgl@sss.pgh.pa.us         695                 :CBC       48788 : float8abs(PG_FUNCTION_ARGS)
                                696                 :                : {
                                697                 :          48788 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                698                 :                : 
 7247 bruce@momjian.us          699                 :          48788 :     PG_RETURN_FLOAT8(fabs(arg1));
                                700                 :                : }
                                701                 :                : 
                                702                 :                : 
                                703                 :                : /*
                                704                 :                :  *      float8um        - returns -arg1 (unary minus)
                                705                 :                :  */
                                706                 :                : Datum
 9592 tgl@sss.pgh.pa.us         707                 :            211 : float8um(PG_FUNCTION_ARGS)
                                708                 :                : {
                                709                 :            211 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                710                 :                :     float8      result;
                                711                 :                : 
 6469                           712                 :            211 :     result = -arg1;
 9592                           713                 :            211 :     PG_RETURN_FLOAT8(result);
                                714                 :                : }
                                715                 :                : 
                                716                 :                : Datum
 9282 bruce@momjian.us          717                 :UBC           0 : float8up(PG_FUNCTION_ARGS)
                                718                 :                : {
                                719                 :              0 :     float8      arg = PG_GETARG_FLOAT8(0);
                                720                 :                : 
                                721                 :              0 :     PG_RETURN_FLOAT8(arg);
                                722                 :                : }
                                723                 :                : 
                                724                 :                : Datum
 9592 tgl@sss.pgh.pa.us         725                 :CBC        8584 : float8larger(PG_FUNCTION_ARGS)
                                726                 :                : {
                                727                 :           8584 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                728                 :           8584 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                729                 :                :     float8      result;
                                730                 :                : 
 3021 tomas.vondra@postgre      731         [ +  + ]:           8584 :     if (float8_gt(arg1, arg2))
 8499 tgl@sss.pgh.pa.us         732                 :           8157 :         result = arg1;
                                733                 :                :     else
                                734                 :            427 :         result = arg2;
 9592                           735                 :           8584 :     PG_RETURN_FLOAT8(result);
                                736                 :                : }
                                737                 :                : 
                                738                 :                : Datum
                                739                 :            768 : float8smaller(PG_FUNCTION_ARGS)
                                740                 :                : {
                                741                 :            768 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                742                 :            768 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                743                 :                :     float8      result;
                                744                 :                : 
 3021 tomas.vondra@postgre      745         [ +  + ]:            768 :     if (float8_lt(arg1, arg2))
 8499 tgl@sss.pgh.pa.us         746                 :            592 :         result = arg1;
                                747                 :                :     else
                                748                 :            176 :         result = arg2;
 9592                           749                 :            768 :     PG_RETURN_FLOAT8(result);
                                750                 :                : }
                                751                 :                : 
                                752                 :                : 
                                753                 :                : /*
                                754                 :                :  *      ====================
                                755                 :                :  *      ARITHMETIC OPERATORS
                                756                 :                :  *      ====================
                                757                 :                :  */
                                758                 :                : 
                                759                 :                : /*
                                760                 :                :  *      float4pl        - returns arg1 + arg2
                                761                 :                :  *      float4mi        - returns arg1 - arg2
                                762                 :                :  *      float4mul       - returns arg1 * arg2
                                763                 :                :  *      float4div       - returns arg1 / arg2
                                764                 :                :  */
                                765                 :                : Datum
                                766                 :             36 : float4pl(PG_FUNCTION_ARGS)
                                767                 :                : {
 6469                           768                 :             36 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                769                 :             36 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                770                 :                : 
 3021 tomas.vondra@postgre      771                 :             36 :     PG_RETURN_FLOAT4(float4_pl(arg1, arg2));
                                772                 :                : }
                                773                 :                : 
                                774                 :                : Datum
 9592 tgl@sss.pgh.pa.us         775                 :             12 : float4mi(PG_FUNCTION_ARGS)
                                776                 :                : {
                                777                 :             12 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                778                 :             12 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                779                 :                : 
 3021 tomas.vondra@postgre      780                 :             12 :     PG_RETURN_FLOAT4(float4_mi(arg1, arg2));
                                781                 :                : }
                                782                 :                : 
                                783                 :                : Datum
 9592 tgl@sss.pgh.pa.us         784                 :             24 : float4mul(PG_FUNCTION_ARGS)
                                785                 :                : {
                                786                 :             24 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                787                 :             24 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                788                 :                : 
 3021 tomas.vondra@postgre      789                 :             24 :     PG_RETURN_FLOAT4(float4_mul(arg1, arg2));
                                790                 :                : }
                                791                 :                : 
                                792                 :                : Datum
 9592 tgl@sss.pgh.pa.us         793                 :             36 : float4div(PG_FUNCTION_ARGS)
                                794                 :                : {
                                795                 :             36 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                796                 :             36 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                797                 :                : 
 3021 tomas.vondra@postgre      798                 :             36 :     PG_RETURN_FLOAT4(float4_div(arg1, arg2));
                                799                 :                : }
                                800                 :                : 
                                801                 :                : /*
                                802                 :                :  *      float8pl        - returns arg1 + arg2
                                803                 :                :  *      float8mi        - returns arg1 - arg2
                                804                 :                :  *      float8mul       - returns arg1 * arg2
                                805                 :                :  *      float8div       - returns arg1 / arg2
                                806                 :                :  */
                                807                 :                : Datum
 9592 tgl@sss.pgh.pa.us         808                 :          66598 : float8pl(PG_FUNCTION_ARGS)
                                809                 :                : {
                                810                 :          66598 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                811                 :          66598 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                812                 :                : 
 3021 tomas.vondra@postgre      813                 :          66598 :     PG_RETURN_FLOAT8(float8_pl(arg1, arg2));
                                814                 :                : }
                                815                 :                : 
                                816                 :                : Datum
 9592 tgl@sss.pgh.pa.us         817                 :           8308 : float8mi(PG_FUNCTION_ARGS)
                                818                 :                : {
                                819                 :           8308 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                820                 :           8308 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                821                 :                : 
 3021 tomas.vondra@postgre      822                 :           8308 :     PG_RETURN_FLOAT8(float8_mi(arg1, arg2));
                                823                 :                : }
                                824                 :                : 
                                825                 :                : Datum
 9592 tgl@sss.pgh.pa.us         826                 :        1051134 : float8mul(PG_FUNCTION_ARGS)
                                827                 :                : {
                                828                 :        1051134 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                829                 :        1051134 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                830                 :                : 
 3021 tomas.vondra@postgre      831                 :        1051134 :     PG_RETURN_FLOAT8(float8_mul(arg1, arg2));
                                832                 :                : }
                                833                 :                : 
                                834                 :                : Datum
 9592 tgl@sss.pgh.pa.us         835                 :          10058 : float8div(PG_FUNCTION_ARGS)
                                836                 :                : {
                                837                 :          10058 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                838                 :          10058 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                839                 :                : 
 3021 tomas.vondra@postgre      840                 :          10058 :     PG_RETURN_FLOAT8(float8_div(arg1, arg2));
                                841                 :                : }
                                842                 :                : 
                                843                 :                : 
                                844                 :                : /*
                                845                 :                :  *      ====================
                                846                 :                :  *      COMPARISON OPERATORS
                                847                 :                :  *      ====================
                                848                 :                :  */
                                849                 :                : 
                                850                 :                : /*
                                851                 :                :  *      float4{eq,ne,lt,le,gt,ge}       - float4/float4 comparison operations
                                852                 :                :  */
                                853                 :                : int
 9317 tgl@sss.pgh.pa.us         854                 :        9697547 : float4_cmp_internal(float4 a, float4 b)
                                855                 :                : {
 3021 tomas.vondra@postgre      856         [ +  + ]:        9697547 :     if (float4_gt(a, b))
                                857                 :         248481 :         return 1;
                                858         [ +  + ]:        9449066 :     if (float4_lt(a, b))
                                859                 :        1100174 :         return -1;
                                860                 :        8348892 :     return 0;
                                861                 :                : }
                                862                 :                : 
                                863                 :                : Datum
 9592 tgl@sss.pgh.pa.us         864                 :          36360 : float4eq(PG_FUNCTION_ARGS)
                                865                 :                : {
                                866                 :          36360 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                867                 :          36360 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                868                 :                : 
 3021 tomas.vondra@postgre      869                 :          36360 :     PG_RETURN_BOOL(float4_eq(arg1, arg2));
                                870                 :                : }
                                871                 :                : 
                                872                 :                : Datum
 9592 tgl@sss.pgh.pa.us         873                 :             20 : float4ne(PG_FUNCTION_ARGS)
                                874                 :                : {
                                875                 :             20 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                876                 :             20 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                877                 :                : 
 3021 tomas.vondra@postgre      878                 :             20 :     PG_RETURN_BOOL(float4_ne(arg1, arg2));
                                879                 :                : }
                                880                 :                : 
                                881                 :                : Datum
 9592 tgl@sss.pgh.pa.us         882                 :          49392 : float4lt(PG_FUNCTION_ARGS)
                                883                 :                : {
                                884                 :          49392 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                885                 :          49392 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                886                 :                : 
 3021 tomas.vondra@postgre      887                 :          49392 :     PG_RETURN_BOOL(float4_lt(arg1, arg2));
                                888                 :                : }
                                889                 :                : 
                                890                 :                : Datum
 9592 tgl@sss.pgh.pa.us         891                 :           2552 : float4le(PG_FUNCTION_ARGS)
                                892                 :                : {
                                893                 :           2552 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                894                 :           2552 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                895                 :                : 
 3021 tomas.vondra@postgre      896                 :           2552 :     PG_RETURN_BOOL(float4_le(arg1, arg2));
                                897                 :                : }
                                898                 :                : 
                                899                 :                : Datum
 9592 tgl@sss.pgh.pa.us         900                 :           3092 : float4gt(PG_FUNCTION_ARGS)
                                901                 :                : {
                                902                 :           3092 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                903                 :           3092 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                904                 :                : 
 3021 tomas.vondra@postgre      905                 :           3092 :     PG_RETURN_BOOL(float4_gt(arg1, arg2));
                                906                 :                : }
                                907                 :                : 
                                908                 :                : Datum
 9592 tgl@sss.pgh.pa.us         909                 :           2552 : float4ge(PG_FUNCTION_ARGS)
                                910                 :                : {
                                911                 :           2552 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                912                 :           2552 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                913                 :                : 
 3021 tomas.vondra@postgre      914                 :           2552 :     PG_RETURN_BOOL(float4_ge(arg1, arg2));
                                915                 :                : }
                                916                 :                : 
                                917                 :                : Datum
 9317 tgl@sss.pgh.pa.us         918                 :         942766 : btfloat4cmp(PG_FUNCTION_ARGS)
                                919                 :                : {
                                920                 :         942766 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                                921                 :         942766 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                                922                 :                : 
                                923                 :         942766 :     PG_RETURN_INT32(float4_cmp_internal(arg1, arg2));
                                924                 :                : }
                                925                 :                : 
                                926                 :                : static int
 5447                           927                 :        8743407 : btfloat4fastcmp(Datum x, Datum y, SortSupport ssup)
                                928                 :                : {
                                929                 :        8743407 :     float4      arg1 = DatumGetFloat4(x);
                                930                 :        8743407 :     float4      arg2 = DatumGetFloat4(y);
                                931                 :                : 
                                932                 :        8743407 :     return float4_cmp_internal(arg1, arg2);
                                933                 :                : }
                                934                 :                : 
                                935                 :                : Datum
                                936                 :            754 : btfloat4sortsupport(PG_FUNCTION_ARGS)
                                937                 :                : {
 5261 bruce@momjian.us          938                 :            754 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
                                939                 :                : 
 5447 tgl@sss.pgh.pa.us         940                 :            754 :     ssup->comparator = btfloat4fastcmp;
                                941                 :            754 :     PG_RETURN_VOID();
                                942                 :                : }
                                943                 :                : 
                                944                 :                : /*
                                945                 :                :  *      float8{eq,ne,lt,le,gt,ge}       - float8/float8 comparison operations
                                946                 :                :  */
                                947                 :                : int
 9317                           948                 :       15549046 : float8_cmp_internal(float8 a, float8 b)
                                949                 :                : {
 3021 tomas.vondra@postgre      950         [ +  + ]:       15549046 :     if (float8_gt(a, b))
                                951                 :        5600525 :         return 1;
                                952         [ +  + ]:        9948521 :     if (float8_lt(a, b))
                                953                 :        9756694 :         return -1;
                                954                 :         191827 :     return 0;
                                955                 :                : }
                                956                 :                : 
                                957                 :                : Datum
 9592 tgl@sss.pgh.pa.us         958                 :         402719 : float8eq(PG_FUNCTION_ARGS)
                                959                 :                : {
                                960                 :         402719 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                961                 :         402719 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                962                 :                : 
 3021 tomas.vondra@postgre      963                 :         402719 :     PG_RETURN_BOOL(float8_eq(arg1, arg2));
                                964                 :                : }
                                965                 :                : 
                                966                 :                : Datum
 9592 tgl@sss.pgh.pa.us         967                 :           8243 : float8ne(PG_FUNCTION_ARGS)
                                968                 :                : {
                                969                 :           8243 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                970                 :           8243 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                971                 :                : 
 3021 tomas.vondra@postgre      972                 :           8243 :     PG_RETURN_BOOL(float8_ne(arg1, arg2));
                                973                 :                : }
                                974                 :                : 
                                975                 :                : Datum
 9592 tgl@sss.pgh.pa.us         976                 :         112597 : float8lt(PG_FUNCTION_ARGS)
                                977                 :                : {
                                978                 :         112597 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                979                 :         112597 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                980                 :                : 
 3021 tomas.vondra@postgre      981                 :         112597 :     PG_RETURN_BOOL(float8_lt(arg1, arg2));
                                982                 :                : }
                                983                 :                : 
                                984                 :                : Datum
 9592 tgl@sss.pgh.pa.us         985                 :           3813 : float8le(PG_FUNCTION_ARGS)
                                986                 :                : {
                                987                 :           3813 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                988                 :           3813 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                989                 :                : 
 3021 tomas.vondra@postgre      990                 :           3813 :     PG_RETURN_BOOL(float8_le(arg1, arg2));
                                991                 :                : }
                                992                 :                : 
                                993                 :                : Datum
 9592 tgl@sss.pgh.pa.us         994                 :          20326 : float8gt(PG_FUNCTION_ARGS)
                                995                 :                : {
                                996                 :          20326 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                                997                 :          20326 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                                998                 :                : 
 3021 tomas.vondra@postgre      999                 :          20326 :     PG_RETURN_BOOL(float8_gt(arg1, arg2));
                               1000                 :                : }
                               1001                 :                : 
                               1002                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1003                 :          14030 : float8ge(PG_FUNCTION_ARGS)
                               1004                 :                : {
                               1005                 :          14030 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1006                 :          14030 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               1007                 :                : 
 3021 tomas.vondra@postgre     1008                 :          14030 :     PG_RETURN_BOOL(float8_ge(arg1, arg2));
                               1009                 :                : }
                               1010                 :                : 
                               1011                 :                : Datum
 9317 tgl@sss.pgh.pa.us        1012                 :           2242 : btfloat8cmp(PG_FUNCTION_ARGS)
                               1013                 :                : {
                               1014                 :           2242 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1015                 :           2242 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               1016                 :                : 
                               1017                 :           2242 :     PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
                               1018                 :                : }
                               1019                 :                : 
                               1020                 :                : static int
 5447                          1021                 :        4017713 : btfloat8fastcmp(Datum x, Datum y, SortSupport ssup)
                               1022                 :                : {
                               1023                 :        4017713 :     float8      arg1 = DatumGetFloat8(x);
                               1024                 :        4017713 :     float8      arg2 = DatumGetFloat8(y);
                               1025                 :                : 
                               1026                 :        4017713 :     return float8_cmp_internal(arg1, arg2);
                               1027                 :                : }
                               1028                 :                : 
                               1029                 :                : Datum
                               1030                 :            724 : btfloat8sortsupport(PG_FUNCTION_ARGS)
                               1031                 :                : {
 5261 bruce@momjian.us         1032                 :            724 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
                               1033                 :                : 
 5447 tgl@sss.pgh.pa.us        1034                 :            724 :     ssup->comparator = btfloat8fastcmp;
                               1035                 :            724 :     PG_RETURN_VOID();
                               1036                 :                : }
                               1037                 :                : 
                               1038                 :                : Datum
 8394                          1039                 :             17 : btfloat48cmp(PG_FUNCTION_ARGS)
                               1040                 :                : {
                               1041                 :             17 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               1042                 :             17 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               1043                 :                : 
                               1044                 :                :     /* widen float4 to float8 and then compare */
                               1045                 :             17 :     PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
                               1046                 :                : }
                               1047                 :                : 
                               1048                 :                : Datum
                               1049                 :            149 : btfloat84cmp(PG_FUNCTION_ARGS)
                               1050                 :                : {
                               1051                 :            149 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1052                 :            149 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               1053                 :                : 
                               1054                 :                :     /* widen float4 to float8 and then compare */
                               1055                 :            149 :     PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
                               1056                 :                : }
                               1057                 :                : 
                               1058                 :                : /*
                               1059                 :                :  * in_range support function for float8.
                               1060                 :                :  *
                               1061                 :                :  * Note: we needn't supply a float8_float4 variant, as implicit coercion
                               1062                 :                :  * of the offset value takes care of that scenario just as well.
                               1063                 :                :  */
                               1064                 :                : Datum
 3176                          1065                 :            768 : in_range_float8_float8(PG_FUNCTION_ARGS)
                               1066                 :                : {
                               1067                 :            768 :     float8      val = PG_GETARG_FLOAT8(0);
                               1068                 :            768 :     float8      base = PG_GETARG_FLOAT8(1);
                               1069                 :            768 :     float8      offset = PG_GETARG_FLOAT8(2);
                               1070                 :            768 :     bool        sub = PG_GETARG_BOOL(3);
                               1071                 :            768 :     bool        less = PG_GETARG_BOOL(4);
                               1072                 :                :     float8      sum;
                               1073                 :                : 
                               1074                 :                :     /*
                               1075                 :                :      * Reject negative or NaN offset.  Negative is per spec, and NaN is
                               1076                 :                :      * because appropriate semantics for that seem non-obvious.
                               1077                 :                :      */
                               1078   [ +  +  -  + ]:            768 :     if (isnan(offset) || offset < 0)
                               1079         [ +  - ]:              4 :         ereport(ERROR,
                               1080                 :                :                 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
                               1081                 :                :                  errmsg("invalid preceding or following size in window function")));
                               1082                 :                : 
                               1083                 :                :     /*
                               1084                 :                :      * Deal with cases where val and/or base is NaN, following the rule that
                               1085                 :                :      * NaN sorts after non-NaN (cf float8_cmp_internal).  The offset cannot
                               1086                 :                :      * affect the conclusion.
                               1087                 :                :      */
                               1088         [ +  + ]:            764 :     if (isnan(val))
                               1089                 :                :     {
                               1090         [ +  + ]:            124 :         if (isnan(base))
                               1091                 :             40 :             PG_RETURN_BOOL(true);   /* NAN = NAN */
                               1092                 :                :         else
                               1093                 :             84 :             PG_RETURN_BOOL(!less);  /* NAN > non-NAN */
                               1094                 :                :     }
                               1095         [ +  + ]:            640 :     else if (isnan(base))
                               1096                 :                :     {
                               1097                 :             84 :         PG_RETURN_BOOL(less);   /* non-NAN < NAN */
                               1098                 :                :     }
                               1099                 :                : 
                               1100                 :                :     /*
                               1101                 :                :      * Deal with cases where both base and offset are infinite, and computing
                               1102                 :                :      * base +/- offset would produce NaN.  This corresponds to a window frame
                               1103                 :                :      * whose boundary infinitely precedes +inf or infinitely follows -inf,
                               1104                 :                :      * which is not well-defined.  For consistency with other cases involving
                               1105                 :                :      * infinities, such as the fact that +inf infinitely follows +inf, we
                               1106                 :                :      * choose to assume that +inf infinitely precedes +inf and -inf infinitely
                               1107                 :                :      * follows -inf, and therefore that all finite and infinite values are in
                               1108                 :                :      * such a window frame.
                               1109                 :                :      *
                               1110                 :                :      * offset is known positive, so we need only check the sign of base in
                               1111                 :                :      * this test.
                               1112                 :                :      */
 2299                          1113   [ +  +  +  +  :            556 :     if (isinf(offset) && isinf(base) &&
                                        +  +  +  + ]
                               1114                 :                :         (sub ? base > 0 : base < 0))
                               1115                 :            116 :         PG_RETURN_BOOL(true);
                               1116                 :                : 
                               1117                 :                :     /*
                               1118                 :                :      * Otherwise it should be safe to compute base +/- offset.  We trust the
                               1119                 :                :      * FPU to cope if an input is +/-inf or the true sum would overflow, and
                               1120                 :                :      * produce a suitably signed infinity, which will compare properly against
                               1121                 :                :      * val whether or not that's infinity.
                               1122                 :                :      */
 3176                          1123         [ +  + ]:            440 :     if (sub)
                               1124                 :            240 :         sum = base - offset;
                               1125                 :                :     else
                               1126                 :            200 :         sum = base + offset;
                               1127                 :                : 
                               1128         [ +  + ]:            440 :     if (less)
                               1129                 :            172 :         PG_RETURN_BOOL(val <= sum);
                               1130                 :                :     else
                               1131                 :            268 :         PG_RETURN_BOOL(val >= sum);
                               1132                 :                : }
                               1133                 :                : 
                               1134                 :                : /*
                               1135                 :                :  * in_range support function for float4.
                               1136                 :                :  *
                               1137                 :                :  * We would need a float4_float8 variant in any case, so we supply that and
                               1138                 :                :  * let implicit coercion take care of the float4_float4 case.
                               1139                 :                :  */
                               1140                 :                : Datum
                               1141                 :            768 : in_range_float4_float8(PG_FUNCTION_ARGS)
                               1142                 :                : {
 3106                          1143                 :            768 :     float4      val = PG_GETARG_FLOAT4(0);
                               1144                 :            768 :     float4      base = PG_GETARG_FLOAT4(1);
                               1145                 :            768 :     float8      offset = PG_GETARG_FLOAT8(2);
                               1146                 :            768 :     bool        sub = PG_GETARG_BOOL(3);
                               1147                 :            768 :     bool        less = PG_GETARG_BOOL(4);
                               1148                 :                :     float8      sum;
                               1149                 :                : 
                               1150                 :                :     /*
                               1151                 :                :      * Reject negative or NaN offset.  Negative is per spec, and NaN is
                               1152                 :                :      * because appropriate semantics for that seem non-obvious.
                               1153                 :                :      */
                               1154   [ +  +  -  + ]:            768 :     if (isnan(offset) || offset < 0)
                               1155         [ +  - ]:              4 :         ereport(ERROR,
                               1156                 :                :                 (errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
                               1157                 :                :                  errmsg("invalid preceding or following size in window function")));
                               1158                 :                : 
                               1159                 :                :     /*
                               1160                 :                :      * Deal with cases where val and/or base is NaN, following the rule that
                               1161                 :                :      * NaN sorts after non-NaN (cf float8_cmp_internal).  The offset cannot
                               1162                 :                :      * affect the conclusion.
                               1163                 :                :      */
                               1164         [ +  + ]:            764 :     if (isnan(val))
                               1165                 :                :     {
                               1166         [ +  + ]:            124 :         if (isnan(base))
                               1167                 :             40 :             PG_RETURN_BOOL(true);   /* NAN = NAN */
                               1168                 :                :         else
                               1169                 :             84 :             PG_RETURN_BOOL(!less);  /* NAN > non-NAN */
                               1170                 :                :     }
                               1171         [ +  + ]:            640 :     else if (isnan(base))
                               1172                 :                :     {
                               1173                 :             84 :         PG_RETURN_BOOL(less);   /* non-NAN < NAN */
                               1174                 :                :     }
                               1175                 :                : 
                               1176                 :                :     /*
                               1177                 :                :      * Deal with cases where both base and offset are infinite, and computing
                               1178                 :                :      * base +/- offset would produce NaN.  This corresponds to a window frame
                               1179                 :                :      * whose boundary infinitely precedes +inf or infinitely follows -inf,
                               1180                 :                :      * which is not well-defined.  For consistency with other cases involving
                               1181                 :                :      * infinities, such as the fact that +inf infinitely follows +inf, we
                               1182                 :                :      * choose to assume that +inf infinitely precedes +inf and -inf infinitely
                               1183                 :                :      * follows -inf, and therefore that all finite and infinite values are in
                               1184                 :                :      * such a window frame.
                               1185                 :                :      *
                               1186                 :                :      * offset is known positive, so we need only check the sign of base in
                               1187                 :                :      * this test.
                               1188                 :                :      */
 2299                          1189   [ +  +  +  +  :            556 :     if (isinf(offset) && isinf(base) &&
                                        +  +  +  + ]
                               1190                 :                :         (sub ? base > 0 : base < 0))
                               1191                 :            116 :         PG_RETURN_BOOL(true);
                               1192                 :                : 
                               1193                 :                :     /*
                               1194                 :                :      * Otherwise it should be safe to compute base +/- offset.  We trust the
                               1195                 :                :      * FPU to cope if an input is +/-inf or the true sum would overflow, and
                               1196                 :                :      * produce a suitably signed infinity, which will compare properly against
                               1197                 :                :      * val whether or not that's infinity.
                               1198                 :                :      */
 3106                          1199         [ +  + ]:            440 :     if (sub)
                               1200                 :            240 :         sum = base - offset;
                               1201                 :                :     else
                               1202                 :            200 :         sum = base + offset;
                               1203                 :                : 
                               1204         [ +  + ]:            440 :     if (less)
                               1205                 :            172 :         PG_RETURN_BOOL(val <= sum);
                               1206                 :                :     else
                               1207                 :            268 :         PG_RETURN_BOOL(val >= sum);
                               1208                 :                : }
                               1209                 :                : 
                               1210                 :                : 
                               1211                 :                : /*
                               1212                 :                :  *      ===================
                               1213                 :                :  *      CONVERSION ROUTINES
                               1214                 :                :  *      ===================
                               1215                 :                :  */
                               1216                 :                : 
                               1217                 :                : /*
                               1218                 :                :  *      ftod            - converts a float4 number to a float8 number
                               1219                 :                :  */
                               1220                 :                : Datum
 9592                          1221                 :            201 : ftod(PG_FUNCTION_ARGS)
                               1222                 :                : {
                               1223                 :            201 :     float4      num = PG_GETARG_FLOAT4(0);
                               1224                 :                : 
                               1225                 :            201 :     PG_RETURN_FLOAT8((float8) num);
                               1226                 :                : }
                               1227                 :                : 
                               1228                 :                : 
                               1229                 :                : /*
                               1230                 :                :  *      dtof            - converts a float8 number to a float4 number
                               1231                 :                :  */
                               1232                 :                : Datum
                               1233                 :             36 : dtof(PG_FUNCTION_ARGS)
                               1234                 :                : {
                               1235                 :             36 :     float8      num = PG_GETARG_FLOAT8(0);
                               1236                 :                :     float4      result;
                               1237                 :                : 
 2457                          1238                 :             36 :     result = (float4) num;
                               1239   [ +  +  +  - ]:             36 :     if (unlikely(isinf(result)) && !isinf(num))
  221 peter@eisentraut.org     1240                 :              8 :         float_overflow_error_ext(fcinfo->context);
 2457 tgl@sss.pgh.pa.us        1241   [ +  +  +  - ]:             28 :     if (unlikely(result == 0.0f) && num != 0.0)
  221 peter@eisentraut.org     1242                 :              8 :         float_underflow_error_ext(fcinfo->context);
                               1243                 :                : 
 2457 tgl@sss.pgh.pa.us        1244                 :             20 :     PG_RETURN_FLOAT4(result);
                               1245                 :                : }
                               1246                 :                : 
                               1247                 :                : 
                               1248                 :                : /*
                               1249                 :                :  *      dtoi4           - converts a float8 number to an int4 number
                               1250                 :                :  */
                               1251                 :                : Datum
 9592                          1252                 :         744332 : dtoi4(PG_FUNCTION_ARGS)
                               1253                 :                : {
                               1254                 :         744332 :     float8      num = PG_GETARG_FLOAT8(0);
                               1255                 :                : 
                               1256                 :                :     /*
                               1257                 :                :      * Get rid of any fractional part in the input.  This is so we don't fail
                               1258                 :                :      * on just-out-of-range values that would round into range.  Note
                               1259                 :                :      * assumption that rint() will pass through a NaN or Inf unchanged.
                               1260                 :                :      */
 2904                          1261                 :         744332 :     num = rint(num);
                               1262                 :                : 
                               1263                 :                :     /* Range check */
 2555                          1264   [ +  +  +  +  :         744332 :     if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT32(num)))
                                     +  +  +  +  +  
                                                 + ]
  226 peter@eisentraut.org     1265         [ +  - ]:             20 :         ereturn(fcinfo->context, (Datum) 0,
                               1266                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1267                 :                :                  errmsg("integer out of range")));
                               1268                 :                : 
 2904 tgl@sss.pgh.pa.us        1269                 :         744312 :     PG_RETURN_INT32((int32) num);
                               1270                 :                : }
                               1271                 :                : 
                               1272                 :                : 
                               1273                 :                : /*
                               1274                 :                :  *      dtoi2           - converts a float8 number to an int2 number
                               1275                 :                :  */
                               1276                 :                : Datum
 9649                          1277                 :             62 : dtoi2(PG_FUNCTION_ARGS)
                               1278                 :                : {
                               1279                 :             62 :     float8      num = PG_GETARG_FLOAT8(0);
                               1280                 :                : 
                               1281                 :                :     /*
                               1282                 :                :      * Get rid of any fractional part in the input.  This is so we don't fail
                               1283                 :                :      * on just-out-of-range values that would round into range.  Note
                               1284                 :                :      * assumption that rint() will pass through a NaN or Inf unchanged.
                               1285                 :                :      */
 2904                          1286                 :             62 :     num = rint(num);
                               1287                 :                : 
                               1288                 :                :     /* Range check */
 2555                          1289   [ +  -  +  +  :             62 :     if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT16(num)))
                                     +  +  +  +  +  
                                                 + ]
  226 peter@eisentraut.org     1290         [ +  - ]:              8 :         ereturn(fcinfo->context, (Datum) 0,
                               1291                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1292                 :                :                  errmsg("smallint out of range")));
                               1293                 :                : 
 2904 tgl@sss.pgh.pa.us        1294                 :             54 :     PG_RETURN_INT16((int16) num);
                               1295                 :                : }
                               1296                 :                : 
                               1297                 :                : 
                               1298                 :                : /*
                               1299                 :                :  *      i4tod           - converts an int4 number to a float8 number
                               1300                 :                :  */
                               1301                 :                : Datum
 9641                          1302                 :        1539418 : i4tod(PG_FUNCTION_ARGS)
                               1303                 :                : {
                               1304                 :        1539418 :     int32       num = PG_GETARG_INT32(0);
                               1305                 :                : 
 7247 bruce@momjian.us         1306                 :        1539418 :     PG_RETURN_FLOAT8((float8) num);
                               1307                 :                : }
                               1308                 :                : 
                               1309                 :                : 
                               1310                 :                : /*
                               1311                 :                :  *      i2tod           - converts an int2 number to a float8 number
                               1312                 :                :  */
                               1313                 :                : Datum
 9649 tgl@sss.pgh.pa.us        1314                 :            164 : i2tod(PG_FUNCTION_ARGS)
                               1315                 :                : {
                               1316                 :            164 :     int16       num = PG_GETARG_INT16(0);
                               1317                 :                : 
 7247 bruce@momjian.us         1318                 :            164 :     PG_RETURN_FLOAT8((float8) num);
                               1319                 :                : }
                               1320                 :                : 
                               1321                 :                : 
                               1322                 :                : /*
                               1323                 :                :  *      ftoi4           - converts a float4 number to an int4 number
                               1324                 :                :  */
                               1325                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1326                 :             18 : ftoi4(PG_FUNCTION_ARGS)
                               1327                 :                : {
                               1328                 :             18 :     float4      num = PG_GETARG_FLOAT4(0);
                               1329                 :                : 
                               1330                 :                :     /*
                               1331                 :                :      * Get rid of any fractional part in the input.  This is so we don't fail
                               1332                 :                :      * on just-out-of-range values that would round into range.  Note
                               1333                 :                :      * assumption that rint() will pass through a NaN or Inf unchanged.
                               1334                 :                :      */
 2904                          1335                 :             18 :     num = rint(num);
                               1336                 :                : 
                               1337                 :                :     /* Range check */
 2555                          1338   [ +  -  +  +  :             18 :     if (unlikely(isnan(num) || !FLOAT4_FITS_IN_INT32(num)))
                                     +  +  +  +  +  
                                                 + ]
  226 peter@eisentraut.org     1339         [ +  - ]:              8 :         ereturn(fcinfo->context, (Datum) 0,
                               1340                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1341                 :                :                  errmsg("integer out of range")));
                               1342                 :                : 
 2904 tgl@sss.pgh.pa.us        1343                 :             10 :     PG_RETURN_INT32((int32) num);
                               1344                 :                : }
                               1345                 :                : 
                               1346                 :                : 
                               1347                 :                : /*
                               1348                 :                :  *      ftoi2           - converts a float4 number to an int2 number
                               1349                 :                :  */
                               1350                 :                : Datum
 9649                          1351                 :             18 : ftoi2(PG_FUNCTION_ARGS)
                               1352                 :                : {
                               1353                 :             18 :     float4      num = PG_GETARG_FLOAT4(0);
                               1354                 :                : 
                               1355                 :                :     /*
                               1356                 :                :      * Get rid of any fractional part in the input.  This is so we don't fail
                               1357                 :                :      * on just-out-of-range values that would round into range.  Note
                               1358                 :                :      * assumption that rint() will pass through a NaN or Inf unchanged.
                               1359                 :                :      */
 2904                          1360                 :             18 :     num = rint(num);
                               1361                 :                : 
                               1362                 :                :     /* Range check */
 2555                          1363   [ +  -  +  +  :             18 :     if (unlikely(isnan(num) || !FLOAT4_FITS_IN_INT16(num)))
                                     +  +  +  +  +  
                                                 + ]
  226 peter@eisentraut.org     1364         [ +  - ]:              8 :         ereturn(fcinfo->context, (Datum) 0,
                               1365                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1366                 :                :                  errmsg("smallint out of range")));
                               1367                 :                : 
 2904 tgl@sss.pgh.pa.us        1368                 :             10 :     PG_RETURN_INT16((int16) num);
                               1369                 :                : }
                               1370                 :                : 
                               1371                 :                : 
                               1372                 :                : /*
                               1373                 :                :  *      i4tof           - converts an int4 number to a float4 number
                               1374                 :                :  */
                               1375                 :                : Datum
 9641                          1376                 :            407 : i4tof(PG_FUNCTION_ARGS)
                               1377                 :                : {
                               1378                 :            407 :     int32       num = PG_GETARG_INT32(0);
                               1379                 :                : 
 7247 bruce@momjian.us         1380                 :            407 :     PG_RETURN_FLOAT4((float4) num);
                               1381                 :                : }
                               1382                 :                : 
                               1383                 :                : 
                               1384                 :                : /*
                               1385                 :                :  *      i2tof           - converts an int2 number to a float4 number
                               1386                 :                :  */
                               1387                 :                : Datum
 9649 tgl@sss.pgh.pa.us        1388                 :UBC           0 : i2tof(PG_FUNCTION_ARGS)
                               1389                 :                : {
                               1390                 :              0 :     int16       num = PG_GETARG_INT16(0);
                               1391                 :                : 
 7247 bruce@momjian.us         1392                 :              0 :     PG_RETURN_FLOAT4((float4) num);
                               1393                 :                : }
                               1394                 :                : 
                               1395                 :                : 
                               1396                 :                : /*
                               1397                 :                :  *      =======================
                               1398                 :                :  *      RANDOM FLOAT8 OPERATORS
                               1399                 :                :  *      =======================
                               1400                 :                :  */
                               1401                 :                : 
                               1402                 :                : /*
                               1403                 :                :  *      dround          - returns   ROUND(arg1)
                               1404                 :                :  */
                               1405                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1406                 :CBC      592744 : dround(PG_FUNCTION_ARGS)
                               1407                 :                : {
                               1408                 :         592744 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1409                 :                : 
 7247 bruce@momjian.us         1410                 :         592744 :     PG_RETURN_FLOAT8(rint(arg1));
                               1411                 :                : }
                               1412                 :                : 
                               1413                 :                : /*
                               1414                 :                :  *      dceil           - returns the smallest integer greater than or
                               1415                 :                :  *                        equal to the specified float
                               1416                 :                :  */
                               1417                 :                : Datum
 8783                          1418                 :          34240 : dceil(PG_FUNCTION_ARGS)
                               1419                 :                : {
                               1420                 :          34240 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1421                 :                : 
                               1422                 :          34240 :     PG_RETURN_FLOAT8(ceil(arg1));
                               1423                 :                : }
                               1424                 :                : 
                               1425                 :                : /*
                               1426                 :                :  *      dfloor          - returns the largest integer lesser than or
                               1427                 :                :  *                        equal to the specified float
                               1428                 :                :  */
                               1429                 :                : Datum
                               1430                 :             40 : dfloor(PG_FUNCTION_ARGS)
                               1431                 :                : {
                               1432                 :             40 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1433                 :                : 
                               1434                 :             40 :     PG_RETURN_FLOAT8(floor(arg1));
                               1435                 :                : }
                               1436                 :                : 
                               1437                 :                : /*
                               1438                 :                :  *      dsign           - returns -1 if the argument is less than 0, 0
                               1439                 :                :  *                        if the argument is equal to 0, and 1 if the
                               1440                 :                :  *                        argument is greater than zero.
                               1441                 :                :  */
                               1442                 :                : Datum
                               1443                 :             20 : dsign(PG_FUNCTION_ARGS)
                               1444                 :                : {
                               1445                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1446                 :                :     float8      result;
                               1447                 :                : 
                               1448         [ +  + ]:             20 :     if (arg1 > 0)
                               1449                 :             12 :         result = 1.0;
                               1450         [ +  + ]:              8 :     else if (arg1 < 0)
                               1451                 :              4 :         result = -1.0;
                               1452                 :                :     else
                               1453                 :              4 :         result = 0.0;
                               1454                 :                : 
                               1455                 :             20 :     PG_RETURN_FLOAT8(result);
                               1456                 :                : }
                               1457                 :                : 
                               1458                 :                : /*
                               1459                 :                :  *      dtrunc          - returns truncation-towards-zero of arg1,
                               1460                 :                :  *                        arg1 >= 0 ... the greatest integer less
                               1461                 :                :  *                                      than or equal to arg1
                               1462                 :                :  *                        arg1 < 0   ... the least integer greater
                               1463                 :                :  *                                      than or equal to arg1
                               1464                 :                :  */
                               1465                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1466                 :         113143 : dtrunc(PG_FUNCTION_ARGS)
                               1467                 :                : {
                               1468                 :         113143 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1469                 :                :     float8      result;
                               1470                 :                : 
                               1471         [ +  + ]:         113143 :     if (arg1 >= 0)
                               1472                 :         113139 :         result = floor(arg1);
                               1473                 :                :     else
                               1474                 :              4 :         result = -floor(-arg1);
                               1475                 :                : 
                               1476                 :         113143 :     PG_RETURN_FLOAT8(result);
                               1477                 :                : }
                               1478                 :                : 
                               1479                 :                : 
                               1480                 :                : /*
                               1481                 :                :  *      dsqrt           - returns square root of arg1
                               1482                 :                :  */
                               1483                 :                : Datum
                               1484                 :           2052 : dsqrt(PG_FUNCTION_ARGS)
                               1485                 :                : {
                               1486                 :           2052 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1487                 :                :     float8      result;
                               1488                 :                : 
                               1489         [ -  + ]:           2052 :     if (arg1 < 0)
 8502 tgl@sss.pgh.pa.us        1490         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1491                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
                               1492                 :                :                  errmsg("cannot take square root of a negative number")));
                               1493                 :                : 
 9592 tgl@sss.pgh.pa.us        1494                 :CBC        2052 :     result = sqrt(arg1);
 2457                          1495   [ -  +  -  - ]:           2052 :     if (unlikely(isinf(result)) && !isinf(arg1))
 2457 tgl@sss.pgh.pa.us        1496                 :UBC           0 :         float_overflow_error();
 2457 tgl@sss.pgh.pa.us        1497   [ +  +  -  + ]:CBC        2052 :     if (unlikely(result == 0.0) && arg1 != 0.0)
 2457 tgl@sss.pgh.pa.us        1498                 :UBC           0 :         float_underflow_error();
                               1499                 :                : 
 9592 tgl@sss.pgh.pa.us        1500                 :CBC        2052 :     PG_RETURN_FLOAT8(result);
                               1501                 :                : }
                               1502                 :                : 
                               1503                 :                : 
                               1504                 :                : /*
                               1505                 :                :  *      dcbrt           - returns cube root of arg1
                               1506                 :                :  */
                               1507                 :                : Datum
                               1508                 :             25 : dcbrt(PG_FUNCTION_ARGS)
                               1509                 :                : {
                               1510                 :             25 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1511                 :                :     float8      result;
                               1512                 :                : 
                               1513                 :             25 :     result = cbrt(arg1);
 2457                          1514   [ -  +  -  - ]:             25 :     if (unlikely(isinf(result)) && !isinf(arg1))
 2457 tgl@sss.pgh.pa.us        1515                 :UBC           0 :         float_overflow_error();
 2457 tgl@sss.pgh.pa.us        1516   [ +  +  -  + ]:CBC          25 :     if (unlikely(result == 0.0) && arg1 != 0.0)
 2457 tgl@sss.pgh.pa.us        1517                 :UBC           0 :         float_underflow_error();
                               1518                 :                : 
 9592 tgl@sss.pgh.pa.us        1519                 :CBC          25 :     PG_RETURN_FLOAT8(result);
                               1520                 :                : }
                               1521                 :                : 
                               1522                 :                : 
                               1523                 :                : /*
                               1524                 :                :  *      dpow            - returns pow(arg1,arg2)
                               1525                 :                :  */
                               1526                 :                : Datum
                               1527                 :            484 : dpow(PG_FUNCTION_ARGS)
                               1528                 :                : {
                               1529                 :            484 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1530                 :            484 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               1531                 :                :     float8      result;
                               1532                 :                : 
                               1533                 :                :     /*
                               1534                 :                :      * The POSIX spec says that NaN ^ 0 = 1, and 1 ^ NaN = 1, while all other
                               1535                 :                :      * cases with NaN inputs yield NaN (with no error).  Many older platforms
                               1536                 :                :      * get one or more of these cases wrong, so deal with them via explicit
                               1537                 :                :      * logic rather than trusting pow(3).
                               1538                 :                :      */
 3112                          1539         [ +  + ]:            484 :     if (isnan(arg1))
                               1540                 :                :     {
                               1541   [ +  +  +  + ]:             15 :         if (isnan(arg2) || arg2 != 0.0)
                               1542                 :             10 :             PG_RETURN_FLOAT8(get_float8_nan());
                               1543                 :              5 :         PG_RETURN_FLOAT8(1.0);
                               1544                 :                :     }
                               1545         [ +  + ]:            469 :     if (isnan(arg2))
                               1546                 :                :     {
                               1547         [ +  + ]:             15 :         if (arg1 != 1.0)
                               1548                 :             10 :             PG_RETURN_FLOAT8(get_float8_nan());
                               1549                 :              5 :         PG_RETURN_FLOAT8(1.0);
                               1550                 :                :     }
                               1551                 :                : 
                               1552                 :                :     /*
                               1553                 :                :      * The SQL spec requires that we emit a particular SQLSTATE error code for
                               1554                 :                :      * certain error conditions.  Specifically, we don't return a
                               1555                 :                :      * divide-by-zero error code for 0 ^ -1.
                               1556                 :                :      */
 6754 bruce@momjian.us         1557   [ +  +  +  + ]:            454 :     if (arg1 == 0 && arg2 < 0)
 8208 neilc@samurai.com        1558         [ +  - ]:              4 :         ereport(ERROR,
                               1559                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
                               1560                 :                :                  errmsg("zero raised to a negative power is undefined")));
 6754 bruce@momjian.us         1561   [ +  +  +  + ]:            450 :     if (arg1 < 0 && floor(arg2) != arg2)
                               1562         [ +  - ]:              4 :         ereport(ERROR,
                               1563                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
                               1564                 :                :                  errmsg("a negative number raised to a non-integer power yields a complex result")));
                               1565                 :                : 
                               1566                 :                :     /*
                               1567                 :                :      * We don't trust the platform's pow() to handle infinity cases per POSIX
                               1568                 :                :      * spec either, so deal with those explicitly too.  It's easier to handle
                               1569                 :                :      * infinite y first, so that it doesn't matter if x is also infinite.
                               1570                 :                :      */
 2334 tgl@sss.pgh.pa.us        1571         [ +  + ]:            446 :     if (isinf(arg2))
                               1572                 :                :     {
 2333                          1573                 :             85 :         float8      absx = fabs(arg1);
                               1574                 :                : 
 2334                          1575         [ +  + ]:             85 :         if (absx == 1.0)
                               1576                 :             20 :             result = 1.0;
                               1577         [ +  + ]:             65 :         else if (arg2 > 0.0) /* y = +Inf */
                               1578                 :                :         {
                               1579         [ +  + ]:             35 :             if (absx > 1.0)
                               1580                 :             20 :                 result = arg2;
                               1581                 :                :             else
                               1582                 :             15 :                 result = 0.0;
                               1583                 :                :         }
                               1584                 :                :         else                    /* y = -Inf */
                               1585                 :                :         {
                               1586         [ +  + ]:             30 :             if (absx > 1.0)
                               1587                 :             20 :                 result = 0.0;
                               1588                 :                :             else
                               1589                 :             10 :                 result = -arg2;
                               1590                 :                :         }
                               1591                 :                :     }
                               1592         [ +  + ]:            361 :     else if (isinf(arg1))
                               1593                 :                :     {
                               1594         [ +  + ]:             40 :         if (arg2 == 0.0)
                               1595                 :             10 :             result = 1.0;
                               1596         [ +  + ]:             30 :         else if (arg1 > 0.0) /* x = +Inf */
                               1597                 :                :         {
                               1598         [ +  + ]:             10 :             if (arg2 > 0.0)
                               1599                 :              5 :                 result = arg1;
                               1600                 :                :             else
                               1601                 :              5 :                 result = 0.0;
                               1602                 :                :         }
                               1603                 :                :         else                    /* x = -Inf */
                               1604                 :                :         {
                               1605                 :                :             /*
                               1606                 :                :              * Per POSIX, the sign of the result depends on whether y is an
                               1607                 :                :              * odd integer.  Since x < 0, we already know from the previous
                               1608                 :                :              * domain check that y is an integer.  It is odd if y/2 is not
                               1609                 :                :              * also an integer.
                               1610                 :                :              */
 2333                          1611                 :             20 :             float8      halfy = arg2 / 2;   /* should be computed exactly */
                               1612                 :             20 :             bool        yisoddinteger = (floor(halfy) != halfy);
                               1613                 :                : 
 2334                          1614         [ +  + ]:             20 :             if (arg2 > 0.0)
                               1615         [ +  + ]:             10 :                 result = yisoddinteger ? arg1 : -arg1;
                               1616                 :                :             else
                               1617         [ +  + ]:             10 :                 result = yisoddinteger ? -0.0 : 0.0;
                               1618                 :                :         }
                               1619                 :                :     }
                               1620                 :                :     else
                               1621                 :                :     {
                               1622                 :                :         /*
                               1623                 :                :          * pow() sets errno on only some platforms, depending on whether it
                               1624                 :                :          * follows _IEEE_, _POSIX_, _XOPEN_, or _SVID_, so we must check both
                               1625                 :                :          * errno and invalid output values.  (We can't rely on just the
                               1626                 :                :          * latter, either; some old platforms return a large-but-finite
                               1627                 :                :          * HUGE_VAL when reporting overflow.)
                               1628                 :                :          */
                               1629                 :            321 :         errno = 0;
                               1630                 :            321 :         result = pow(arg1, arg2);
                               1631   [ +  -  -  + ]:            321 :         if (errno == EDOM || isnan(result))
                               1632                 :                :         {
                               1633                 :                :             /*
                               1634                 :                :              * We handled all possible domain errors above, so this should be
                               1635                 :                :              * impossible.  However, old glibc versions on x86 have a bug that
                               1636                 :                :              * causes them to fail this way for abs(y) greater than 2^63:
                               1637                 :                :              *
                               1638                 :                :              * https://sourceware.org/bugzilla/show_bug.cgi?id=3866
                               1639                 :                :              *
                               1640                 :                :              * Hence, if we get here, assume y is finite but large (large
                               1641                 :                :              * enough to be certainly even). The result should be 0 if x == 0,
                               1642                 :                :              * 1.0 if abs(x) == 1.0, otherwise an overflow or underflow error.
                               1643                 :                :              */
 2334 tgl@sss.pgh.pa.us        1644         [ #  # ]:UBC           0 :             if (arg1 == 0.0)
                               1645                 :              0 :                 result = 0.0;   /* we already verified y is positive */
                               1646                 :                :             else
                               1647                 :                :             {
 2333                          1648                 :              0 :                 float8      absx = fabs(arg1);
                               1649                 :                : 
 2334                          1650         [ #  # ]:              0 :                 if (absx == 1.0)
                               1651                 :              0 :                     result = 1.0;
                               1652   [ #  #  #  # ]:              0 :                 else if (arg2 >= 0.0 ? (absx > 1.0) : (absx < 1.0))
                               1653                 :              0 :                     float_overflow_error();
                               1654                 :                :                 else
                               1655                 :              0 :                     float_underflow_error();
                               1656                 :                :             }
                               1657                 :                :         }
 2334 tgl@sss.pgh.pa.us        1658         [ +  + ]:CBC         321 :         else if (errno == ERANGE)
                               1659                 :                :         {
                               1660         [ +  - ]:              4 :             if (result != 0.0)
                               1661                 :              4 :                 float_overflow_error();
                               1662                 :                :             else
 2334 tgl@sss.pgh.pa.us        1663                 :UBC           0 :                 float_underflow_error();
                               1664                 :                :         }
                               1665                 :                :         else
                               1666                 :                :         {
 2334 tgl@sss.pgh.pa.us        1667         [ -  + ]:CBC         317 :             if (unlikely(isinf(result)))
 2334 tgl@sss.pgh.pa.us        1668                 :UBC           0 :                 float_overflow_error();
 2334 tgl@sss.pgh.pa.us        1669   [ +  +  -  + ]:CBC         317 :             if (unlikely(result == 0.0) && arg1 != 0.0)
 2334 tgl@sss.pgh.pa.us        1670                 :UBC           0 :                 float_underflow_error();
                               1671                 :                :         }
                               1672                 :                :     }
                               1673                 :                : 
 9592 tgl@sss.pgh.pa.us        1674                 :CBC         442 :     PG_RETURN_FLOAT8(result);
                               1675                 :                : }
                               1676                 :                : 
                               1677                 :                : 
                               1678                 :                : /*
                               1679                 :                :  *      dexp            - returns the exponential function of arg1
                               1680                 :                :  */
                               1681                 :                : Datum
                               1682                 :             39 : dexp(PG_FUNCTION_ARGS)
                               1683                 :                : {
                               1684                 :             39 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1685                 :                :     float8      result;
                               1686                 :                : 
                               1687                 :                :     /*
                               1688                 :                :      * Handle NaN and Inf cases explicitly.  This avoids needing to assume
                               1689                 :                :      * that the platform's exp() conforms to POSIX for these cases, and it
                               1690                 :                :      * removes some edge cases for the overflow checks below.
                               1691                 :                :      */
 2335                          1692         [ +  + ]:             39 :     if (isnan(arg1))
                               1693                 :              5 :         result = arg1;
                               1694         [ +  + ]:             34 :     else if (isinf(arg1))
                               1695                 :                :     {
                               1696                 :                :         /* Per POSIX, exp(-Inf) is 0 */
                               1697         [ +  + ]:             10 :         result = (arg1 > 0.0) ? arg1 : 0;
                               1698                 :                :     }
                               1699                 :                :     else
                               1700                 :                :     {
                               1701                 :                :         /*
                               1702                 :                :          * On some platforms, exp() will not set errno but just return Inf or
                               1703                 :                :          * zero to report overflow/underflow; therefore, test both cases.
                               1704                 :                :          */
                               1705                 :             24 :         errno = 0;
                               1706                 :             24 :         result = exp(arg1);
                               1707         [ +  + ]:             24 :         if (unlikely(errno == ERANGE))
                               1708                 :                :         {
                               1709         [ -  + ]:              4 :             if (result != 0.0)
 2335 tgl@sss.pgh.pa.us        1710                 :UBC           0 :                 float_overflow_error();
                               1711                 :                :             else
 2335 tgl@sss.pgh.pa.us        1712                 :CBC           4 :                 float_underflow_error();
                               1713                 :                :         }
                               1714         [ -  + ]:             20 :         else if (unlikely(isinf(result)))
 2335 tgl@sss.pgh.pa.us        1715                 :UBC           0 :             float_overflow_error();
 2335 tgl@sss.pgh.pa.us        1716         [ -  + ]:CBC          20 :         else if (unlikely(result == 0.0))
 2335 tgl@sss.pgh.pa.us        1717                 :UBC           0 :             float_underflow_error();
                               1718                 :                :     }
                               1719                 :                : 
 9592 tgl@sss.pgh.pa.us        1720                 :CBC          35 :     PG_RETURN_FLOAT8(result);
                               1721                 :                : }
                               1722                 :                : 
                               1723                 :                : 
                               1724                 :                : /*
                               1725                 :                :  *      dlog1           - returns the natural logarithm of arg1
                               1726                 :                :  */
                               1727                 :                : Datum
                               1728                 :             20 : dlog1(PG_FUNCTION_ARGS)
                               1729                 :                : {
                               1730                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1731                 :                :     float8      result;
                               1732                 :                : 
                               1733                 :                :     /*
                               1734                 :                :      * Emit particular SQLSTATE error codes for ln(). This is required by the
                               1735                 :                :      * SQL standard.
                               1736                 :                :      */
                               1737         [ +  + ]:             20 :     if (arg1 == 0.0)
 8502                          1738         [ +  - ]:              4 :         ereport(ERROR,
                               1739                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                               1740                 :                :                  errmsg("cannot take logarithm of zero")));
 9592                          1741         [ +  + ]:             16 :     if (arg1 < 0)
 8502                          1742         [ +  - ]:              4 :         ereport(ERROR,
                               1743                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                               1744                 :                :                  errmsg("cannot take logarithm of a negative number")));
                               1745                 :                : 
 9592                          1746                 :             12 :     result = log(arg1);
 2457                          1747   [ -  +  -  - ]:             12 :     if (unlikely(isinf(result)) && !isinf(arg1))
 2457 tgl@sss.pgh.pa.us        1748                 :UBC           0 :         float_overflow_error();
 2457 tgl@sss.pgh.pa.us        1749   [ -  +  -  - ]:CBC          12 :     if (unlikely(result == 0.0) && arg1 != 1.0)
 2457 tgl@sss.pgh.pa.us        1750                 :UBC           0 :         float_underflow_error();
                               1751                 :                : 
 9592 tgl@sss.pgh.pa.us        1752                 :CBC          12 :     PG_RETURN_FLOAT8(result);
                               1753                 :                : }
                               1754                 :                : 
                               1755                 :                : 
                               1756                 :                : /*
                               1757                 :                :  *      dlog10          - returns the base 10 logarithm of arg1
                               1758                 :                :  */
                               1759                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1760                 :UBC           0 : dlog10(PG_FUNCTION_ARGS)
                               1761                 :                : {
                               1762                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1763                 :                :     float8      result;
                               1764                 :                : 
                               1765                 :                :     /*
                               1766                 :                :      * Emit particular SQLSTATE error codes for log(). The SQL spec doesn't
                               1767                 :                :      * define log(), but it does define ln(), so it makes sense to emit the
                               1768                 :                :      * same error code for an analogous error condition.
                               1769                 :                :      */
                               1770         [ #  # ]:              0 :     if (arg1 == 0.0)
 8502                          1771         [ #  # ]:              0 :         ereport(ERROR,
                               1772                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                               1773                 :                :                  errmsg("cannot take logarithm of zero")));
 9592                          1774         [ #  # ]:              0 :     if (arg1 < 0)
 8502                          1775         [ #  # ]:              0 :         ereport(ERROR,
                               1776                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
                               1777                 :                :                  errmsg("cannot take logarithm of a negative number")));
                               1778                 :                : 
 9592                          1779                 :              0 :     result = log10(arg1);
 2457                          1780   [ #  #  #  # ]:              0 :     if (unlikely(isinf(result)) && !isinf(arg1))
                               1781                 :              0 :         float_overflow_error();
                               1782   [ #  #  #  # ]:              0 :     if (unlikely(result == 0.0) && arg1 != 1.0)
                               1783                 :              0 :         float_underflow_error();
                               1784                 :                : 
 9592                          1785                 :              0 :     PG_RETURN_FLOAT8(result);
                               1786                 :                : }
                               1787                 :                : 
                               1788                 :                : 
                               1789                 :                : /*
                               1790                 :                :  *      dacos           - returns the arccos of arg1 (radians)
                               1791                 :                :  */
                               1792                 :                : Datum
                               1793                 :              0 : dacos(PG_FUNCTION_ARGS)
                               1794                 :                : {
                               1795                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1796                 :                :     float8      result;
                               1797                 :                : 
                               1798                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          1799         [ #  # ]:              0 :     if (isnan(arg1))
                               1800                 :              0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1801                 :                : 
                               1802                 :                :     /*
                               1803                 :                :      * The principal branch of the inverse cosine function maps values in the
                               1804                 :                :      * range [-1, 1] to values in the range [0, Pi], so we should reject any
                               1805                 :                :      * inputs outside that range and the result will always be finite.
                               1806                 :                :      */
                               1807   [ #  #  #  # ]:              0 :     if (arg1 < -1.0 || arg1 > 1.0)
 8502                          1808         [ #  # ]:              0 :         ereport(ERROR,
                               1809                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1810                 :                :                  errmsg("input is out of range")));
                               1811                 :                : 
 3940                          1812                 :              0 :     result = acos(arg1);
 2457                          1813         [ #  # ]:              0 :     if (unlikely(isinf(result)))
                               1814                 :              0 :         float_overflow_error();
                               1815                 :                : 
 9592                          1816                 :              0 :     PG_RETURN_FLOAT8(result);
                               1817                 :                : }
                               1818                 :                : 
                               1819                 :                : 
                               1820                 :                : /*
                               1821                 :                :  *      dasin           - returns the arcsin of arg1 (radians)
                               1822                 :                :  */
                               1823                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1824                 :CBC          55 : dasin(PG_FUNCTION_ARGS)
                               1825                 :                : {
                               1826                 :             55 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1827                 :                :     float8      result;
                               1828                 :                : 
                               1829                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          1830         [ -  + ]:             55 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        1831                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1832                 :                : 
                               1833                 :                :     /*
                               1834                 :                :      * The principal branch of the inverse sine function maps values in the
                               1835                 :                :      * range [-1, 1] to values in the range [-Pi/2, Pi/2], so we should reject
                               1836                 :                :      * any inputs outside that range and the result will always be finite.
                               1837                 :                :      */
 3940 tgl@sss.pgh.pa.us        1838   [ +  -  -  + ]:CBC          55 :     if (arg1 < -1.0 || arg1 > 1.0)
 8502 tgl@sss.pgh.pa.us        1839         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1840                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1841                 :                :                  errmsg("input is out of range")));
                               1842                 :                : 
 3940 tgl@sss.pgh.pa.us        1843                 :CBC          55 :     result = asin(arg1);
 2457                          1844         [ -  + ]:             55 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        1845                 :UBC           0 :         float_overflow_error();
                               1846                 :                : 
 9592 tgl@sss.pgh.pa.us        1847                 :CBC          55 :     PG_RETURN_FLOAT8(result);
                               1848                 :                : }
                               1849                 :                : 
                               1850                 :                : 
                               1851                 :                : /*
                               1852                 :                :  *      datan           - returns the arctan of arg1 (radians)
                               1853                 :                :  */
                               1854                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1855                 :UBC           0 : datan(PG_FUNCTION_ARGS)
                               1856                 :                : {
                               1857                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1858                 :                :     float8      result;
                               1859                 :                : 
                               1860                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          1861         [ #  # ]:              0 :     if (isnan(arg1))
                               1862                 :              0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1863                 :                : 
                               1864                 :                :     /*
                               1865                 :                :      * The principal branch of the inverse tangent function maps all inputs to
                               1866                 :                :      * values in the range [-Pi/2, Pi/2], so the result should always be
                               1867                 :                :      * finite, even if the input is infinite.
                               1868                 :                :      */
 9592                          1869                 :              0 :     result = atan(arg1);
 2457                          1870         [ #  # ]:              0 :     if (unlikely(isinf(result)))
                               1871                 :              0 :         float_overflow_error();
                               1872                 :                : 
 9592                          1873                 :              0 :     PG_RETURN_FLOAT8(result);
                               1874                 :                : }
                               1875                 :                : 
                               1876                 :                : 
                               1877                 :                : /*
                               1878                 :                :  *      atan2           - returns the arctan of arg1/arg2 (radians)
                               1879                 :                :  */
                               1880                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1881                 :CBC          20 : datan2(PG_FUNCTION_ARGS)
                               1882                 :                : {
                               1883                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1884                 :             20 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               1885                 :                :     float8      result;
                               1886                 :                : 
                               1887                 :                :     /* Per the POSIX spec, return NaN if either input is NaN */
 3940                          1888   [ +  -  -  + ]:             20 :     if (isnan(arg1) || isnan(arg2))
 3940 tgl@sss.pgh.pa.us        1889                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1890                 :                : 
                               1891                 :                :     /*
                               1892                 :                :      * atan2 maps all inputs to values in the range [-Pi, Pi], so the result
                               1893                 :                :      * should always be finite, even if the inputs are infinite.
                               1894                 :                :      */
 9592 tgl@sss.pgh.pa.us        1895                 :CBC          20 :     result = atan2(arg1, arg2);
 2457                          1896         [ -  + ]:             20 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        1897                 :UBC           0 :         float_overflow_error();
                               1898                 :                : 
 9592 tgl@sss.pgh.pa.us        1899                 :CBC          20 :     PG_RETURN_FLOAT8(result);
                               1900                 :                : }
                               1901                 :                : 
                               1902                 :                : 
                               1903                 :                : /*
                               1904                 :                :  *      dcos            - returns the cosine of arg1 (radians)
                               1905                 :                :  */
                               1906                 :                : Datum
                               1907                 :            589 : dcos(PG_FUNCTION_ARGS)
                               1908                 :                : {
                               1909                 :            589 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1910                 :                :     float8      result;
                               1911                 :                : 
                               1912                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          1913         [ -  + ]:            589 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        1914                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1915                 :                : 
                               1916                 :                :     /*
                               1917                 :                :      * cos() is periodic and so theoretically can work for all finite inputs,
                               1918                 :                :      * but some implementations may choose to throw error if the input is so
                               1919                 :                :      * large that there are no significant digits in the result.  So we should
                               1920                 :                :      * check for errors.  POSIX allows an error to be reported either via
                               1921                 :                :      * errno or via fetestexcept(), but currently we only support checking
                               1922                 :                :      * errno.  (fetestexcept() is rumored to report underflow unreasonably
                               1923                 :                :      * early on some platforms, so it's not clear that believing it would be a
                               1924                 :                :      * net improvement anyway.)
                               1925                 :                :      *
                               1926                 :                :      * For infinite inputs, POSIX specifies that the trigonometric functions
                               1927                 :                :      * should return a domain error; but we won't notice that unless the
                               1928                 :                :      * platform reports via errno, so also explicitly test for infinite
                               1929                 :                :      * inputs.
                               1930                 :                :      */
 7643 bruce@momjian.us         1931                 :CBC         589 :     errno = 0;
 9592 tgl@sss.pgh.pa.us        1932                 :            589 :     result = cos(arg1);
 3940                          1933   [ +  -  -  + ]:            589 :     if (errno != 0 || isinf(arg1))
 8502 tgl@sss.pgh.pa.us        1934         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1935                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1936                 :                :                  errmsg("input is out of range")));
 2457 tgl@sss.pgh.pa.us        1937         [ -  + ]:CBC         589 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        1938                 :UBC           0 :         float_overflow_error();
                               1939                 :                : 
 9592 tgl@sss.pgh.pa.us        1940                 :CBC         589 :     PG_RETURN_FLOAT8(result);
                               1941                 :                : }
                               1942                 :                : 
                               1943                 :                : 
                               1944                 :                : /*
                               1945                 :                :  *      dcot            - returns the cotangent of arg1 (radians)
                               1946                 :                :  */
                               1947                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1948                 :UBC           0 : dcot(PG_FUNCTION_ARGS)
                               1949                 :                : {
                               1950                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1951                 :                :     float8      result;
                               1952                 :                : 
                               1953                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          1954         [ #  # ]:              0 :     if (isnan(arg1))
                               1955                 :              0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1956                 :                : 
                               1957                 :                :     /* Be sure to throw an error if the input is infinite --- see dcos() */
 7643 bruce@momjian.us         1958                 :              0 :     errno = 0;
 9592 tgl@sss.pgh.pa.us        1959                 :              0 :     result = tan(arg1);
 3940                          1960   [ #  #  #  # ]:              0 :     if (errno != 0 || isinf(arg1))
 8502                          1961         [ #  # ]:              0 :         ereport(ERROR,
                               1962                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1963                 :                :                  errmsg("input is out of range")));
                               1964                 :                : 
 9592                          1965                 :              0 :     result = 1.0 / result;
                               1966                 :                :     /* Not checking for overflow because cot(0) == Inf */
                               1967                 :                : 
                               1968                 :              0 :     PG_RETURN_FLOAT8(result);
                               1969                 :                : }
                               1970                 :                : 
                               1971                 :                : 
                               1972                 :                : /*
                               1973                 :                :  *      dsin            - returns the sine of arg1 (radians)
                               1974                 :                :  */
                               1975                 :                : Datum
 9592 tgl@sss.pgh.pa.us        1976                 :CBC         518 : dsin(PG_FUNCTION_ARGS)
                               1977                 :                : {
                               1978                 :            518 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               1979                 :                :     float8      result;
                               1980                 :                : 
                               1981                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          1982         [ -  + ]:            518 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        1983                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               1984                 :                : 
                               1985                 :                :     /* Be sure to throw an error if the input is infinite --- see dcos() */
 7643 bruce@momjian.us         1986                 :CBC         518 :     errno = 0;
 9592 tgl@sss.pgh.pa.us        1987                 :            518 :     result = sin(arg1);
 3940                          1988   [ +  -  -  + ]:            518 :     if (errno != 0 || isinf(arg1))
 8502 tgl@sss.pgh.pa.us        1989         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1990                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               1991                 :                :                  errmsg("input is out of range")));
 2457 tgl@sss.pgh.pa.us        1992         [ -  + ]:CBC         518 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        1993                 :UBC           0 :         float_overflow_error();
                               1994                 :                : 
 9592 tgl@sss.pgh.pa.us        1995                 :CBC         518 :     PG_RETURN_FLOAT8(result);
                               1996                 :                : }
                               1997                 :                : 
                               1998                 :                : 
                               1999                 :                : /*
                               2000                 :                :  *      dtan            - returns the tangent of arg1 (radians)
                               2001                 :                :  */
                               2002                 :                : Datum
 9592 tgl@sss.pgh.pa.us        2003                 :UBC           0 : dtan(PG_FUNCTION_ARGS)
                               2004                 :                : {
                               2005                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2006                 :                :     float8      result;
                               2007                 :                : 
                               2008                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
 3940                          2009         [ #  # ]:              0 :     if (isnan(arg1))
                               2010                 :              0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2011                 :                : 
                               2012                 :                :     /* Be sure to throw an error if the input is infinite --- see dcos() */
 7643 bruce@momjian.us         2013                 :              0 :     errno = 0;
 9592 tgl@sss.pgh.pa.us        2014                 :              0 :     result = tan(arg1);
 3940                          2015   [ #  #  #  # ]:              0 :     if (errno != 0 || isinf(arg1))
 8502                          2016         [ #  # ]:              0 :         ereport(ERROR,
                               2017                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2018                 :                :                  errmsg("input is out of range")));
                               2019                 :                :     /* Not checking for overflow because tan(pi/2) == Inf */
                               2020                 :                : 
 9592                          2021                 :              0 :     PG_RETURN_FLOAT8(result);
                               2022                 :                : }
                               2023                 :                : 
                               2024                 :                : 
                               2025                 :                : /* ========== DEGREE-BASED TRIGONOMETRIC FUNCTIONS ========== */
                               2026                 :                : 
                               2027                 :                : 
                               2028                 :                : /*
                               2029                 :                :  * Initialize the cached constants declared at the head of this file
                               2030                 :                :  * (sin_30 etc).  The fact that we need those at all, let alone need this
                               2031                 :                :  * Rube-Goldberg-worthy method of initializing them, is because there are
                               2032                 :                :  * compilers out there that will precompute expressions such as sin(constant)
                               2033                 :                :  * using a sin() function different from what will be used at runtime.  If we
                               2034                 :                :  * want exact results, we must ensure that none of the scaling constants used
                               2035                 :                :  * in the degree-based trig functions are computed that way.  To do so, we
                               2036                 :                :  * compute them from the variables degree_c_thirty etc, which are also really
                               2037                 :                :  * constants, but the compiler cannot assume that.
                               2038                 :                :  *
                               2039                 :                :  * Other hazards we are trying to forestall with this kluge include the
                               2040                 :                :  * possibility that compilers will rearrange the expressions, or compute
                               2041                 :                :  * some intermediate results in registers wider than a standard double.
                               2042                 :                :  *
                               2043                 :                :  * In the places where we use these constants, the typical pattern is like
                               2044                 :                :  *      volatile float8 sin_x = sin(x * RADIANS_PER_DEGREE);
                               2045                 :                :  *      return (sin_x / sin_30);
                               2046                 :                :  * where we hope to get a value of exactly 1.0 from the division when x = 30.
                               2047                 :                :  * The volatile temporary variable is needed on machines with wide float
                               2048                 :                :  * registers, to ensure that the result of sin(x) is rounded to double width
                               2049                 :                :  * the same as the value of sin_30 has been.  Experimentation with gcc shows
                               2050                 :                :  * that marking the temp variable volatile is necessary to make the store and
                               2051                 :                :  * reload actually happen; hopefully the same trick works for other compilers.
                               2052                 :                :  * (gcc's documentation suggests using the -ffloat-store compiler switch to
                               2053                 :                :  * ensure this, but that is compiler-specific and it also pessimizes code in
                               2054                 :                :  * many places where we don't care about this.)
                               2055                 :                :  */
                               2056                 :                : static void
 3846 tgl@sss.pgh.pa.us        2057                 :CBC           4 : init_degree_constants(void)
                               2058                 :                : {
                               2059                 :              4 :     sin_30 = sin(degree_c_thirty * RADIANS_PER_DEGREE);
                               2060                 :              4 :     one_minus_cos_60 = 1.0 - cos(degree_c_sixty * RADIANS_PER_DEGREE);
                               2061                 :              4 :     asin_0_5 = asin(degree_c_one_half);
                               2062                 :              4 :     acos_0_5 = acos(degree_c_one_half);
                               2063                 :              4 :     atan_1_0 = atan(degree_c_one);
                               2064                 :              4 :     tan_45 = sind_q1(degree_c_forty_five) / cosd_q1(degree_c_forty_five);
                               2065                 :              4 :     cot_45 = cosd_q1(degree_c_forty_five) / sind_q1(degree_c_forty_five);
 3939                          2066                 :              4 :     degree_consts_set = true;
                               2067                 :              4 : }
                               2068                 :                : 
                               2069                 :                : #define INIT_DEGREE_CONSTANTS() \
                               2070                 :                : do { \
                               2071                 :                :     if (!degree_consts_set) \
                               2072                 :                :         init_degree_constants(); \
                               2073                 :                : } while(0)
                               2074                 :                : 
                               2075                 :                : 
                               2076                 :                : /*
                               2077                 :                :  *      asind_q1        - returns the inverse sine of x in degrees, for x in
                               2078                 :                :  *                        the range [0, 1].  The result is an angle in the
                               2079                 :                :  *                        first quadrant --- [0, 90] degrees.
                               2080                 :                :  *
                               2081                 :                :  *                        For the 3 special case inputs (0, 0.5 and 1), this
                               2082                 :                :  *                        function will return exact values (0, 30 and 90
                               2083                 :                :  *                        degrees respectively).
                               2084                 :                :  */
                               2085                 :                : static double
 3940                          2086                 :             56 : asind_q1(double x)
                               2087                 :                : {
                               2088                 :                :     /*
                               2089                 :                :      * Stitch together inverse sine and cosine functions for the ranges [0,
                               2090                 :                :      * 0.5] and (0.5, 1].  Each expression below is guaranteed to return
                               2091                 :                :      * exactly 30 for x=0.5, so the result is a continuous monotonic function
                               2092                 :                :      * over the full range.
                               2093                 :                :      */
                               2094         [ +  + ]:             56 :     if (x <= 0.5)
                               2095                 :                :     {
 3845                          2096                 :             32 :         volatile float8 asin_x = asin(x);
                               2097                 :                : 
                               2098                 :             32 :         return (asin_x / asin_0_5) * 30.0;
                               2099                 :                :     }
                               2100                 :                :     else
                               2101                 :                :     {
                               2102                 :             24 :         volatile float8 acos_x = acos(x);
                               2103                 :                : 
                               2104                 :             24 :         return 90.0 - (acos_x / acos_0_5) * 60.0;
                               2105                 :                :     }
                               2106                 :                : }
                               2107                 :                : 
                               2108                 :                : 
                               2109                 :                : /*
                               2110                 :                :  *      acosd_q1        - returns the inverse cosine of x in degrees, for x in
                               2111                 :                :  *                        the range [0, 1].  The result is an angle in the
                               2112                 :                :  *                        first quadrant --- [0, 90] degrees.
                               2113                 :                :  *
                               2114                 :                :  *                        For the 3 special case inputs (0, 0.5 and 1), this
                               2115                 :                :  *                        function will return exact values (0, 60 and 90
                               2116                 :                :  *                        degrees respectively).
                               2117                 :                :  */
                               2118                 :                : static double
 3940                          2119                 :             24 : acosd_q1(double x)
                               2120                 :                : {
                               2121                 :                :     /*
                               2122                 :                :      * Stitch together inverse sine and cosine functions for the ranges [0,
                               2123                 :                :      * 0.5] and (0.5, 1].  Each expression below is guaranteed to return
                               2124                 :                :      * exactly 60 for x=0.5, so the result is a continuous monotonic function
                               2125                 :                :      * over the full range.
                               2126                 :                :      */
                               2127         [ +  + ]:             24 :     if (x <= 0.5)
                               2128                 :                :     {
 3845                          2129                 :             16 :         volatile float8 asin_x = asin(x);
                               2130                 :                : 
                               2131                 :             16 :         return 90.0 - (asin_x / asin_0_5) * 30.0;
                               2132                 :                :     }
                               2133                 :                :     else
                               2134                 :                :     {
                               2135                 :              8 :         volatile float8 acos_x = acos(x);
                               2136                 :                : 
                               2137                 :              8 :         return (acos_x / acos_0_5) * 60.0;
                               2138                 :                :     }
                               2139                 :                : }
                               2140                 :                : 
                               2141                 :                : 
                               2142                 :                : /*
                               2143                 :                :  *      dacosd          - returns the arccos of arg1 (degrees)
                               2144                 :                :  */
                               2145                 :                : Datum
 3940                          2146                 :             40 : dacosd(PG_FUNCTION_ARGS)
                               2147                 :                : {
                               2148                 :             40 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2149                 :                :     float8      result;
                               2150                 :                : 
                               2151                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
                               2152         [ -  + ]:             40 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2153                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2154                 :                : 
 3939 tgl@sss.pgh.pa.us        2155         [ -  + ]:CBC          40 :     INIT_DEGREE_CONSTANTS();
                               2156                 :                : 
                               2157                 :                :     /*
                               2158                 :                :      * The principal branch of the inverse cosine function maps values in the
                               2159                 :                :      * range [-1, 1] to values in the range [0, 180], so we should reject any
                               2160                 :                :      * inputs outside that range and the result will always be finite.
                               2161                 :                :      */
 3940                          2162   [ +  -  -  + ]:             40 :     if (arg1 < -1.0 || arg1 > 1.0)
 3940 tgl@sss.pgh.pa.us        2163         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2164                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2165                 :                :                  errmsg("input is out of range")));
                               2166                 :                : 
 3940 tgl@sss.pgh.pa.us        2167         [ +  + ]:CBC          40 :     if (arg1 >= 0.0)
                               2168                 :             24 :         result = acosd_q1(arg1);
                               2169                 :                :     else
                               2170                 :             16 :         result = 90.0 + asind_q1(-arg1);
                               2171                 :                : 
 2457                          2172         [ -  + ]:             40 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2173                 :UBC           0 :         float_overflow_error();
                               2174                 :                : 
 3940 tgl@sss.pgh.pa.us        2175                 :CBC          40 :     PG_RETURN_FLOAT8(result);
                               2176                 :                : }
                               2177                 :                : 
                               2178                 :                : 
                               2179                 :                : /*
                               2180                 :                :  *      dasind          - returns the arcsin of arg1 (degrees)
                               2181                 :                :  */
                               2182                 :                : Datum
                               2183                 :             40 : dasind(PG_FUNCTION_ARGS)
                               2184                 :                : {
                               2185                 :             40 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2186                 :                :     float8      result;
                               2187                 :                : 
                               2188                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
                               2189         [ -  + ]:             40 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2190                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2191                 :                : 
 3939 tgl@sss.pgh.pa.us        2192         [ -  + ]:CBC          40 :     INIT_DEGREE_CONSTANTS();
                               2193                 :                : 
                               2194                 :                :     /*
                               2195                 :                :      * The principal branch of the inverse sine function maps values in the
                               2196                 :                :      * range [-1, 1] to values in the range [-90, 90], so we should reject any
                               2197                 :                :      * inputs outside that range and the result will always be finite.
                               2198                 :                :      */
 3940                          2199   [ +  -  -  + ]:             40 :     if (arg1 < -1.0 || arg1 > 1.0)
 3940 tgl@sss.pgh.pa.us        2200         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2201                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2202                 :                :                  errmsg("input is out of range")));
                               2203                 :                : 
 3940 tgl@sss.pgh.pa.us        2204         [ +  + ]:CBC          40 :     if (arg1 >= 0.0)
                               2205                 :             24 :         result = asind_q1(arg1);
                               2206                 :                :     else
                               2207                 :             16 :         result = -asind_q1(-arg1);
                               2208                 :                : 
 2457                          2209         [ -  + ]:             40 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2210                 :UBC           0 :         float_overflow_error();
                               2211                 :                : 
 3940 tgl@sss.pgh.pa.us        2212                 :CBC          40 :     PG_RETURN_FLOAT8(result);
                               2213                 :                : }
                               2214                 :                : 
                               2215                 :                : 
                               2216                 :                : /*
                               2217                 :                :  *      datand          - returns the arctan of arg1 (degrees)
                               2218                 :                :  */
                               2219                 :                : Datum
                               2220                 :             40 : datand(PG_FUNCTION_ARGS)
                               2221                 :                : {
                               2222                 :             40 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2223                 :                :     float8      result;
                               2224                 :                :     volatile float8 atan_arg1;
                               2225                 :                : 
                               2226                 :                :     /* Per the POSIX spec, return NaN if the input is NaN */
                               2227         [ -  + ]:             40 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2228                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2229                 :                : 
 3939 tgl@sss.pgh.pa.us        2230         [ -  + ]:CBC          40 :     INIT_DEGREE_CONSTANTS();
                               2231                 :                : 
                               2232                 :                :     /*
                               2233                 :                :      * The principal branch of the inverse tangent function maps all inputs to
                               2234                 :                :      * values in the range [-90, 90], so the result should always be finite,
                               2235                 :                :      * even if the input is infinite.  Additionally, we take care to ensure
                               2236                 :                :      * than when arg1 is 1, the result is exactly 45.
                               2237                 :                :      */
 3845                          2238                 :             40 :     atan_arg1 = atan(arg1);
                               2239                 :             40 :     result = (atan_arg1 / atan_1_0) * 45.0;
                               2240                 :                : 
 2457                          2241         [ -  + ]:             40 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2242                 :UBC           0 :         float_overflow_error();
                               2243                 :                : 
 3940 tgl@sss.pgh.pa.us        2244                 :CBC          40 :     PG_RETURN_FLOAT8(result);
                               2245                 :                : }
                               2246                 :                : 
                               2247                 :                : 
                               2248                 :                : /*
                               2249                 :                :  *      atan2d          - returns the arctan of arg1/arg2 (degrees)
                               2250                 :                :  */
                               2251                 :                : Datum
                               2252                 :             40 : datan2d(PG_FUNCTION_ARGS)
                               2253                 :                : {
                               2254                 :             40 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2255                 :             40 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               2256                 :                :     float8      result;
                               2257                 :                :     volatile float8 atan2_arg1_arg2;
                               2258                 :                : 
                               2259                 :                :     /* Per the POSIX spec, return NaN if either input is NaN */
                               2260   [ +  -  -  + ]:             40 :     if (isnan(arg1) || isnan(arg2))
 3940 tgl@sss.pgh.pa.us        2261                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2262                 :                : 
 3939 tgl@sss.pgh.pa.us        2263         [ -  + ]:CBC          40 :     INIT_DEGREE_CONSTANTS();
                               2264                 :                : 
                               2265                 :                :     /*
                               2266                 :                :      * atan2d maps all inputs to values in the range [-180, 180], so the
                               2267                 :                :      * result should always be finite, even if the inputs are infinite.
                               2268                 :                :      *
                               2269                 :                :      * Note: this coding assumes that atan(1.0) is a suitable scaling constant
                               2270                 :                :      * to get an exact result from atan2().  This might well fail on us at
                               2271                 :                :      * some point, requiring us to decide exactly what inputs we think we're
                               2272                 :                :      * going to guarantee an exact result for.
                               2273                 :                :      */
 3845                          2274                 :             40 :     atan2_arg1_arg2 = atan2(arg1, arg2);
                               2275                 :             40 :     result = (atan2_arg1_arg2 / atan_1_0) * 45.0;
                               2276                 :                : 
 2457                          2277         [ -  + ]:             40 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2278                 :UBC           0 :         float_overflow_error();
                               2279                 :                : 
 3940 tgl@sss.pgh.pa.us        2280                 :CBC          40 :     PG_RETURN_FLOAT8(result);
                               2281                 :                : }
                               2282                 :                : 
                               2283                 :                : 
                               2284                 :                : /*
                               2285                 :                :  *      sind_0_to_30    - returns the sine of an angle that lies between 0 and
                               2286                 :                :  *                        30 degrees.  This will return exactly 0 when x is 0,
                               2287                 :                :  *                        and exactly 0.5 when x is 30 degrees.
                               2288                 :                :  */
                               2289                 :                : static double
                               2290                 :            212 : sind_0_to_30(double x)
                               2291                 :                : {
 3845                          2292                 :            212 :     volatile float8 sin_x = sin(x * RADIANS_PER_DEGREE);
                               2293                 :                : 
                               2294                 :            212 :     return (sin_x / sin_30) / 2.0;
                               2295                 :                : }
                               2296                 :                : 
                               2297                 :                : 
                               2298                 :                : /*
                               2299                 :                :  *      cosd_0_to_60    - returns the cosine of an angle that lies between 0
                               2300                 :                :  *                        and 60 degrees.  This will return exactly 1 when x
                               2301                 :                :  *                        is 0, and exactly 0.5 when x is 60 degrees.
                               2302                 :                :  */
                               2303                 :                : static double
 3940                          2304                 :            356 : cosd_0_to_60(double x)
                               2305                 :                : {
 3845                          2306                 :            356 :     volatile float8 one_minus_cos_x = 1.0 - cos(x * RADIANS_PER_DEGREE);
                               2307                 :                : 
 3938                          2308                 :            356 :     return 1.0 - (one_minus_cos_x / one_minus_cos_60) / 2.0;
                               2309                 :                : }
                               2310                 :                : 
                               2311                 :                : 
                               2312                 :                : /*
                               2313                 :                :  *      sind_q1         - returns the sine of an angle in the first quadrant
                               2314                 :                :  *                        (0 to 90 degrees).
                               2315                 :                :  */
                               2316                 :                : static double
 3940                          2317                 :            284 : sind_q1(double x)
                               2318                 :                : {
                               2319                 :                :     /*
                               2320                 :                :      * Stitch together the sine and cosine functions for the ranges [0, 30]
                               2321                 :                :      * and (30, 90].  These guarantee to return exact answers at their
                               2322                 :                :      * endpoints, so the overall result is a continuous monotonic function
                               2323                 :                :      * that gives exact results when x = 0, 30 and 90 degrees.
                               2324                 :                :      */
                               2325         [ +  + ]:            284 :     if (x <= 30.0)
                               2326                 :            140 :         return sind_0_to_30(x);
                               2327                 :                :     else
                               2328                 :            144 :         return cosd_0_to_60(90.0 - x);
                               2329                 :                : }
                               2330                 :                : 
                               2331                 :                : 
                               2332                 :                : /*
                               2333                 :                :  *      cosd_q1         - returns the cosine of an angle in the first quadrant
                               2334                 :                :  *                        (0 to 90 degrees).
                               2335                 :                :  */
                               2336                 :                : static double
                               2337                 :            284 : cosd_q1(double x)
                               2338                 :                : {
                               2339                 :                :     /*
                               2340                 :                :      * Stitch together the sine and cosine functions for the ranges [0, 60]
                               2341                 :                :      * and (60, 90].  These guarantee to return exact answers at their
                               2342                 :                :      * endpoints, so the overall result is a continuous monotonic function
                               2343                 :                :      * that gives exact results when x = 0, 60 and 90 degrees.
                               2344                 :                :      */
                               2345         [ +  + ]:            284 :     if (x <= 60.0)
                               2346                 :            212 :         return cosd_0_to_60(x);
                               2347                 :                :     else
                               2348                 :             72 :         return sind_0_to_30(90.0 - x);
                               2349                 :                : }
                               2350                 :                : 
                               2351                 :                : 
                               2352                 :                : /*
                               2353                 :                :  *      dcosd           - returns the cosine of arg1 (degrees)
                               2354                 :                :  */
                               2355                 :                : Datum
                               2356                 :            132 : dcosd(PG_FUNCTION_ARGS)
                               2357                 :                : {
                               2358                 :            132 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2359                 :                :     float8      result;
 3939                          2360                 :            132 :     int         sign = 1;
                               2361                 :                : 
                               2362                 :                :     /*
                               2363                 :                :      * Per the POSIX spec, return NaN if the input is NaN and throw an error
                               2364                 :                :      * if the input is infinite.
                               2365                 :                :      */
 3940                          2366         [ -  + ]:            132 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2367                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2368                 :                : 
 3940 tgl@sss.pgh.pa.us        2369         [ -  + ]:CBC         132 :     if (isinf(arg1))
 3940 tgl@sss.pgh.pa.us        2370         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2371                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2372                 :                :                  errmsg("input is out of range")));
                               2373                 :                : 
 3939 tgl@sss.pgh.pa.us        2374         [ -  + ]:CBC         132 :     INIT_DEGREE_CONSTANTS();
                               2375                 :                : 
                               2376                 :                :     /* Reduce the range of the input to [0,90] degrees */
 3940                          2377                 :            132 :     arg1 = fmod(arg1, 360.0);
                               2378                 :                : 
                               2379         [ -  + ]:            132 :     if (arg1 < 0.0)
                               2380                 :                :     {
                               2381                 :                :         /* cosd(-x) = cosd(x) */
 3940 tgl@sss.pgh.pa.us        2382                 :UBC           0 :         arg1 = -arg1;
                               2383                 :                :     }
                               2384                 :                : 
 3940 tgl@sss.pgh.pa.us        2385         [ +  + ]:CBC         132 :     if (arg1 > 180.0)
                               2386                 :                :     {
                               2387                 :                :         /* cosd(360-x) = cosd(x) */
                               2388                 :             36 :         arg1 = 360.0 - arg1;
                               2389                 :                :     }
                               2390                 :                : 
                               2391         [ +  + ]:            132 :     if (arg1 > 90.0)
                               2392                 :                :     {
                               2393                 :                :         /* cosd(180-x) = -cosd(x) */
                               2394                 :             36 :         arg1 = 180.0 - arg1;
                               2395                 :             36 :         sign = -sign;
                               2396                 :                :     }
                               2397                 :                : 
                               2398                 :            132 :     result = sign * cosd_q1(arg1);
                               2399                 :                : 
 2457                          2400         [ -  + ]:            132 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2401                 :UBC           0 :         float_overflow_error();
                               2402                 :                : 
 3940 tgl@sss.pgh.pa.us        2403                 :CBC         132 :     PG_RETURN_FLOAT8(result);
                               2404                 :                : }
                               2405                 :                : 
                               2406                 :                : 
                               2407                 :                : /*
                               2408                 :                :  *      dcotd           - returns the cotangent of arg1 (degrees)
                               2409                 :                :  */
                               2410                 :                : Datum
                               2411                 :             72 : dcotd(PG_FUNCTION_ARGS)
                               2412                 :                : {
                               2413                 :             72 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2414                 :                :     float8      result;
                               2415                 :                :     volatile float8 cot_arg1;
 3939                          2416                 :             72 :     int         sign = 1;
                               2417                 :                : 
                               2418                 :                :     /*
                               2419                 :                :      * Per the POSIX spec, return NaN if the input is NaN and throw an error
                               2420                 :                :      * if the input is infinite.
                               2421                 :                :      */
 3940                          2422         [ -  + ]:             72 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2423                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2424                 :                : 
 3940 tgl@sss.pgh.pa.us        2425         [ -  + ]:CBC          72 :     if (isinf(arg1))
 3940 tgl@sss.pgh.pa.us        2426         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2427                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2428                 :                :                  errmsg("input is out of range")));
                               2429                 :                : 
 3939 tgl@sss.pgh.pa.us        2430         [ -  + ]:CBC          72 :     INIT_DEGREE_CONSTANTS();
                               2431                 :                : 
                               2432                 :                :     /* Reduce the range of the input to [0,90] degrees */
 3940                          2433                 :             72 :     arg1 = fmod(arg1, 360.0);
                               2434                 :                : 
                               2435         [ -  + ]:             72 :     if (arg1 < 0.0)
                               2436                 :                :     {
                               2437                 :                :         /* cotd(-x) = -cotd(x) */
 3940 tgl@sss.pgh.pa.us        2438                 :UBC           0 :         arg1 = -arg1;
                               2439                 :              0 :         sign = -sign;
                               2440                 :                :     }
                               2441                 :                : 
 3940 tgl@sss.pgh.pa.us        2442         [ +  + ]:CBC          72 :     if (arg1 > 180.0)
                               2443                 :                :     {
                               2444                 :                :         /* cotd(360-x) = -cotd(x) */
                               2445                 :             24 :         arg1 = 360.0 - arg1;
                               2446                 :             24 :         sign = -sign;
                               2447                 :                :     }
                               2448                 :                : 
                               2449         [ +  + ]:             72 :     if (arg1 > 90.0)
                               2450                 :                :     {
                               2451                 :                :         /* cotd(180-x) = -cotd(x) */
                               2452                 :             24 :         arg1 = 180.0 - arg1;
                               2453                 :             24 :         sign = -sign;
                               2454                 :                :     }
                               2455                 :                : 
 3845                          2456                 :             72 :     cot_arg1 = cosd_q1(arg1) / sind_q1(arg1);
                               2457                 :             72 :     result = sign * (cot_arg1 / cot_45);
                               2458                 :                : 
                               2459                 :                :     /*
                               2460                 :                :      * On some machines we get cotd(270) = minus zero, but this isn't always
                               2461                 :                :      * true.  For portability, and because the user constituency for this
                               2462                 :                :      * function probably doesn't want minus zero, force it to plain zero.
                               2463                 :                :      */
 3939                          2464         [ +  + ]:             72 :     if (result == 0.0)
                               2465                 :             16 :         result = 0.0;
                               2466                 :                : 
                               2467                 :                :     /* Not checking for overflow because cotd(0) == Inf */
                               2468                 :                : 
 3940                          2469                 :             72 :     PG_RETURN_FLOAT8(result);
                               2470                 :                : }
                               2471                 :                : 
                               2472                 :                : 
                               2473                 :                : /*
                               2474                 :                :  *      dsind           - returns the sine of arg1 (degrees)
                               2475                 :                :  */
                               2476                 :                : Datum
                               2477                 :            132 : dsind(PG_FUNCTION_ARGS)
                               2478                 :                : {
                               2479                 :            132 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2480                 :                :     float8      result;
 3939                          2481                 :            132 :     int         sign = 1;
                               2482                 :                : 
                               2483                 :                :     /*
                               2484                 :                :      * Per the POSIX spec, return NaN if the input is NaN and throw an error
                               2485                 :                :      * if the input is infinite.
                               2486                 :                :      */
 3940                          2487         [ -  + ]:            132 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2488                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2489                 :                : 
 3940 tgl@sss.pgh.pa.us        2490         [ -  + ]:CBC         132 :     if (isinf(arg1))
 3940 tgl@sss.pgh.pa.us        2491         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2492                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2493                 :                :                  errmsg("input is out of range")));
                               2494                 :                : 
 3939 tgl@sss.pgh.pa.us        2495         [ +  + ]:CBC         132 :     INIT_DEGREE_CONSTANTS();
                               2496                 :                : 
                               2497                 :                :     /* Reduce the range of the input to [0,90] degrees */
 3940                          2498                 :            132 :     arg1 = fmod(arg1, 360.0);
                               2499                 :                : 
                               2500         [ -  + ]:            132 :     if (arg1 < 0.0)
                               2501                 :                :     {
                               2502                 :                :         /* sind(-x) = -sind(x) */
 3940 tgl@sss.pgh.pa.us        2503                 :UBC           0 :         arg1 = -arg1;
                               2504                 :              0 :         sign = -sign;
                               2505                 :                :     }
                               2506                 :                : 
 3940 tgl@sss.pgh.pa.us        2507         [ +  + ]:CBC         132 :     if (arg1 > 180.0)
                               2508                 :                :     {
                               2509                 :                :         /* sind(360-x) = -sind(x) */
                               2510                 :             36 :         arg1 = 360.0 - arg1;
                               2511                 :             36 :         sign = -sign;
                               2512                 :                :     }
                               2513                 :                : 
                               2514         [ +  + ]:            132 :     if (arg1 > 90.0)
                               2515                 :                :     {
                               2516                 :                :         /* sind(180-x) = sind(x) */
                               2517                 :             36 :         arg1 = 180.0 - arg1;
                               2518                 :                :     }
                               2519                 :                : 
                               2520                 :            132 :     result = sign * sind_q1(arg1);
                               2521                 :                : 
 2457                          2522         [ -  + ]:            132 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2523                 :UBC           0 :         float_overflow_error();
                               2524                 :                : 
 3940 tgl@sss.pgh.pa.us        2525                 :CBC         132 :     PG_RETURN_FLOAT8(result);
                               2526                 :                : }
                               2527                 :                : 
                               2528                 :                : 
                               2529                 :                : /*
                               2530                 :                :  *      dtand           - returns the tangent of arg1 (degrees)
                               2531                 :                :  */
                               2532                 :                : Datum
                               2533                 :             72 : dtand(PG_FUNCTION_ARGS)
                               2534                 :                : {
                               2535                 :             72 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2536                 :                :     float8      result;
                               2537                 :                :     volatile float8 tan_arg1;
 3939                          2538                 :             72 :     int         sign = 1;
                               2539                 :                : 
                               2540                 :                :     /*
                               2541                 :                :      * Per the POSIX spec, return NaN if the input is NaN and throw an error
                               2542                 :                :      * if the input is infinite.
                               2543                 :                :      */
 3940                          2544         [ -  + ]:             72 :     if (isnan(arg1))
 3940 tgl@sss.pgh.pa.us        2545                 :UBC           0 :         PG_RETURN_FLOAT8(get_float8_nan());
                               2546                 :                : 
 3940 tgl@sss.pgh.pa.us        2547         [ -  + ]:CBC          72 :     if (isinf(arg1))
 3940 tgl@sss.pgh.pa.us        2548         [ #  # ]:UBC           0 :         ereport(ERROR,
                               2549                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2550                 :                :                  errmsg("input is out of range")));
                               2551                 :                : 
 3939 tgl@sss.pgh.pa.us        2552         [ -  + ]:CBC          72 :     INIT_DEGREE_CONSTANTS();
                               2553                 :                : 
                               2554                 :                :     /* Reduce the range of the input to [0,90] degrees */
 3940                          2555                 :             72 :     arg1 = fmod(arg1, 360.0);
                               2556                 :                : 
                               2557         [ -  + ]:             72 :     if (arg1 < 0.0)
                               2558                 :                :     {
                               2559                 :                :         /* tand(-x) = -tand(x) */
 3940 tgl@sss.pgh.pa.us        2560                 :UBC           0 :         arg1 = -arg1;
                               2561                 :              0 :         sign = -sign;
                               2562                 :                :     }
                               2563                 :                : 
 3940 tgl@sss.pgh.pa.us        2564         [ +  + ]:CBC          72 :     if (arg1 > 180.0)
                               2565                 :                :     {
                               2566                 :                :         /* tand(360-x) = -tand(x) */
                               2567                 :             24 :         arg1 = 360.0 - arg1;
                               2568                 :             24 :         sign = -sign;
                               2569                 :                :     }
                               2570                 :                : 
                               2571         [ +  + ]:             72 :     if (arg1 > 90.0)
                               2572                 :                :     {
                               2573                 :                :         /* tand(180-x) = -tand(x) */
                               2574                 :             24 :         arg1 = 180.0 - arg1;
                               2575                 :             24 :         sign = -sign;
                               2576                 :                :     }
                               2577                 :                : 
 3845                          2578                 :             72 :     tan_arg1 = sind_q1(arg1) / cosd_q1(arg1);
                               2579                 :             72 :     result = sign * (tan_arg1 / tan_45);
                               2580                 :                : 
                               2581                 :                :     /*
                               2582                 :                :      * On some machines we get tand(180) = minus zero, but this isn't always
                               2583                 :                :      * true.  For portability, and because the user constituency for this
                               2584                 :                :      * function probably doesn't want minus zero, force it to plain zero.
                               2585                 :                :      */
 3939                          2586         [ +  + ]:             72 :     if (result == 0.0)
                               2587                 :             24 :         result = 0.0;
                               2588                 :                : 
                               2589                 :                :     /* Not checking for overflow because tand(90) == Inf */
                               2590                 :                : 
 3940                          2591                 :             72 :     PG_RETURN_FLOAT8(result);
                               2592                 :                : }
                               2593                 :                : 
                               2594                 :                : 
                               2595                 :                : /*
                               2596                 :                :  *      degrees     - returns degrees converted from radians
                               2597                 :                :  */
                               2598                 :                : Datum
 9592                          2599                 :             40 : degrees(PG_FUNCTION_ARGS)
                               2600                 :                : {
                               2601                 :             40 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2602                 :                : 
 3021 tomas.vondra@postgre     2603                 :             40 :     PG_RETURN_FLOAT8(float8_div(arg1, RADIANS_PER_DEGREE));
                               2604                 :                : }
                               2605                 :                : 
                               2606                 :                : 
                               2607                 :                : /*
                               2608                 :                :  *      dpi             - returns the constant PI
                               2609                 :                :  */
                               2610                 :                : Datum
 9592 tgl@sss.pgh.pa.us        2611                 :             36 : dpi(PG_FUNCTION_ARGS)
                               2612                 :                : {
                               2613                 :             36 :     PG_RETURN_FLOAT8(M_PI);
                               2614                 :                : }
                               2615                 :                : 
                               2616                 :                : 
                               2617                 :                : /*
                               2618                 :                :  *      radians     - returns radians converted from degrees
                               2619                 :                :  */
                               2620                 :                : Datum
                               2621                 :            955 : radians(PG_FUNCTION_ARGS)
                               2622                 :                : {
                               2623                 :            955 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2624                 :                : 
 3021 tomas.vondra@postgre     2625                 :            955 :     PG_RETURN_FLOAT8(float8_mul(arg1, RADIANS_PER_DEGREE));
                               2626                 :                : }
                               2627                 :                : 
                               2628                 :                : 
                               2629                 :                : /* ========== HYPERBOLIC FUNCTIONS ========== */
                               2630                 :                : 
                               2631                 :                : 
                               2632                 :                : /*
                               2633                 :                :  *      dsinh           - returns the hyperbolic sine of arg1
                               2634                 :                :  */
                               2635                 :                : Datum
 2795 tgl@sss.pgh.pa.us        2636                 :             20 : dsinh(PG_FUNCTION_ARGS)
                               2637                 :                : {
                               2638                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2639                 :                :     float8      result;
                               2640                 :                : 
                               2641                 :             20 :     errno = 0;
                               2642                 :             20 :     result = sinh(arg1);
                               2643                 :                : 
                               2644                 :                :     /*
                               2645                 :                :      * if an ERANGE error occurs, it means there is an overflow.  For sinh,
                               2646                 :                :      * the result should be either -infinity or infinity, depending on the
                               2647                 :                :      * sign of arg1.
                               2648                 :                :      */
                               2649         [ -  + ]:             20 :     if (errno == ERANGE)
                               2650                 :                :     {
 2795 tgl@sss.pgh.pa.us        2651         [ #  # ]:UBC           0 :         if (arg1 < 0)
                               2652                 :              0 :             result = -get_float8_infinity();
                               2653                 :                :         else
                               2654                 :              0 :             result = get_float8_infinity();
                               2655                 :                :     }
                               2656                 :                : 
 2795 tgl@sss.pgh.pa.us        2657                 :CBC          20 :     PG_RETURN_FLOAT8(result);
                               2658                 :                : }
                               2659                 :                : 
                               2660                 :                : 
                               2661                 :                : /*
                               2662                 :                :  *      dcosh           - returns the hyperbolic cosine of arg1
                               2663                 :                :  */
                               2664                 :                : Datum
                               2665                 :             20 : dcosh(PG_FUNCTION_ARGS)
                               2666                 :                : {
                               2667                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2668                 :                :     float8      result;
                               2669                 :                : 
                               2670                 :             20 :     errno = 0;
                               2671                 :             20 :     result = cosh(arg1);
                               2672                 :                : 
                               2673                 :                :     /*
                               2674                 :                :      * if an ERANGE error occurs, it means there is an overflow.  As cosh is
                               2675                 :                :      * always positive, it always means the result is positive infinity.
                               2676                 :                :      */
                               2677         [ -  + ]:             20 :     if (errno == ERANGE)
 2795 tgl@sss.pgh.pa.us        2678                 :UBC           0 :         result = get_float8_infinity();
                               2679                 :                : 
 2457 tgl@sss.pgh.pa.us        2680         [ -  + ]:CBC          20 :     if (unlikely(result == 0.0))
 2457 tgl@sss.pgh.pa.us        2681                 :UBC           0 :         float_underflow_error();
                               2682                 :                : 
 2795 tgl@sss.pgh.pa.us        2683                 :CBC          20 :     PG_RETURN_FLOAT8(result);
                               2684                 :                : }
                               2685                 :                : 
                               2686                 :                : /*
                               2687                 :                :  *      dtanh           - returns the hyperbolic tangent of arg1
                               2688                 :                :  */
                               2689                 :                : Datum
                               2690                 :             20 : dtanh(PG_FUNCTION_ARGS)
                               2691                 :                : {
                               2692                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2693                 :                :     float8      result;
                               2694                 :                : 
                               2695                 :                :     /*
                               2696                 :                :      * For tanh, we don't need an errno check because it never overflows.
                               2697                 :                :      */
                               2698                 :             20 :     result = tanh(arg1);
                               2699                 :                : 
 2457                          2700         [ -  + ]:             20 :     if (unlikely(isinf(result)))
 2457 tgl@sss.pgh.pa.us        2701                 :UBC           0 :         float_overflow_error();
                               2702                 :                : 
 2795 tgl@sss.pgh.pa.us        2703                 :CBC          20 :     PG_RETURN_FLOAT8(result);
                               2704                 :                : }
                               2705                 :                : 
                               2706                 :                : /*
                               2707                 :                :  *      dasinh          - returns the inverse hyperbolic sine of arg1
                               2708                 :                :  */
                               2709                 :                : Datum
                               2710                 :             20 : dasinh(PG_FUNCTION_ARGS)
                               2711                 :                : {
                               2712                 :             20 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2713                 :                :     float8      result;
                               2714                 :                : 
                               2715                 :                :     /*
                               2716                 :                :      * For asinh, we don't need an errno check because it never overflows.
                               2717                 :                :      */
                               2718                 :             20 :     result = asinh(arg1);
                               2719                 :                : 
                               2720                 :             20 :     PG_RETURN_FLOAT8(result);
                               2721                 :                : }
                               2722                 :                : 
                               2723                 :                : /*
                               2724                 :                :  *      dacosh          - returns the inverse hyperbolic cosine of arg1
                               2725                 :                :  */
                               2726                 :                : Datum
                               2727                 :             14 : dacosh(PG_FUNCTION_ARGS)
                               2728                 :                : {
                               2729                 :             14 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2730                 :                :     float8      result;
                               2731                 :                : 
                               2732                 :                :     /*
                               2733                 :                :      * acosh is only defined for inputs >= 1.0.  By checking this ourselves,
                               2734                 :                :      * we need not worry about checking for an EDOM error, which is a good
                               2735                 :                :      * thing because some implementations will report that for NaN. Otherwise,
                               2736                 :                :      * no error is possible.
                               2737                 :                :      */
                               2738         [ +  + ]:             14 :     if (arg1 < 1.0)
                               2739         [ +  - ]:              4 :         ereport(ERROR,
                               2740                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2741                 :                :                  errmsg("input is out of range")));
                               2742                 :                : 
                               2743                 :             10 :     result = acosh(arg1);
                               2744                 :                : 
                               2745                 :             10 :     PG_RETURN_FLOAT8(result);
                               2746                 :                : }
                               2747                 :                : 
                               2748                 :                : /*
                               2749                 :                :  *      datanh          - returns the inverse hyperbolic tangent of arg1
                               2750                 :                :  */
                               2751                 :                : Datum
                               2752                 :             18 : datanh(PG_FUNCTION_ARGS)
                               2753                 :                : {
                               2754                 :             18 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2755                 :                :     float8      result;
                               2756                 :                : 
                               2757                 :                :     /*
                               2758                 :                :      * atanh is only defined for inputs between -1 and 1.  By checking this
                               2759                 :                :      * ourselves, we need not worry about checking for an EDOM error, which is
                               2760                 :                :      * a good thing because some implementations will report that for NaN.
                               2761                 :                :      */
                               2762   [ +  +  +  + ]:             18 :     if (arg1 < -1.0 || arg1 > 1.0)
                               2763         [ +  - ]:              8 :         ereport(ERROR,
                               2764                 :                :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               2765                 :                :                  errmsg("input is out of range")));
                               2766                 :                : 
                               2767                 :                :     /*
                               2768                 :                :      * Also handle the infinity cases ourselves; this is helpful because old
                               2769                 :                :      * glibc versions may produce the wrong errno for this.  All other inputs
                               2770                 :                :      * cannot produce an error.
                               2771                 :                :      */
                               2772         [ -  + ]:             10 :     if (arg1 == -1.0)
 2795 tgl@sss.pgh.pa.us        2773                 :UBC           0 :         result = -get_float8_infinity();
 2795 tgl@sss.pgh.pa.us        2774         [ -  + ]:CBC          10 :     else if (arg1 == 1.0)
 2795 tgl@sss.pgh.pa.us        2775                 :UBC           0 :         result = get_float8_infinity();
                               2776                 :                :     else
 2795 tgl@sss.pgh.pa.us        2777                 :CBC          10 :         result = atanh(arg1);
                               2778                 :                : 
                               2779                 :             10 :     PG_RETURN_FLOAT8(result);
                               2780                 :                : }
                               2781                 :                : 
                               2782                 :                : 
                               2783                 :                : /* ========== ERROR FUNCTIONS ========== */
                               2784                 :                : 
                               2785                 :                : 
                               2786                 :                : /*
                               2787                 :                :  *      derf            - returns the error function: erf(arg1)
                               2788                 :                :  */
                               2789                 :                : Datum
 1332 dean.a.rasheed@gmail     2790                 :           4088 : derf(PG_FUNCTION_ARGS)
                               2791                 :                : {
                               2792                 :           4088 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2793                 :                :     float8      result;
                               2794                 :                : 
                               2795                 :                :     /*
                               2796                 :                :      * For erf, we don't need an errno check because it never overflows.
                               2797                 :                :      */
                               2798                 :           4088 :     result = erf(arg1);
                               2799                 :                : 
                               2800         [ -  + ]:           4088 :     if (unlikely(isinf(result)))
 1332 dean.a.rasheed@gmail     2801                 :UBC           0 :         float_overflow_error();
                               2802                 :                : 
 1332 dean.a.rasheed@gmail     2803                 :CBC        4088 :     PG_RETURN_FLOAT8(result);
                               2804                 :                : }
                               2805                 :                : 
                               2806                 :                : /*
                               2807                 :                :  *      derfc           - returns the complementary error function: 1 - erf(arg1)
                               2808                 :                :  */
                               2809                 :                : Datum
                               2810                 :             88 : derfc(PG_FUNCTION_ARGS)
                               2811                 :                : {
                               2812                 :             88 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2813                 :                :     float8      result;
                               2814                 :                : 
                               2815                 :                :     /*
                               2816                 :                :      * For erfc, we don't need an errno check because it never overflows.
                               2817                 :                :      */
                               2818                 :             88 :     result = erfc(arg1);
                               2819                 :                : 
                               2820         [ -  + ]:             88 :     if (unlikely(isinf(result)))
 1332 dean.a.rasheed@gmail     2821                 :UBC           0 :         float_overflow_error();
                               2822                 :                : 
 1332 dean.a.rasheed@gmail     2823                 :CBC          88 :     PG_RETURN_FLOAT8(result);
                               2824                 :                : }
                               2825                 :                : 
                               2826                 :                : 
                               2827                 :                : /* ========== GAMMA FUNCTIONS ========== */
                               2828                 :                : 
                               2829                 :                : 
                               2830                 :                : /*
                               2831                 :                :  *      dgamma          - returns the gamma function of arg1
                               2832                 :                :  */
                               2833                 :                : Datum
  589                          2834                 :             52 : dgamma(PG_FUNCTION_ARGS)
                               2835                 :                : {
                               2836                 :             52 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2837                 :                :     float8      result;
                               2838                 :                : 
                               2839                 :                :     /*
                               2840                 :                :      * Handle NaN and Inf cases explicitly.  This simplifies the overflow
                               2841                 :                :      * checks on platforms that do not set errno.
                               2842                 :                :      */
                               2843         [ +  + ]:             52 :     if (isnan(arg1))
                               2844                 :              4 :         result = arg1;
                               2845         [ +  + ]:             48 :     else if (isinf(arg1))
                               2846                 :                :     {
                               2847                 :                :         /* Per POSIX, an input of -Inf causes a domain error */
                               2848         [ +  + ]:              8 :         if (arg1 < 0)
                               2849                 :                :         {
                               2850                 :              4 :             float_overflow_error();
                               2851                 :                :             result = get_float8_nan();  /* keep compiler quiet */
                               2852                 :                :         }
                               2853                 :                :         else
                               2854                 :              4 :             result = arg1;
                               2855                 :                :     }
                               2856                 :                :     else
                               2857                 :                :     {
                               2858                 :                :         /*
                               2859                 :                :          * Note: the POSIX/C99 gamma function is called "tgamma", not "gamma".
                               2860                 :                :          *
                               2861                 :                :          * On some platforms, tgamma() will not set errno but just return Inf,
                               2862                 :                :          * NaN, or zero to report overflow/underflow; therefore, test those
                               2863                 :                :          * cases explicitly (note that, like the exponential function, the
                               2864                 :                :          * gamma function has no zeros).
                               2865                 :                :          */
                               2866                 :             40 :         errno = 0;
                               2867                 :             40 :         result = tgamma(arg1);
                               2868                 :                : 
                               2869   [ +  +  +  -  :             40 :         if (errno != 0 || isinf(result) || isnan(result))
                                              -  + ]
                               2870                 :                :         {
                               2871         [ +  + ]:             16 :             if (result != 0.0)
                               2872                 :             12 :                 float_overflow_error();
                               2873                 :                :             else
                               2874                 :              4 :                 float_underflow_error();
                               2875                 :                :         }
                               2876         [ -  + ]:             24 :         else if (result == 0.0)
  589 dean.a.rasheed@gmail     2877                 :UBC           0 :             float_underflow_error();
                               2878                 :                :     }
                               2879                 :                : 
  589 dean.a.rasheed@gmail     2880                 :CBC          32 :     PG_RETURN_FLOAT8(result);
                               2881                 :                : }
                               2882                 :                : 
                               2883                 :                : 
                               2884                 :                : /*
                               2885                 :                :  *      dlgamma         - natural logarithm of absolute value of gamma of arg1
                               2886                 :                :  */
                               2887                 :                : Datum
                               2888                 :             59 : dlgamma(PG_FUNCTION_ARGS)
                               2889                 :                : {
                               2890                 :             59 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               2891                 :                :     float8      result;
                               2892                 :                : 
                               2893                 :                :     /* On some versions of AIX, lgamma(NaN) fails with ERANGE */
                               2894                 :                : #if defined(_AIX)
                               2895                 :                :     if (isnan(arg1))
                               2896                 :                :         PG_RETURN_FLOAT8(arg1);
                               2897                 :                : #endif
                               2898                 :                : 
                               2899                 :                :     /*
                               2900                 :                :      * Note: lgamma may not be thread-safe because it may write to a global
                               2901                 :                :      * variable signgam, which may not be thread-local. However, this doesn't
                               2902                 :                :      * matter to us, since we don't use signgam.
                               2903                 :                :      */
                               2904                 :             59 :     errno = 0;
                               2905                 :             59 :     result = lgamma(arg1);
                               2906                 :                : 
                               2907                 :                :     /*
                               2908                 :                :      * If an ERANGE error occurs, it means there was an overflow or a pole
                               2909                 :                :      * error (which happens for zero and negative integer inputs).
                               2910                 :                :      *
                               2911                 :                :      * On some platforms, lgamma() will not set errno but just return infinity
                               2912                 :                :      * to report overflow, but it should never underflow.
                               2913                 :                :      */
                               2914   [ +  +  +  +  :             59 :     if (errno == ERANGE || (isinf(result) && !isinf(arg1)))
                                              -  + ]
                               2915                 :             12 :         float_overflow_error();
                               2916                 :                : 
                               2917                 :             47 :     PG_RETURN_FLOAT8(result);
                               2918                 :                : }
                               2919                 :                : 
                               2920                 :                : 
                               2921                 :                : 
                               2922                 :                : /*
                               2923                 :                :  *      =========================
                               2924                 :                :  *      FLOAT AGGREGATE OPERATORS
                               2925                 :                :  *      =========================
                               2926                 :                :  *
                               2927                 :                :  *      float8_accum        - accumulate for AVG(), variance aggregates, etc.
                               2928                 :                :  *      float4_accum        - same, but input data is float4
                               2929                 :                :  *      float8_avg          - produce final result for float AVG()
                               2930                 :                :  *      float8_var_samp     - produce final result for float VAR_SAMP()
                               2931                 :                :  *      float8_var_pop      - produce final result for float VAR_POP()
                               2932                 :                :  *      float8_stddev_samp  - produce final result for float STDDEV_SAMP()
                               2933                 :                :  *      float8_stddev_pop   - produce final result for float STDDEV_POP()
                               2934                 :                :  *
                               2935                 :                :  * The naive schoolbook implementation of these aggregates works by
                               2936                 :                :  * accumulating sum(X) and sum(X^2).  However, this approach suffers from
                               2937                 :                :  * large rounding errors in the final computation of quantities like the
                               2938                 :                :  * population variance (N*sum(X^2) - sum(X)^2) / N^2, since each of the
                               2939                 :                :  * intermediate terms is potentially very large, while the difference is often
                               2940                 :                :  * quite small.
                               2941                 :                :  *
                               2942                 :                :  * Instead we use the Youngs-Cramer algorithm [1] which works by accumulating
                               2943                 :                :  * Sx=sum(X) and Sxx=sum((X-Sx/N)^2), using a numerically stable algorithm to
                               2944                 :                :  * incrementally update those quantities.  The final computations of each of
                               2945                 :                :  * the aggregate values is then trivial and gives more accurate results (for
                               2946                 :                :  * example, the population variance is just Sxx/N).  This algorithm is also
                               2947                 :                :  * fairly easy to generalize to allow parallel execution without loss of
                               2948                 :                :  * precision (see, for example, [2]).  For more details, and a comparison of
                               2949                 :                :  * this with other algorithms, see [3].
                               2950                 :                :  *
                               2951                 :                :  * The transition datatype for all these aggregates is a 3-element array
                               2952                 :                :  * of float8, holding the values N, Sx, Sxx in that order.
                               2953                 :                :  *
                               2954                 :                :  * Note that we represent N as a float to avoid having to build a special
                               2955                 :                :  * datatype.  Given a reasonable floating-point implementation, there should
                               2956                 :                :  * be no accuracy loss unless N exceeds 2 ^ 52 or so (by which time the
                               2957                 :                :  * user will have doubtless lost interest anyway...)
                               2958                 :                :  *
                               2959                 :                :  * [1] Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms,
                               2960                 :                :  * E. A. Youngs and E. M. Cramer, Technometrics Vol 13, No 3, August 1971.
                               2961                 :                :  *
                               2962                 :                :  * [2] Updating Formulae and a Pairwise Algorithm for Computing Sample
                               2963                 :                :  * Variances, T. F. Chan, G. H. Golub & R. J. LeVeque, COMPSTAT 1982.
                               2964                 :                :  *
                               2965                 :                :  * [3] Numerically Stable Parallel Computation of (Co-)Variance, Erich
                               2966                 :                :  * Schubert and Michael Gertz, Proceedings of the 30th International
                               2967                 :                :  * Conference on Scientific and Statistical Database Management, 2018.
                               2968                 :                :  */
                               2969                 :                : 
                               2970                 :                : static float8 *
 7405 tgl@sss.pgh.pa.us        2971                 :          33643 : check_float8_array(ArrayType *transarray, const char *caller, int n)
                               2972                 :                : {
                               2973                 :                :     /*
                               2974                 :                :      * We expect the input to be an N-element float array; verify that. We
                               2975                 :                :      * don't need to use deconstruct_array() since the array data is just
                               2976                 :                :      * going to look like a C array of N float8 values.
                               2977                 :                :      */
 8837                          2978         [ +  - ]:          33643 :     if (ARR_NDIM(transarray) != 1 ||
 7405                          2979         [ +  - ]:          33643 :         ARR_DIMS(transarray)[0] != n ||
 7658                          2980         [ +  - ]:          33643 :         ARR_HASNULL(transarray) ||
 8837                          2981         [ -  + ]:          33643 :         ARR_ELEMTYPE(transarray) != FLOAT8OID)
 7405 tgl@sss.pgh.pa.us        2982         [ #  # ]:UBC           0 :         elog(ERROR, "%s: expected %d-element float8 array", caller, n);
 9607 tgl@sss.pgh.pa.us        2983         [ -  + ]:CBC       33643 :     return (float8 *) ARR_DATA_PTR(transarray);
                               2984                 :                : }
                               2985                 :                : 
                               2986                 :                : /*
                               2987                 :                :  * float8_combine
                               2988                 :                :  *
                               2989                 :                :  * An aggregate combine function used to combine two 3 fields
                               2990                 :                :  * aggregate transition data into a single transition data.
                               2991                 :                :  * This function is used only in two stage aggregation and
                               2992                 :                :  * shouldn't be called outside aggregate context.
                               2993                 :                :  */
                               2994                 :                : Datum
 3863 rhaas@postgresql.org     2995                 :            271 : float8_combine(PG_FUNCTION_ARGS)
                               2996                 :                : {
                               2997                 :            271 :     ArrayType  *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
                               2998                 :            271 :     ArrayType  *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
                               2999                 :                :     float8     *transvalues1;
                               3000                 :                :     float8     *transvalues2;
                               3001                 :                :     float8      N1,
                               3002                 :                :                 Sx1,
                               3003                 :                :                 Sxx1,
                               3004                 :                :                 N2,
                               3005                 :                :                 Sx2,
                               3006                 :                :                 Sxx2,
                               3007                 :                :                 tmp,
                               3008                 :                :                 N,
                               3009                 :                :                 Sx,
                               3010                 :                :                 Sxx;
                               3011                 :                : 
                               3012                 :            271 :     transvalues1 = check_float8_array(transarray1, "float8_combine", 3);
                               3013                 :            271 :     transvalues2 = check_float8_array(transarray2, "float8_combine", 3);
                               3014                 :                : 
 2952 dean.a.rasheed@gmail     3015                 :            271 :     N1 = transvalues1[0];
                               3016                 :            271 :     Sx1 = transvalues1[1];
                               3017                 :            271 :     Sxx1 = transvalues1[2];
                               3018                 :                : 
                               3019                 :            271 :     N2 = transvalues2[0];
                               3020                 :            271 :     Sx2 = transvalues2[1];
                               3021                 :            271 :     Sxx2 = transvalues2[2];
                               3022                 :                : 
                               3023                 :                :     /*--------------------
                               3024                 :                :      * The transition values combine using a generalization of the
                               3025                 :                :      * Youngs-Cramer algorithm as follows:
                               3026                 :                :      *
                               3027                 :                :      *  N = N1 + N2
                               3028                 :                :      *  Sx = Sx1 + Sx2
                               3029                 :                :      *  Sxx = Sxx1 + Sxx2 + N1 * N2 * (Sx1/N1 - Sx2/N2)^2 / N;
                               3030                 :                :      *
                               3031                 :                :      * It's worth handling the special cases N1 = 0 and N2 = 0 separately
                               3032                 :                :      * since those cases are trivial, and we then don't need to worry about
                               3033                 :                :      * division-by-zero errors in the general case.
                               3034                 :                :      *--------------------
                               3035                 :                :      */
                               3036         [ +  + ]:            271 :     if (N1 == 0.0)
                               3037                 :                :     {
                               3038                 :            261 :         N = N2;
                               3039                 :            261 :         Sx = Sx2;
                               3040                 :            261 :         Sxx = Sxx2;
                               3041                 :                :     }
                               3042         [ +  + ]:             10 :     else if (N2 == 0.0)
                               3043                 :                :     {
                               3044                 :              5 :         N = N1;
                               3045                 :              5 :         Sx = Sx1;
                               3046                 :              5 :         Sxx = Sxx1;
                               3047                 :                :     }
                               3048                 :                :     else
                               3049                 :                :     {
                               3050                 :              5 :         N = N1 + N2;
                               3051                 :              5 :         Sx = float8_pl(Sx1, Sx2);
                               3052                 :              5 :         tmp = Sx1 / N1 - Sx2 / N2;
                               3053                 :              5 :         Sxx = Sxx1 + Sxx2 + N1 * N2 * tmp * tmp / N;
 2457 tgl@sss.pgh.pa.us        3054   [ -  +  -  -  :              5 :         if (unlikely(isinf(Sxx)) && !isinf(Sxx1) && !isinf(Sxx2))
                                              -  - ]
 2457 tgl@sss.pgh.pa.us        3055                 :UBC           0 :             float_overflow_error();
                               3056                 :                :     }
                               3057                 :                : 
                               3058                 :                :     /*
                               3059                 :                :      * If we're invoked as an aggregate, we can cheat and modify our first
                               3060                 :                :      * parameter in-place to reduce palloc overhead. Otherwise we construct a
                               3061                 :                :      * new array with the updated transition data and return it.
                               3062                 :                :      */
 2952 dean.a.rasheed@gmail     3063         [ +  + ]:CBC         271 :     if (AggCheckCallContext(fcinfo, NULL))
                               3064                 :                :     {
                               3065                 :            256 :         transvalues1[0] = N;
                               3066                 :            256 :         transvalues1[1] = Sx;
                               3067                 :            256 :         transvalues1[2] = Sxx;
                               3068                 :                : 
                               3069                 :            256 :         PG_RETURN_ARRAYTYPE_P(transarray1);
                               3070                 :                :     }
                               3071                 :                :     else
                               3072                 :                :     {
                               3073                 :                :         Datum       transdatums[3];
                               3074                 :                :         ArrayType  *result;
                               3075                 :                : 
                               3076                 :             15 :         transdatums[0] = Float8GetDatumFast(N);
                               3077                 :             15 :         transdatums[1] = Float8GetDatumFast(Sx);
                               3078                 :             15 :         transdatums[2] = Float8GetDatumFast(Sxx);
                               3079                 :                : 
  752 msawada@postgresql.o     3080                 :             15 :         result = construct_array_builtin(transdatums, 3, FLOAT8OID);
                               3081                 :                : 
 2952 dean.a.rasheed@gmail     3082                 :             15 :         PG_RETURN_ARRAYTYPE_P(result);
                               3083                 :                :     }
                               3084                 :                : }
                               3085                 :                : 
                               3086                 :                : Datum
 9607 tgl@sss.pgh.pa.us        3087                 :          31009 : float8_accum(PG_FUNCTION_ARGS)
                               3088                 :                : {
                               3089                 :          31009 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3090                 :          31009 :     float8      newval = PG_GETARG_FLOAT8(1);
                               3091                 :                :     float8     *transvalues;
                               3092                 :                :     float8      N,
                               3093                 :                :                 Sx,
                               3094                 :                :                 Sxx,
                               3095                 :                :                 tmp;
                               3096                 :                : 
 7405                          3097                 :          31009 :     transvalues = check_float8_array(transarray, "float8_accum", 3);
 2952 dean.a.rasheed@gmail     3098                 :          31009 :     N = transvalues[0];
                               3099                 :          31009 :     Sx = transvalues[1];
                               3100                 :          31009 :     Sxx = transvalues[2];
                               3101                 :                : 
                               3102                 :                :     /*
                               3103                 :                :      * Use the Youngs-Cramer algorithm to incorporate the new value into the
                               3104                 :                :      * transition values.
                               3105                 :                :      */
                               3106                 :          31009 :     N += 1.0;
                               3107                 :          31009 :     Sx += newval;
                               3108         [ +  + ]:          31009 :     if (transvalues[0] > 0.0)
                               3109                 :                :     {
                               3110                 :          30636 :         tmp = newval * N - Sx;
                               3111                 :          30636 :         Sxx += tmp * tmp / (N * transvalues[0]);
                               3112                 :                : 
                               3113                 :                :         /*
                               3114                 :                :          * Overflow check.  We only report an overflow error when finite
                               3115                 :                :          * inputs lead to infinite results.  Note also that Sxx should be NaN
                               3116                 :                :          * if any of the inputs are infinite, so we intentionally prevent Sxx
                               3117                 :                :          * from becoming infinite.
                               3118                 :                :          */
                               3119   [ +  +  -  + ]:          30636 :         if (isinf(Sx) || isinf(Sxx))
                               3120                 :                :         {
                               3121   [ +  +  +  +  :             16 :             if (!isinf(transvalues[1]) && !isinf(newval))
                                              -  + ]
 2457 tgl@sss.pgh.pa.us        3122                 :UBC           0 :                 float_overflow_error();
                               3123                 :                : 
 2952 dean.a.rasheed@gmail     3124                 :CBC          16 :             Sxx = get_float8_nan();
                               3125                 :                :         }
                               3126                 :                :     }
                               3127                 :                :     else
                               3128                 :                :     {
                               3129                 :                :         /*
                               3130                 :                :          * At the first input, we normally can leave Sxx as 0.  However, if
                               3131                 :                :          * the first input is Inf or NaN, we'd better force Sxx to NaN;
                               3132                 :                :          * otherwise we will falsely report variance zero when there are no
                               3133                 :                :          * more inputs.
                               3134                 :                :          */
 2336 tgl@sss.pgh.pa.us        3135   [ +  +  +  + ]:            373 :         if (isnan(newval) || isinf(newval))
                               3136                 :             32 :             Sxx = get_float8_nan();
                               3137                 :                :     }
                               3138                 :                : 
                               3139                 :                :     /*
                               3140                 :                :      * If we're invoked as an aggregate, we can cheat and modify our first
                               3141                 :                :      * parameter in-place to reduce palloc overhead. Otherwise we construct a
                               3142                 :                :      * new array with the updated transition data and return it.
                               3143                 :                :      */
 6114                          3144         [ +  + ]:          31009 :     if (AggCheckCallContext(fcinfo, NULL))
                               3145                 :                :     {
 2952 dean.a.rasheed@gmail     3146                 :          31004 :         transvalues[0] = N;
                               3147                 :          31004 :         transvalues[1] = Sx;
                               3148                 :          31004 :         transvalues[2] = Sxx;
                               3149                 :                : 
 7883 neilc@samurai.com        3150                 :          31004 :         PG_RETURN_ARRAYTYPE_P(transarray);
                               3151                 :                :     }
                               3152                 :                :     else
                               3153                 :                :     {
                               3154                 :                :         Datum       transdatums[3];
                               3155                 :                :         ArrayType  *result;
                               3156                 :                : 
 2952 dean.a.rasheed@gmail     3157                 :              5 :         transdatums[0] = Float8GetDatumFast(N);
                               3158                 :              5 :         transdatums[1] = Float8GetDatumFast(Sx);
                               3159                 :              5 :         transdatums[2] = Float8GetDatumFast(Sxx);
                               3160                 :                : 
  752 msawada@postgresql.o     3161                 :              5 :         result = construct_array_builtin(transdatums, 3, FLOAT8OID);
                               3162                 :                : 
 7883 neilc@samurai.com        3163                 :              5 :         PG_RETURN_ARRAYTYPE_P(result);
                               3164                 :                :     }
                               3165                 :                : }
                               3166                 :                : 
                               3167                 :                : Datum
 9607 tgl@sss.pgh.pa.us        3168                 :            192 : float4_accum(PG_FUNCTION_ARGS)
                               3169                 :                : {
                               3170                 :            192 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3171                 :                : 
                               3172                 :                :     /* do computations as float8 */
 7247 bruce@momjian.us         3173                 :            192 :     float8      newval = PG_GETARG_FLOAT4(1);
                               3174                 :                :     float8     *transvalues;
                               3175                 :                :     float8      N,
                               3176                 :                :                 Sx,
                               3177                 :                :                 Sxx,
                               3178                 :                :                 tmp;
                               3179                 :                : 
 7405 tgl@sss.pgh.pa.us        3180                 :            192 :     transvalues = check_float8_array(transarray, "float4_accum", 3);
 2952 dean.a.rasheed@gmail     3181                 :            192 :     N = transvalues[0];
                               3182                 :            192 :     Sx = transvalues[1];
                               3183                 :            192 :     Sxx = transvalues[2];
                               3184                 :                : 
                               3185                 :                :     /*
                               3186                 :                :      * Use the Youngs-Cramer algorithm to incorporate the new value into the
                               3187                 :                :      * transition values.
                               3188                 :                :      */
                               3189                 :            192 :     N += 1.0;
                               3190                 :            192 :     Sx += newval;
                               3191         [ +  + ]:            192 :     if (transvalues[0] > 0.0)
                               3192                 :                :     {
                               3193                 :            136 :         tmp = newval * N - Sx;
                               3194                 :            136 :         Sxx += tmp * tmp / (N * transvalues[0]);
                               3195                 :                : 
                               3196                 :                :         /*
                               3197                 :                :          * Overflow check.  We only report an overflow error when finite
                               3198                 :                :          * inputs lead to infinite results.  Note also that Sxx should be NaN
                               3199                 :                :          * if any of the inputs are infinite, so we intentionally prevent Sxx
                               3200                 :                :          * from becoming infinite.
                               3201                 :                :          */
                               3202   [ +  -  -  + ]:            136 :         if (isinf(Sx) || isinf(Sxx))
                               3203                 :                :         {
 2952 dean.a.rasheed@gmail     3204   [ #  #  #  #  :UBC           0 :             if (!isinf(transvalues[1]) && !isinf(newval))
                                              #  # ]
 2457 tgl@sss.pgh.pa.us        3205                 :              0 :                 float_overflow_error();
                               3206                 :                : 
 2952 dean.a.rasheed@gmail     3207                 :              0 :             Sxx = get_float8_nan();
                               3208                 :                :         }
                               3209                 :                :     }
                               3210                 :                :     else
                               3211                 :                :     {
                               3212                 :                :         /*
                               3213                 :                :          * At the first input, we normally can leave Sxx as 0.  However, if
                               3214                 :                :          * the first input is Inf or NaN, we'd better force Sxx to NaN;
                               3215                 :                :          * otherwise we will falsely report variance zero when there are no
                               3216                 :                :          * more inputs.
                               3217                 :                :          */
 2336 tgl@sss.pgh.pa.us        3218   [ +  +  +  + ]:CBC          56 :         if (isnan(newval) || isinf(newval))
                               3219                 :             16 :             Sxx = get_float8_nan();
                               3220                 :                :     }
                               3221                 :                : 
                               3222                 :                :     /*
                               3223                 :                :      * If we're invoked as an aggregate, we can cheat and modify our first
                               3224                 :                :      * parameter in-place to reduce palloc overhead. Otherwise we construct a
                               3225                 :                :      * new array with the updated transition data and return it.
                               3226                 :                :      */
 6114                          3227         [ +  - ]:            192 :     if (AggCheckCallContext(fcinfo, NULL))
                               3228                 :                :     {
 2952 dean.a.rasheed@gmail     3229                 :            192 :         transvalues[0] = N;
                               3230                 :            192 :         transvalues[1] = Sx;
                               3231                 :            192 :         transvalues[2] = Sxx;
                               3232                 :                : 
 7883 neilc@samurai.com        3233                 :            192 :         PG_RETURN_ARRAYTYPE_P(transarray);
                               3234                 :                :     }
                               3235                 :                :     else
                               3236                 :                :     {
                               3237                 :                :         Datum       transdatums[3];
                               3238                 :                :         ArrayType  *result;
                               3239                 :                : 
 2952 dean.a.rasheed@gmail     3240                 :UBC           0 :         transdatums[0] = Float8GetDatumFast(N);
                               3241                 :              0 :         transdatums[1] = Float8GetDatumFast(Sx);
                               3242                 :              0 :         transdatums[2] = Float8GetDatumFast(Sxx);
                               3243                 :                : 
  752 msawada@postgresql.o     3244                 :              0 :         result = construct_array_builtin(transdatums, 3, FLOAT8OID);
                               3245                 :                : 
 7883 neilc@samurai.com        3246                 :              0 :         PG_RETURN_ARRAYTYPE_P(result);
                               3247                 :                :     }
                               3248                 :                : }
                               3249                 :                : 
                               3250                 :                : Datum
 9607 tgl@sss.pgh.pa.us        3251                 :CBC         337 : float8_avg(PG_FUNCTION_ARGS)
                               3252                 :                : {
                               3253                 :            337 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3254                 :                :     float8     *transvalues;
                               3255                 :                :     float8      N,
                               3256                 :                :                 Sx;
                               3257                 :                : 
 7405                          3258                 :            337 :     transvalues = check_float8_array(transarray, "float8_avg", 3);
 9607                          3259                 :            337 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3260                 :            337 :     Sx = transvalues[1];
                               3261                 :                :     /* ignore Sxx */
                               3262                 :                : 
                               3263                 :                :     /* SQL defines AVG of no values to be NULL */
 9607 tgl@sss.pgh.pa.us        3264         [ +  + ]:            337 :     if (N == 0.0)
                               3265                 :              8 :         PG_RETURN_NULL();
                               3266                 :                : 
 2952 dean.a.rasheed@gmail     3267                 :            329 :     PG_RETURN_FLOAT8(Sx / N);
                               3268                 :                : }
                               3269                 :                : 
                               3270                 :                : Datum
 7545 neilc@samurai.com        3271                 :             56 : float8_var_pop(PG_FUNCTION_ARGS)
                               3272                 :                : {
 9607 tgl@sss.pgh.pa.us        3273                 :             56 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3274                 :                :     float8     *transvalues;
                               3275                 :                :     float8      N,
                               3276                 :                :                 Sxx;
                               3277                 :                : 
 7405                          3278                 :             56 :     transvalues = check_float8_array(transarray, "float8_var_pop", 3);
 7545 neilc@samurai.com        3279                 :             56 :     N = transvalues[0];
                               3280                 :                :     /* ignore Sx */
 2952 dean.a.rasheed@gmail     3281                 :             56 :     Sxx = transvalues[2];
                               3282                 :                : 
                               3283                 :                :     /* Population variance is undefined when N is 0, so return NULL */
 7545 neilc@samurai.com        3284         [ -  + ]:             56 :     if (N == 0.0)
 7545 neilc@samurai.com        3285                 :UBC           0 :         PG_RETURN_NULL();
                               3286                 :                : 
                               3287                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               3288                 :                : 
 2952 dean.a.rasheed@gmail     3289                 :CBC          56 :     PG_RETURN_FLOAT8(Sxx / N);
                               3290                 :                : }
                               3291                 :                : 
                               3292                 :                : Datum
 7545 neilc@samurai.com        3293                 :             28 : float8_var_samp(PG_FUNCTION_ARGS)
                               3294                 :                : {
                               3295                 :             28 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3296                 :                :     float8     *transvalues;
                               3297                 :                :     float8      N,
                               3298                 :                :                 Sxx;
                               3299                 :                : 
 7405 tgl@sss.pgh.pa.us        3300                 :             28 :     transvalues = check_float8_array(transarray, "float8_var_samp", 3);
 9607                          3301                 :             28 :     N = transvalues[0];
                               3302                 :                :     /* ignore Sx */
 2952 dean.a.rasheed@gmail     3303                 :             28 :     Sxx = transvalues[2];
                               3304                 :                : 
                               3305                 :                :     /* Sample variance is undefined when N is 0 or 1, so return NULL */
 9607 tgl@sss.pgh.pa.us        3306         [ +  + ]:             28 :     if (N <= 1.0)
 8599                          3307                 :             24 :         PG_RETURN_NULL();
                               3308                 :                : 
                               3309                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               3310                 :                : 
 2952 dean.a.rasheed@gmail     3311                 :              4 :     PG_RETURN_FLOAT8(Sxx / (N - 1.0));
                               3312                 :                : }
                               3313                 :                : 
                               3314                 :                : Datum
 7545 neilc@samurai.com        3315                 :             28 : float8_stddev_pop(PG_FUNCTION_ARGS)
                               3316                 :                : {
                               3317                 :             28 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3318                 :                :     float8     *transvalues;
                               3319                 :                :     float8      N,
                               3320                 :                :                 Sxx;
                               3321                 :                : 
 7405 tgl@sss.pgh.pa.us        3322                 :             28 :     transvalues = check_float8_array(transarray, "float8_stddev_pop", 3);
 7545 neilc@samurai.com        3323                 :             28 :     N = transvalues[0];
                               3324                 :                :     /* ignore Sx */
 2952 dean.a.rasheed@gmail     3325                 :             28 :     Sxx = transvalues[2];
                               3326                 :                : 
                               3327                 :                :     /* Population stddev is undefined when N is 0, so return NULL */
 7545 neilc@samurai.com        3328         [ -  + ]:             28 :     if (N == 0.0)
 7545 neilc@samurai.com        3329                 :UBC           0 :         PG_RETURN_NULL();
                               3330                 :                : 
                               3331                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               3332                 :                : 
 2952 dean.a.rasheed@gmail     3333                 :CBC          28 :     PG_RETURN_FLOAT8(sqrt(Sxx / N));
                               3334                 :                : }
                               3335                 :                : 
                               3336                 :                : Datum
 7545 neilc@samurai.com        3337                 :             32 : float8_stddev_samp(PG_FUNCTION_ARGS)
                               3338                 :                : {
 9607 tgl@sss.pgh.pa.us        3339                 :             32 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3340                 :                :     float8     *transvalues;
                               3341                 :                :     float8      N,
                               3342                 :                :                 Sxx;
                               3343                 :                : 
 7405                          3344                 :             32 :     transvalues = check_float8_array(transarray, "float8_stddev_samp", 3);
 9607                          3345                 :             32 :     N = transvalues[0];
                               3346                 :                :     /* ignore Sx */
 2952 dean.a.rasheed@gmail     3347                 :             32 :     Sxx = transvalues[2];
                               3348                 :                : 
                               3349                 :                :     /* Sample stddev is undefined when N is 0 or 1, so return NULL */
 9607 tgl@sss.pgh.pa.us        3350         [ +  + ]:             32 :     if (N <= 1.0)
 8599                          3351                 :             24 :         PG_RETURN_NULL();
                               3352                 :                : 
                               3353                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               3354                 :                : 
 2952 dean.a.rasheed@gmail     3355                 :              8 :     PG_RETURN_FLOAT8(sqrt(Sxx / (N - 1.0)));
                               3356                 :                : }
                               3357                 :                : 
                               3358                 :                : /*
                               3359                 :                :  *      =========================
                               3360                 :                :  *      SQL2003 BINARY AGGREGATES
                               3361                 :                :  *      =========================
                               3362                 :                :  *
                               3363                 :                :  * As with the preceding aggregates, we use the Youngs-Cramer algorithm to
                               3364                 :                :  * reduce rounding errors in the aggregate final functions.
                               3365                 :                :  *
                               3366                 :                :  * The transition datatype for all these aggregates is an 8-element array of
                               3367                 :                :  * float8, holding the values N, Sx=sum(X), Sxx=sum((X-Sx/N)^2), Sy=sum(Y),
                               3368                 :                :  * Syy=sum((Y-Sy/N)^2), Sxy=sum((X-Sx/N)*(Y-Sy/N)), commonX, and commonY
                               3369                 :                :  * in that order.
                               3370                 :                :  *
                               3371                 :                :  * commonX is defined as the common X value if all the X values were the same,
                               3372                 :                :  * else NaN; likewise for commonY.  This is useful for deciding whether corr()
                               3373                 :                :  * and related functions should return NULL.  This representation cannot
                               3374                 :                :  * distinguish the-values-were-all-NaN from the-values-were-not-all-the-same,
                               3375                 :                :  * but that's okay because for this purpose we use the IEEE float arithmetic
                               3376                 :                :  * principle that two NaNs are never equal.  The SQL standard doesn't mention
                               3377                 :                :  * NaNs, but it says that NULL is to be returned when N*sum(X*X) equals
                               3378                 :                :  * sum(X)*sum(X) (etc), and that shouldn't be considered true for NaNs.
                               3379                 :                :  * Testing this as written in the spec would be highly subject to roundoff
                               3380                 :                :  * error, so instead we directly track whether all the inputs are equal.
                               3381                 :                :  *
                               3382                 :                :  * Note that Y is the first argument to all these aggregates!
                               3383                 :                :  *
                               3384                 :                :  * It might seem attractive to optimize this by having multiple accumulator
                               3385                 :                :  * functions that only calculate the sums actually needed.  But on most
                               3386                 :                :  * modern machines, a couple of extra floating-point multiplies will be
                               3387                 :                :  * insignificant compared to the other per-tuple overhead, so I've chosen
                               3388                 :                :  * to minimize code space instead.
                               3389                 :                :  */
                               3390                 :                : 
                               3391                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3392                 :           1161 : float8_regr_accum(PG_FUNCTION_ARGS)
                               3393                 :                : {
                               3394                 :           1161 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3395                 :           1161 :     float8      newvalY = PG_GETARG_FLOAT8(1);
                               3396                 :           1161 :     float8      newvalX = PG_GETARG_FLOAT8(2);
                               3397                 :                :     float8     *transvalues;
                               3398                 :                :     float8      N,
                               3399                 :                :                 Sx,
                               3400                 :                :                 Sxx,
                               3401                 :                :                 Sy,
                               3402                 :                :                 Syy,
                               3403                 :                :                 Sxy,
                               3404                 :                :                 commonX,
                               3405                 :                :                 commonY,
                               3406                 :                :                 tmpX,
                               3407                 :                :                 tmpY,
                               3408                 :                :                 scale;
                               3409                 :                : 
  334                          3410                 :           1161 :     transvalues = check_float8_array(transarray, "float8_regr_accum", 8);
 7405                          3411                 :           1161 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3412                 :           1161 :     Sx = transvalues[1];
                               3413                 :           1161 :     Sxx = transvalues[2];
                               3414                 :           1161 :     Sy = transvalues[3];
                               3415                 :           1161 :     Syy = transvalues[4];
                               3416                 :           1161 :     Sxy = transvalues[5];
  334 tgl@sss.pgh.pa.us        3417                 :           1161 :     commonX = transvalues[6];
                               3418                 :           1161 :     commonY = transvalues[7];
                               3419                 :                : 
                               3420                 :                :     /*
                               3421                 :                :      * Use the Youngs-Cramer algorithm to incorporate the new values into the
                               3422                 :                :      * transition values.
                               3423                 :                :      */
 7405                          3424                 :           1161 :     N += 1.0;
 2952 dean.a.rasheed@gmail     3425                 :           1161 :     Sx += newvalX;
                               3426                 :           1161 :     Sy += newvalY;
                               3427         [ +  + ]:           1161 :     if (transvalues[0] > 0.0)
                               3428                 :                :     {
                               3429                 :                :         /*
                               3430                 :                :          * Check to see if we have seen distinct inputs.  We can use a test
                               3431                 :                :          * that's a bit cheaper than float8_ne() because if commonX is already
                               3432                 :                :          * NaN, it does not matter whether the != test returns true or not.
                               3433                 :                :          */
  334 tgl@sss.pgh.pa.us        3434   [ +  +  -  + ]:           1021 :         if (newvalX != commonX || isnan(newvalX))
                               3435                 :            744 :             commonX = get_float8_nan();
                               3436   [ +  +  -  + ]:           1021 :         if (newvalY != commonY || isnan(newvalY))
                               3437                 :            789 :             commonY = get_float8_nan();
                               3438                 :                : 
 2952 dean.a.rasheed@gmail     3439                 :           1021 :         tmpX = newvalX * N - Sx;
                               3440                 :           1021 :         tmpY = newvalY * N - Sy;
                               3441                 :           1021 :         scale = 1.0 / (N * transvalues[0]);
                               3442                 :                : 
                               3443                 :                :         /*
                               3444                 :                :          * If we have not seen distinct inputs, then Sxx, Syy, and/or Sxy
                               3445                 :                :          * should remain zero (since Sx's exact value would be N * commonX,
                               3446                 :                :          * etc).  Updating them would just create the possibility of injecting
                               3447                 :                :          * roundoff error, and we need exact zero results so that the final
                               3448                 :                :          * functions will return NULL in the right cases.
                               3449                 :                :          */
  334 tgl@sss.pgh.pa.us        3450         [ +  + ]:           1021 :         if (isnan(commonX))
                               3451                 :            744 :             Sxx += tmpX * tmpX * scale;
                               3452         [ +  + ]:           1021 :         if (isnan(commonY))
                               3453                 :            789 :             Syy += tmpY * tmpY * scale;
                               3454   [ +  +  +  + ]:           1021 :         if (isnan(commonX) && isnan(commonY))
                               3455                 :            512 :             Sxy += tmpX * tmpY * scale;
   85                          3456   [ +  +  +  -  :            509 :         else if (isnan(newvalX) || isinf(newvalX) ||
                                              +  + ]
                               3457         [ +  + ]:            273 :                  isnan(newvalY) || isinf(newvalY))
                               3458                 :                :         {
                               3459                 :                :             /*
                               3460                 :                :              * If one input has been constant so far, but the other one is Inf
                               3461                 :                :              * or NaN this time, we must force Sxy to NaN to avoid falsely
                               3462                 :                :              * reporting variance zero (compare the special case for the first
                               3463                 :                :              * inputs, below).  Sxx and Syy don't have this issue.
                               3464                 :                :              */
                               3465                 :            240 :             Sxy = get_float8_nan();
                               3466                 :                :         }
                               3467                 :                : 
                               3468                 :                :         /*
                               3469                 :                :          * Overflow check.  We only report an overflow error when finite
                               3470                 :                :          * inputs lead to infinite results.  Note also that Sxx, Syy and Sxy
                               3471                 :                :          * should be NaN if any of the relevant inputs are infinite, so we
                               3472                 :                :          * intentionally prevent them from becoming infinite.
                               3473                 :                :          */
 2952 dean.a.rasheed@gmail     3474   [ +  -  +  -  :           1021 :         if (isinf(Sx) || isinf(Sxx) || isinf(Sy) || isinf(Syy) || isinf(Sxy))
                                     +  +  +  -  -  
                                                 + ]
                               3475                 :                :         {
                               3476   [ +  -  -  + ]:             16 :             if (((isinf(Sx) || isinf(Sxx)) &&
 2952 dean.a.rasheed@gmail     3477   [ #  #  #  # ]:UBC           0 :                  !isinf(transvalues[1]) && !isinf(newvalX)) ||
 2952 dean.a.rasheed@gmail     3478   [ -  +  -  - ]:CBC          16 :                 ((isinf(Sy) || isinf(Syy)) &&
                               3479   [ +  +  +  - ]:             16 :                  !isinf(transvalues[3]) && !isinf(newvalY)) ||
                               3480         [ -  + ]:             16 :                 (isinf(Sxy) &&
 2952 dean.a.rasheed@gmail     3481   [ #  #  #  # ]:UBC           0 :                  !isinf(transvalues[1]) && !isinf(newvalX) &&
                               3482   [ #  #  #  # ]:              0 :                  !isinf(transvalues[3]) && !isinf(newvalY)))
 2457 tgl@sss.pgh.pa.us        3483                 :              0 :                 float_overflow_error();
                               3484                 :                : 
 2952 dean.a.rasheed@gmail     3485         [ -  + ]:CBC          16 :             if (isinf(Sxx))
 2952 dean.a.rasheed@gmail     3486                 :UBC           0 :                 Sxx = get_float8_nan();
 2952 dean.a.rasheed@gmail     3487         [ -  + ]:CBC          16 :             if (isinf(Syy))
 2952 dean.a.rasheed@gmail     3488                 :UBC           0 :                 Syy = get_float8_nan();
 2952 dean.a.rasheed@gmail     3489         [ -  + ]:CBC          16 :             if (isinf(Sxy))
 2952 dean.a.rasheed@gmail     3490                 :UBC           0 :                 Sxy = get_float8_nan();
                               3491                 :                :         }
                               3492                 :                :     }
                               3493                 :                :     else
                               3494                 :                :     {
                               3495                 :                :         /*
                               3496                 :                :          * At the first input, we normally can leave Sxx et al as 0.  However,
                               3497                 :                :          * if the first input is Inf or NaN, we'd better force the dependent
                               3498                 :                :          * sums to NaN; otherwise we will falsely report variance zero when
                               3499                 :                :          * there are no more inputs.
                               3500                 :                :          */
 2336 tgl@sss.pgh.pa.us        3501   [ +  +  +  + ]:CBC         140 :         if (isnan(newvalX) || isinf(newvalX))
                               3502                 :             28 :             Sxx = Sxy = get_float8_nan();
                               3503   [ +  +  +  + ]:            140 :         if (isnan(newvalY) || isinf(newvalY))
                               3504                 :             16 :             Syy = Sxy = get_float8_nan();
                               3505                 :                : 
  334                          3506                 :            140 :         commonX = newvalX;
                               3507                 :            140 :         commonY = newvalY;
                               3508                 :                :     }
                               3509                 :                : 
                               3510                 :                :     /*
                               3511                 :                :      * If we're invoked as an aggregate, we can cheat and modify our first
                               3512                 :                :      * parameter in-place to reduce palloc overhead. Otherwise we construct a
                               3513                 :                :      * new array with the updated transition data and return it.
                               3514                 :                :      */
 6114                          3515         [ +  + ]:           1161 :     if (AggCheckCallContext(fcinfo, NULL))
                               3516                 :                :     {
 7405                          3517                 :           1156 :         transvalues[0] = N;
 2952 dean.a.rasheed@gmail     3518                 :           1156 :         transvalues[1] = Sx;
                               3519                 :           1156 :         transvalues[2] = Sxx;
                               3520                 :           1156 :         transvalues[3] = Sy;
                               3521                 :           1156 :         transvalues[4] = Syy;
                               3522                 :           1156 :         transvalues[5] = Sxy;
  334 tgl@sss.pgh.pa.us        3523                 :           1156 :         transvalues[6] = commonX;
                               3524                 :           1156 :         transvalues[7] = commonY;
                               3525                 :                : 
 7405                          3526                 :           1156 :         PG_RETURN_ARRAYTYPE_P(transarray);
                               3527                 :                :     }
                               3528                 :                :     else
                               3529                 :                :     {
                               3530                 :                :         Datum       transdatums[8];
                               3531                 :                :         ArrayType  *result;
                               3532                 :                : 
                               3533                 :              5 :         transdatums[0] = Float8GetDatumFast(N);
 2952 dean.a.rasheed@gmail     3534                 :              5 :         transdatums[1] = Float8GetDatumFast(Sx);
                               3535                 :              5 :         transdatums[2] = Float8GetDatumFast(Sxx);
                               3536                 :              5 :         transdatums[3] = Float8GetDatumFast(Sy);
                               3537                 :              5 :         transdatums[4] = Float8GetDatumFast(Syy);
                               3538                 :              5 :         transdatums[5] = Float8GetDatumFast(Sxy);
  334 tgl@sss.pgh.pa.us        3539                 :              5 :         transdatums[6] = Float8GetDatumFast(commonX);
                               3540                 :              5 :         transdatums[7] = Float8GetDatumFast(commonY);
                               3541                 :                : 
                               3542                 :              5 :         result = construct_array_builtin(transdatums, 8, FLOAT8OID);
                               3543                 :                : 
 7405                          3544                 :              5 :         PG_RETURN_ARRAYTYPE_P(result);
                               3545                 :                :     }
                               3546                 :                : }
                               3547                 :                : 
                               3548                 :                : /*
                               3549                 :                :  * float8_regr_combine
                               3550                 :                :  *
                               3551                 :                :  * An aggregate combine function used to combine two 8-fields
                               3552                 :                :  * aggregate transition data into a single transition data.
                               3553                 :                :  * This function is used only in two stage aggregation and
                               3554                 :                :  * shouldn't be called outside aggregate context.
                               3555                 :                :  */
                               3556                 :                : Datum
 3863 rhaas@postgresql.org     3557                 :             15 : float8_regr_combine(PG_FUNCTION_ARGS)
                               3558                 :                : {
                               3559                 :             15 :     ArrayType  *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
                               3560                 :             15 :     ArrayType  *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
                               3561                 :                :     float8     *transvalues1;
                               3562                 :                :     float8     *transvalues2;
                               3563                 :                :     float8      N1,
                               3564                 :                :                 Sx1,
                               3565                 :                :                 Sxx1,
                               3566                 :                :                 Sy1,
                               3567                 :                :                 Syy1,
                               3568                 :                :                 Sxy1,
                               3569                 :                :                 Cx1,
                               3570                 :                :                 Cy1,
                               3571                 :                :                 N2,
                               3572                 :                :                 Sx2,
                               3573                 :                :                 Sxx2,
                               3574                 :                :                 Sy2,
                               3575                 :                :                 Syy2,
                               3576                 :                :                 Sxy2,
                               3577                 :                :                 Cx2,
                               3578                 :                :                 Cy2,
                               3579                 :                :                 tmp1,
                               3580                 :                :                 tmp2,
                               3581                 :                :                 N,
                               3582                 :                :                 Sx,
                               3583                 :                :                 Sxx,
                               3584                 :                :                 Sy,
                               3585                 :                :                 Syy,
                               3586                 :                :                 Sxy,
                               3587                 :                :                 Cx,
                               3588                 :                :                 Cy;
                               3589                 :                : 
  334 tgl@sss.pgh.pa.us        3590                 :             15 :     transvalues1 = check_float8_array(transarray1, "float8_regr_combine", 8);
                               3591                 :             15 :     transvalues2 = check_float8_array(transarray2, "float8_regr_combine", 8);
                               3592                 :                : 
 2952 dean.a.rasheed@gmail     3593                 :             15 :     N1 = transvalues1[0];
                               3594                 :             15 :     Sx1 = transvalues1[1];
                               3595                 :             15 :     Sxx1 = transvalues1[2];
                               3596                 :             15 :     Sy1 = transvalues1[3];
                               3597                 :             15 :     Syy1 = transvalues1[4];
                               3598                 :             15 :     Sxy1 = transvalues1[5];
  334 tgl@sss.pgh.pa.us        3599                 :             15 :     Cx1 = transvalues1[6];
                               3600                 :             15 :     Cy1 = transvalues1[7];
                               3601                 :                : 
 2952 dean.a.rasheed@gmail     3602                 :             15 :     N2 = transvalues2[0];
                               3603                 :             15 :     Sx2 = transvalues2[1];
                               3604                 :             15 :     Sxx2 = transvalues2[2];
                               3605                 :             15 :     Sy2 = transvalues2[3];
                               3606                 :             15 :     Syy2 = transvalues2[4];
                               3607                 :             15 :     Sxy2 = transvalues2[5];
  334 tgl@sss.pgh.pa.us        3608                 :             15 :     Cx2 = transvalues2[6];
                               3609                 :             15 :     Cy2 = transvalues2[7];
                               3610                 :                : 
                               3611                 :                :     /*--------------------
                               3612                 :                :      * The transition values combine using a generalization of the
                               3613                 :                :      * Youngs-Cramer algorithm as follows:
                               3614                 :                :      *
                               3615                 :                :      *  N = N1 + N2
                               3616                 :                :      *  Sx = Sx1 + Sx2
                               3617                 :                :      *  Sxx = Sxx1 + Sxx2 + N1 * N2 * (Sx1/N1 - Sx2/N2)^2 / N
                               3618                 :                :      *  Sy = Sy1 + Sy2
                               3619                 :                :      *  Syy = Syy1 + Syy2 + N1 * N2 * (Sy1/N1 - Sy2/N2)^2 / N
                               3620                 :                :      *  Sxy = Sxy1 + Sxy2 + N1 * N2 * (Sx1/N1 - Sx2/N2) * (Sy1/N1 - Sy2/N2) / N
                               3621                 :                :      *
                               3622                 :                :      * It's worth handling the special cases N1 = 0 and N2 = 0 separately
                               3623                 :                :      * since those cases are trivial, and we then don't need to worry about
                               3624                 :                :      * division-by-zero errors in the general case.
                               3625                 :                :      *--------------------
                               3626                 :                :      */
 2952 dean.a.rasheed@gmail     3627         [ +  + ]:             15 :     if (N1 == 0.0)
                               3628                 :                :     {
                               3629                 :              5 :         N = N2;
                               3630                 :              5 :         Sx = Sx2;
                               3631                 :              5 :         Sxx = Sxx2;
                               3632                 :              5 :         Sy = Sy2;
                               3633                 :              5 :         Syy = Syy2;
                               3634                 :              5 :         Sxy = Sxy2;
  334 tgl@sss.pgh.pa.us        3635                 :              5 :         Cx = Cx2;
                               3636                 :              5 :         Cy = Cy2;
                               3637                 :                :     }
 2952 dean.a.rasheed@gmail     3638         [ +  + ]:             10 :     else if (N2 == 0.0)
                               3639                 :                :     {
                               3640                 :              5 :         N = N1;
                               3641                 :              5 :         Sx = Sx1;
                               3642                 :              5 :         Sxx = Sxx1;
                               3643                 :              5 :         Sy = Sy1;
                               3644                 :              5 :         Syy = Syy1;
                               3645                 :              5 :         Sxy = Sxy1;
  334 tgl@sss.pgh.pa.us        3646                 :              5 :         Cx = Cx1;
                               3647                 :              5 :         Cy = Cy1;
                               3648                 :                :     }
                               3649                 :                :     else
                               3650                 :                :     {
 2952 dean.a.rasheed@gmail     3651                 :              5 :         N = N1 + N2;
                               3652                 :              5 :         Sx = float8_pl(Sx1, Sx2);
                               3653                 :              5 :         tmp1 = Sx1 / N1 - Sx2 / N2;
                               3654                 :              5 :         Sxx = Sxx1 + Sxx2 + N1 * N2 * tmp1 * tmp1 / N;
 2457 tgl@sss.pgh.pa.us        3655   [ -  +  -  -  :              5 :         if (unlikely(isinf(Sxx)) && !isinf(Sxx1) && !isinf(Sxx2))
                                              -  - ]
 2457 tgl@sss.pgh.pa.us        3656                 :UBC           0 :             float_overflow_error();
 2952 dean.a.rasheed@gmail     3657                 :CBC           5 :         Sy = float8_pl(Sy1, Sy2);
                               3658                 :              5 :         tmp2 = Sy1 / N1 - Sy2 / N2;
                               3659                 :              5 :         Syy = Syy1 + Syy2 + N1 * N2 * tmp2 * tmp2 / N;
 2457 tgl@sss.pgh.pa.us        3660   [ -  +  -  -  :              5 :         if (unlikely(isinf(Syy)) && !isinf(Syy1) && !isinf(Syy2))
                                              -  - ]
 2457 tgl@sss.pgh.pa.us        3661                 :UBC           0 :             float_overflow_error();
 2952 dean.a.rasheed@gmail     3662                 :CBC           5 :         Sxy = Sxy1 + Sxy2 + N1 * N2 * tmp1 * tmp2 / N;
 2457 tgl@sss.pgh.pa.us        3663   [ -  +  -  -  :              5 :         if (unlikely(isinf(Sxy)) && !isinf(Sxy1) && !isinf(Sxy2))
                                              -  - ]
 2457 tgl@sss.pgh.pa.us        3664                 :UBC           0 :             float_overflow_error();
  334 tgl@sss.pgh.pa.us        3665         [ +  - ]:CBC           5 :         if (float8_eq(Cx1, Cx2))
                               3666                 :              5 :             Cx = Cx1;
                               3667                 :                :         else
  334 tgl@sss.pgh.pa.us        3668                 :UBC           0 :             Cx = get_float8_nan();
  334 tgl@sss.pgh.pa.us        3669         [ -  + ]:CBC           5 :         if (float8_eq(Cy1, Cy2))
  334 tgl@sss.pgh.pa.us        3670                 :UBC           0 :             Cy = Cy1;
                               3671                 :                :         else
  334 tgl@sss.pgh.pa.us        3672                 :CBC           5 :             Cy = get_float8_nan();
                               3673                 :                :     }
                               3674                 :                : 
                               3675                 :                :     /*
                               3676                 :                :      * If we're invoked as an aggregate, we can cheat and modify our first
                               3677                 :                :      * parameter in-place to reduce palloc overhead. Otherwise we construct a
                               3678                 :                :      * new array with the updated transition data and return it.
                               3679                 :                :      */
 2952 dean.a.rasheed@gmail     3680         [ -  + ]:             15 :     if (AggCheckCallContext(fcinfo, NULL))
                               3681                 :                :     {
 2952 dean.a.rasheed@gmail     3682                 :UBC           0 :         transvalues1[0] = N;
                               3683                 :              0 :         transvalues1[1] = Sx;
                               3684                 :              0 :         transvalues1[2] = Sxx;
                               3685                 :              0 :         transvalues1[3] = Sy;
                               3686                 :              0 :         transvalues1[4] = Syy;
                               3687                 :              0 :         transvalues1[5] = Sxy;
  334 tgl@sss.pgh.pa.us        3688                 :              0 :         transvalues1[6] = Cx;
                               3689                 :              0 :         transvalues1[7] = Cy;
                               3690                 :                : 
 2952 dean.a.rasheed@gmail     3691                 :              0 :         PG_RETURN_ARRAYTYPE_P(transarray1);
                               3692                 :                :     }
                               3693                 :                :     else
                               3694                 :                :     {
                               3695                 :                :         Datum       transdatums[8];
                               3696                 :                :         ArrayType  *result;
                               3697                 :                : 
 2952 dean.a.rasheed@gmail     3698                 :CBC          15 :         transdatums[0] = Float8GetDatumFast(N);
                               3699                 :             15 :         transdatums[1] = Float8GetDatumFast(Sx);
                               3700                 :             15 :         transdatums[2] = Float8GetDatumFast(Sxx);
                               3701                 :             15 :         transdatums[3] = Float8GetDatumFast(Sy);
                               3702                 :             15 :         transdatums[4] = Float8GetDatumFast(Syy);
                               3703                 :             15 :         transdatums[5] = Float8GetDatumFast(Sxy);
  334 tgl@sss.pgh.pa.us        3704                 :             15 :         transdatums[6] = Float8GetDatumFast(Cx);
                               3705                 :             15 :         transdatums[7] = Float8GetDatumFast(Cy);
                               3706                 :                : 
                               3707                 :             15 :         result = construct_array_builtin(transdatums, 8, FLOAT8OID);
                               3708                 :                : 
 2952 dean.a.rasheed@gmail     3709                 :             15 :         PG_RETURN_ARRAYTYPE_P(result);
                               3710                 :                :     }
                               3711                 :                : }
                               3712                 :                : 
                               3713                 :                : 
                               3714                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3715                 :             20 : float8_regr_sxx(PG_FUNCTION_ARGS)
                               3716                 :                : {
                               3717                 :             20 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3718                 :                :     float8     *transvalues;
                               3719                 :                :     float8      N,
                               3720                 :                :                 Sxx;
                               3721                 :                : 
  334                          3722                 :             20 :     transvalues = check_float8_array(transarray, "float8_regr_sxx", 8);
 7405                          3723                 :             20 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3724                 :             20 :     Sxx = transvalues[2];
                               3725                 :                : 
                               3726                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3727         [ -  + ]:             20 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3728                 :UBC           0 :         PG_RETURN_NULL();
                               3729                 :                : 
                               3730                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               3731                 :                : 
 2952 dean.a.rasheed@gmail     3732                 :CBC          20 :     PG_RETURN_FLOAT8(Sxx);
                               3733                 :                : }
                               3734                 :                : 
                               3735                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3736                 :             20 : float8_regr_syy(PG_FUNCTION_ARGS)
                               3737                 :                : {
                               3738                 :             20 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3739                 :                :     float8     *transvalues;
                               3740                 :                :     float8      N,
                               3741                 :                :                 Syy;
                               3742                 :                : 
  334                          3743                 :             20 :     transvalues = check_float8_array(transarray, "float8_regr_syy", 8);
 7405                          3744                 :             20 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3745                 :             20 :     Syy = transvalues[4];
                               3746                 :                : 
                               3747                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3748         [ -  + ]:             20 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3749                 :UBC           0 :         PG_RETURN_NULL();
                               3750                 :                : 
                               3751                 :                :     /* Note that Syy is guaranteed to be non-negative */
                               3752                 :                : 
 2952 dean.a.rasheed@gmail     3753                 :CBC          20 :     PG_RETURN_FLOAT8(Syy);
                               3754                 :                : }
                               3755                 :                : 
                               3756                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3757                 :             20 : float8_regr_sxy(PG_FUNCTION_ARGS)
                               3758                 :                : {
                               3759                 :             20 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3760                 :                :     float8     *transvalues;
                               3761                 :                :     float8      N,
                               3762                 :                :                 Sxy;
                               3763                 :                : 
  334                          3764                 :             20 :     transvalues = check_float8_array(transarray, "float8_regr_sxy", 8);
 7405                          3765                 :             20 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3766                 :             20 :     Sxy = transvalues[5];
                               3767                 :                : 
                               3768                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3769         [ -  + ]:             20 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3770                 :UBC           0 :         PG_RETURN_NULL();
                               3771                 :                : 
                               3772                 :                :     /* A negative result is valid here */
                               3773                 :                : 
 2952 dean.a.rasheed@gmail     3774                 :CBC          20 :     PG_RETURN_FLOAT8(Sxy);
                               3775                 :                : }
                               3776                 :                : 
                               3777                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3778                 :              4 : float8_regr_avgx(PG_FUNCTION_ARGS)
                               3779                 :                : {
                               3780                 :              4 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3781                 :                :     float8     *transvalues;
                               3782                 :                :     float8      N,
                               3783                 :                :                 Sx,
                               3784                 :                :                 commonX;
                               3785                 :                : 
  334                          3786                 :              4 :     transvalues = check_float8_array(transarray, "float8_regr_avgx", 8);
 7405                          3787                 :              4 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3788                 :              4 :     Sx = transvalues[1];
  334 tgl@sss.pgh.pa.us        3789                 :              4 :     commonX = transvalues[6];
                               3790                 :                : 
                               3791                 :                :     /* if N is 0 we should return NULL */
 7405                          3792         [ -  + ]:              4 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3793                 :UBC           0 :         PG_RETURN_NULL();
                               3794                 :                : 
                               3795                 :                :     /* if all inputs were the same just return that, avoiding roundoff error */
  334 tgl@sss.pgh.pa.us        3796         [ -  + ]:CBC           4 :     if (!isnan(commonX))
  334 tgl@sss.pgh.pa.us        3797                 :UBC           0 :         PG_RETURN_FLOAT8(commonX);
                               3798                 :                : 
 2952 dean.a.rasheed@gmail     3799                 :CBC           4 :     PG_RETURN_FLOAT8(Sx / N);
                               3800                 :                : }
                               3801                 :                : 
                               3802                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3803                 :              4 : float8_regr_avgy(PG_FUNCTION_ARGS)
                               3804                 :                : {
                               3805                 :              4 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3806                 :                :     float8     *transvalues;
                               3807                 :                :     float8      N,
                               3808                 :                :                 Sy,
                               3809                 :                :                 commonY;
                               3810                 :                : 
  334                          3811                 :              4 :     transvalues = check_float8_array(transarray, "float8_regr_avgy", 8);
 7405                          3812                 :              4 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3813                 :              4 :     Sy = transvalues[3];
  334 tgl@sss.pgh.pa.us        3814                 :              4 :     commonY = transvalues[7];
                               3815                 :                : 
                               3816                 :                :     /* if N is 0 we should return NULL */
 7405                          3817         [ -  + ]:              4 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3818                 :UBC           0 :         PG_RETURN_NULL();
                               3819                 :                : 
                               3820                 :                :     /* if all inputs were the same just return that, avoiding roundoff error */
  334 tgl@sss.pgh.pa.us        3821         [ -  + ]:CBC           4 :     if (!isnan(commonY))
  334 tgl@sss.pgh.pa.us        3822                 :UBC           0 :         PG_RETURN_FLOAT8(commonY);
                               3823                 :                : 
 2952 dean.a.rasheed@gmail     3824                 :CBC           4 :     PG_RETURN_FLOAT8(Sy / N);
                               3825                 :                : }
                               3826                 :                : 
                               3827                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3828                 :             36 : float8_covar_pop(PG_FUNCTION_ARGS)
                               3829                 :                : {
                               3830                 :             36 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3831                 :                :     float8     *transvalues;
                               3832                 :                :     float8      N,
                               3833                 :                :                 Sxy;
                               3834                 :                : 
  334                          3835                 :             36 :     transvalues = check_float8_array(transarray, "float8_covar_pop", 8);
 7405                          3836                 :             36 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3837                 :             36 :     Sxy = transvalues[5];
                               3838                 :                : 
                               3839                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3840         [ -  + ]:             36 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3841                 :UBC           0 :         PG_RETURN_NULL();
                               3842                 :                : 
 2952 dean.a.rasheed@gmail     3843                 :CBC          36 :     PG_RETURN_FLOAT8(Sxy / N);
                               3844                 :                : }
                               3845                 :                : 
                               3846                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3847                 :             16 : float8_covar_samp(PG_FUNCTION_ARGS)
                               3848                 :                : {
                               3849                 :             16 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3850                 :                :     float8     *transvalues;
                               3851                 :                :     float8      N,
                               3852                 :                :                 Sxy;
                               3853                 :                : 
  334                          3854                 :             16 :     transvalues = check_float8_array(transarray, "float8_covar_samp", 8);
 7405                          3855                 :             16 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3856                 :             16 :     Sxy = transvalues[5];
                               3857                 :                : 
                               3858                 :                :     /* if N is <= 1 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3859         [ +  + ]:             16 :     if (N < 2.0)
                               3860                 :             12 :         PG_RETURN_NULL();
                               3861                 :                : 
 2952 dean.a.rasheed@gmail     3862                 :              4 :     PG_RETURN_FLOAT8(Sxy / (N - 1.0));
                               3863                 :                : }
                               3864                 :                : 
                               3865                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3866                 :             44 : float8_corr(PG_FUNCTION_ARGS)
                               3867                 :                : {
                               3868                 :             44 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3869                 :                :     float8     *transvalues;
                               3870                 :                :     float8      N,
                               3871                 :                :                 Sxx,
                               3872                 :                :                 Syy,
                               3873                 :                :                 Sxy,
                               3874                 :                :                 product,
                               3875                 :                :                 sqrtproduct,
                               3876                 :                :                 result;
                               3877                 :                : 
  334                          3878                 :             44 :     transvalues = check_float8_array(transarray, "float8_corr", 8);
 7405                          3879                 :             44 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3880                 :             44 :     Sxx = transvalues[2];
                               3881                 :             44 :     Syy = transvalues[4];
                               3882                 :             44 :     Sxy = transvalues[5];
                               3883                 :                : 
                               3884                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3885         [ -  + ]:             44 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3886                 :UBC           0 :         PG_RETURN_NULL();
                               3887                 :                : 
                               3888                 :                :     /* Note that Sxx and Syy are guaranteed to be non-negative */
                               3889                 :                : 
                               3890                 :                :     /* per spec, return NULL for horizontal and vertical lines */
 2952 dean.a.rasheed@gmail     3891   [ +  +  +  + ]:CBC          44 :     if (Sxx == 0 || Syy == 0)
 7405 tgl@sss.pgh.pa.us        3892                 :             20 :         PG_RETURN_NULL();
                               3893                 :                : 
                               3894                 :                :     /*
                               3895                 :                :      * The product Sxx * Syy might underflow or overflow.  If so, we can
                               3896                 :                :      * recover by computing sqrt(Sxx) * sqrt(Syy) instead of sqrt(Sxx * Syy).
                               3897                 :                :      * However, the double sqrt() calculation is a bit slower and less
                               3898                 :                :      * accurate, so don't do it if we don't have to.
                               3899                 :                :      */
  334                          3900                 :             24 :     product = Sxx * Syy;
                               3901   [ +  +  +  + ]:             24 :     if (product == 0 || isinf(product))
                               3902                 :             12 :         sqrtproduct = sqrt(Sxx) * sqrt(Syy);
                               3903                 :                :     else
                               3904                 :             12 :         sqrtproduct = sqrt(product);
                               3905                 :             24 :     result = Sxy / sqrtproduct;
                               3906                 :                : 
                               3907                 :                :     /*
                               3908                 :                :      * Despite all these precautions, this formula can yield results outside
                               3909                 :                :      * [-1, 1] due to roundoff error.  Clamp it to the expected range.
                               3910                 :                :      */
                               3911         [ -  + ]:             24 :     if (result < -1)
  334 tgl@sss.pgh.pa.us        3912                 :UBC           0 :         result = -1;
  334 tgl@sss.pgh.pa.us        3913         [ +  + ]:CBC          24 :     else if (result > 1)
                               3914                 :              4 :         result = 1;
                               3915                 :                : 
                               3916                 :             24 :     PG_RETURN_FLOAT8(result);
                               3917                 :                : }
                               3918                 :                : 
                               3919                 :                : Datum
 7405                          3920                 :             40 : float8_regr_r2(PG_FUNCTION_ARGS)
                               3921                 :                : {
                               3922                 :             40 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3923                 :                :     float8     *transvalues;
                               3924                 :                :     float8      N,
                               3925                 :                :                 Sxx,
                               3926                 :                :                 Syy,
                               3927                 :                :                 Sxy,
                               3928                 :                :                 numerator,
                               3929                 :                :                 denominator,
                               3930                 :                :                 sqrtdenominator,
                               3931                 :                :                 sqrtresult,
                               3932                 :                :                 result;
                               3933                 :                : 
  334                          3934                 :             40 :     transvalues = check_float8_array(transarray, "float8_regr_r2", 8);
 7405                          3935                 :             40 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3936                 :             40 :     Sxx = transvalues[2];
                               3937                 :             40 :     Syy = transvalues[4];
                               3938                 :             40 :     Sxy = transvalues[5];
                               3939                 :                : 
                               3940                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        3941         [ -  + ]:             40 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        3942                 :UBC           0 :         PG_RETURN_NULL();
                               3943                 :                : 
                               3944                 :                :     /* Note that Sxx and Syy are guaranteed to be non-negative */
                               3945                 :                : 
                               3946                 :                :     /* per spec, return NULL for a vertical line */
 2952 dean.a.rasheed@gmail     3947         [ +  + ]:CBC          40 :     if (Sxx == 0)
 7405 tgl@sss.pgh.pa.us        3948                 :              8 :         PG_RETURN_NULL();
                               3949                 :                : 
                               3950                 :                :     /* per spec, return 1.0 for a horizontal line */
 2952 dean.a.rasheed@gmail     3951         [ +  + ]:             32 :     if (Syy == 0)
 7405 tgl@sss.pgh.pa.us        3952                 :              8 :         PG_RETURN_FLOAT8(1.0);
                               3953                 :                : 
                               3954                 :                :     /*
                               3955                 :                :      * The products Sxy * Sxy and/or Sxx * Syy might underflow or overflow. If
                               3956                 :                :      * so, we can recover by computing Sxy / (sqrt(Sxx) * sqrt(Syy)) and
                               3957                 :                :      * squaring it instead.  However, the double sqrt() calculation is a bit
                               3958                 :                :      * slower and less accurate, so don't do it if we don't have to.
                               3959                 :                :      */
  155 dean.a.rasheed@gmail     3960                 :             24 :     numerator = Sxy * Sxy;
                               3961                 :             24 :     denominator = Sxx * Syy;
                               3962   [ +  +  +  +  :             24 :     if (numerator == 0 || isinf(numerator) ||
                                              +  - ]
                               3963         [ -  + ]:             12 :         denominator == 0 || isinf(denominator))
                               3964                 :                :     {
                               3965                 :             12 :         sqrtdenominator = sqrt(Sxx) * sqrt(Syy);
                               3966                 :             12 :         sqrtresult = Sxy / sqrtdenominator;
                               3967                 :             12 :         result = sqrtresult * sqrtresult;
                               3968                 :                :     }
                               3969                 :                :     else
                               3970                 :             12 :         result = numerator / denominator;
                               3971                 :                : 
                               3972                 :                :     /*
                               3973                 :                :      * Despite all these precautions, this formula can yield results outside
                               3974                 :                :      * [0, 1] due to roundoff error.  Clamp it to the expected range.
                               3975                 :                :      *
                               3976                 :                :      * Note that result is guaranteed to be non-negative because Sxx and Syy
                               3977                 :                :      * are non-negative, so we only need to clamp the upper end of the range.
                               3978                 :                :      */
                               3979         [ +  + ]:             24 :     if (result > 1)
                               3980                 :              4 :         result = 1;
                               3981                 :                : 
                               3982                 :             24 :     PG_RETURN_FLOAT8(result);
                               3983                 :                : }
                               3984                 :                : 
                               3985                 :                : Datum
 7405 tgl@sss.pgh.pa.us        3986                 :              8 : float8_regr_slope(PG_FUNCTION_ARGS)
                               3987                 :                : {
                               3988                 :              8 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               3989                 :                :     float8     *transvalues;
                               3990                 :                :     float8      N,
                               3991                 :                :                 Sxx,
                               3992                 :                :                 Sxy;
                               3993                 :                : 
  334                          3994                 :              8 :     transvalues = check_float8_array(transarray, "float8_regr_slope", 8);
 7405                          3995                 :              8 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     3996                 :              8 :     Sxx = transvalues[2];
                               3997                 :              8 :     Sxy = transvalues[5];
                               3998                 :                : 
                               3999                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        4000         [ -  + ]:              8 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        4001                 :UBC           0 :         PG_RETURN_NULL();
                               4002                 :                : 
                               4003                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               4004                 :                : 
                               4005                 :                :     /* per spec, return NULL for a vertical line */
 2952 dean.a.rasheed@gmail     4006         [ +  + ]:CBC           8 :     if (Sxx == 0)
 7405 tgl@sss.pgh.pa.us        4007                 :              4 :         PG_RETURN_NULL();
                               4008                 :                : 
 2952 dean.a.rasheed@gmail     4009                 :              4 :     PG_RETURN_FLOAT8(Sxy / Sxx);
                               4010                 :                : }
                               4011                 :                : 
                               4012                 :                : Datum
 7405 tgl@sss.pgh.pa.us        4013                 :             16 : float8_regr_intercept(PG_FUNCTION_ARGS)
                               4014                 :                : {
                               4015                 :             16 :     ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
                               4016                 :                :     float8     *transvalues;
                               4017                 :                :     float8      N,
                               4018                 :                :                 Sx,
                               4019                 :                :                 Sxx,
                               4020                 :                :                 Sy,
                               4021                 :                :                 Sxy,
                               4022                 :                :                 dy;
                               4023                 :                : 
  334                          4024                 :             16 :     transvalues = check_float8_array(transarray, "float8_regr_intercept", 8);
 7405                          4025                 :             16 :     N = transvalues[0];
 2952 dean.a.rasheed@gmail     4026                 :             16 :     Sx = transvalues[1];
                               4027                 :             16 :     Sxx = transvalues[2];
                               4028                 :             16 :     Sy = transvalues[3];
                               4029                 :             16 :     Sxy = transvalues[5];
                               4030                 :                : 
                               4031                 :                :     /* if N is 0 we should return NULL */
 7405 tgl@sss.pgh.pa.us        4032         [ -  + ]:             16 :     if (N < 1.0)
 7405 tgl@sss.pgh.pa.us        4033                 :UBC           0 :         PG_RETURN_NULL();
                               4034                 :                : 
                               4035                 :                :     /* Note that Sxx is guaranteed to be non-negative */
                               4036                 :                : 
                               4037                 :                :     /* per spec, return NULL for a vertical line */
 2952 dean.a.rasheed@gmail     4038         [ +  + ]:CBC          16 :     if (Sxx == 0)
 7405 tgl@sss.pgh.pa.us        4039                 :              4 :         PG_RETURN_NULL();
                               4040                 :                : 
                               4041                 :                :     /*
                               4042                 :                :      * The intercept is given by (Sy - dy) / N, where dy = Sx * Sxy / Sxx.
                               4043                 :                :      * However, when computing dy, the intermediate product Sx * Sxy might
                               4044                 :                :      * underflow or overflow.  If so, we can recover by decomposing Sx, Sxy,
                               4045                 :                :      * and Sxx into normalized mantissa and integer power-of-two components,
                               4046                 :                :      * computing the corresponding components of dy, and then recomposing dy.
                               4047                 :                :      * We avoid doing this if Sx, Sxy, or Sxx are infinite or NaN, since the
                               4048                 :                :      * exponent returned by frexp() is unspecified in those cases (and the
                               4049                 :                :      * final result would be the same in any case).
                               4050                 :                :      */
  155 dean.a.rasheed@gmail     4051                 :             12 :     dy = Sx * Sxy / Sxx;
                               4052   [ +  +  +  + ]:             12 :     if ((dy == 0 || isinf(dy)) &&
                               4053   [ +  -  +  -  :             16 :         !(isinf(Sx) || isinf(Sxy) || isinf(Sxx) ||
                                     +  -  +  -  +  
                                                 - ]
                               4054         [ +  - ]:              8 :           isnan(Sx) || isnan(Sxy) || isnan(Sxx)))
                               4055                 :                :     {
                               4056                 :                :         float8      m_Sx,
                               4057                 :                :                     m_Sxy,
                               4058                 :                :                     m_Sxx,
                               4059                 :                :                     m_dy;
                               4060                 :                :         int         n_Sx,
                               4061                 :                :                     n_Sxy,
                               4062                 :                :                     n_Sxx,
                               4063                 :                :                     n_dy;
                               4064                 :                : 
                               4065                 :              8 :         m_Sx = frexp(Sx, &n_Sx);
                               4066                 :              8 :         m_Sxy = frexp(Sxy, &n_Sxy);
                               4067                 :              8 :         m_Sxx = frexp(Sxx, &n_Sxx);
                               4068                 :                : 
                               4069                 :              8 :         m_dy = m_Sx * m_Sxy / m_Sxx;
                               4070                 :              8 :         n_dy = n_Sx + n_Sxy - n_Sxx;
                               4071                 :                : 
                               4072                 :              8 :         dy = ldexp(m_dy, n_dy);
                               4073                 :                :     }
                               4074                 :                : 
                               4075                 :             12 :     PG_RETURN_FLOAT8((Sy - dy) / N);
                               4076                 :                : }
                               4077                 :                : 
                               4078                 :                : 
                               4079                 :                : /*
                               4080                 :                :  *      ====================================
                               4081                 :                :  *      MIXED-PRECISION ARITHMETIC OPERATORS
                               4082                 :                :  *      ====================================
                               4083                 :                :  */
                               4084                 :                : 
                               4085                 :                : /*
                               4086                 :                :  *      float48pl       - returns arg1 + arg2
                               4087                 :                :  *      float48mi       - returns arg1 - arg2
                               4088                 :                :  *      float48mul      - returns arg1 * arg2
                               4089                 :                :  *      float48div      - returns arg1 / arg2
                               4090                 :                :  */
                               4091                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4092                 :             17 : float48pl(PG_FUNCTION_ARGS)
                               4093                 :                : {
                               4094                 :             17 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4095                 :             17 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4096                 :                : 
 3021 tomas.vondra@postgre     4097                 :             17 :     PG_RETURN_FLOAT8(float8_pl((float8) arg1, arg2));
                               4098                 :                : }
                               4099                 :                : 
                               4100                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4101                 :              4 : float48mi(PG_FUNCTION_ARGS)
                               4102                 :                : {
                               4103                 :              4 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4104                 :              4 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4105                 :                : 
 3021 tomas.vondra@postgre     4106                 :              4 :     PG_RETURN_FLOAT8(float8_mi((float8) arg1, arg2));
                               4107                 :                : }
                               4108                 :                : 
                               4109                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4110                 :              4 : float48mul(PG_FUNCTION_ARGS)
                               4111                 :                : {
                               4112                 :              4 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4113                 :              4 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4114                 :                : 
 3021 tomas.vondra@postgre     4115                 :              4 :     PG_RETURN_FLOAT8(float8_mul((float8) arg1, arg2));
                               4116                 :                : }
                               4117                 :                : 
                               4118                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4119                 :              4 : float48div(PG_FUNCTION_ARGS)
                               4120                 :                : {
                               4121                 :              4 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4122                 :              4 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4123                 :                : 
 3021 tomas.vondra@postgre     4124                 :              4 :     PG_RETURN_FLOAT8(float8_div((float8) arg1, arg2));
                               4125                 :                : }
                               4126                 :                : 
                               4127                 :                : /*
                               4128                 :                :  *      float84pl       - returns arg1 + arg2
                               4129                 :                :  *      float84mi       - returns arg1 - arg2
                               4130                 :                :  *      float84mul      - returns arg1 * arg2
                               4131                 :                :  *      float84div      - returns arg1 / arg2
                               4132                 :                :  */
                               4133                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4134                 :              8 : float84pl(PG_FUNCTION_ARGS)
                               4135                 :                : {
                               4136                 :              8 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4137                 :              8 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4138                 :                : 
 3021 tomas.vondra@postgre     4139                 :              8 :     PG_RETURN_FLOAT8(float8_pl(arg1, (float8) arg2));
                               4140                 :                : }
                               4141                 :                : 
                               4142                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4143                 :UBC           0 : float84mi(PG_FUNCTION_ARGS)
                               4144                 :                : {
                               4145                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4146                 :              0 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4147                 :                : 
 3021 tomas.vondra@postgre     4148                 :              0 :     PG_RETURN_FLOAT8(float8_mi(arg1, (float8) arg2));
                               4149                 :                : }
                               4150                 :                : 
                               4151                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4152                 :              0 : float84mul(PG_FUNCTION_ARGS)
                               4153                 :                : {
                               4154                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4155                 :              0 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4156                 :                : 
 3021 tomas.vondra@postgre     4157                 :              0 :     PG_RETURN_FLOAT8(float8_mul(arg1, (float8) arg2));
                               4158                 :                : }
                               4159                 :                : 
                               4160                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4161                 :CBC           4 : float84div(PG_FUNCTION_ARGS)
                               4162                 :                : {
                               4163                 :              4 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4164                 :              4 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4165                 :                : 
 3021 tomas.vondra@postgre     4166                 :              4 :     PG_RETURN_FLOAT8(float8_div(arg1, (float8) arg2));
                               4167                 :                : }
                               4168                 :                : 
                               4169                 :                : /*
                               4170                 :                :  *      ====================
                               4171                 :                :  *      COMPARISON OPERATORS
                               4172                 :                :  *      ====================
                               4173                 :                :  */
                               4174                 :                : 
                               4175                 :                : /*
                               4176                 :                :  *      float48{eq,ne,lt,le,gt,ge}      - float4/float8 comparison operations
                               4177                 :                :  */
                               4178                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4179                 :           1815 : float48eq(PG_FUNCTION_ARGS)
                               4180                 :                : {
                               4181                 :           1815 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4182                 :           1815 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4183                 :                : 
 3021 tomas.vondra@postgre     4184                 :           1815 :     PG_RETURN_BOOL(float8_eq((float8) arg1, arg2));
                               4185                 :                : }
                               4186                 :                : 
                               4187                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4188                 :          13495 : float48ne(PG_FUNCTION_ARGS)
                               4189                 :                : {
                               4190                 :          13495 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4191                 :          13495 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4192                 :                : 
 3021 tomas.vondra@postgre     4193                 :          13495 :     PG_RETURN_BOOL(float8_ne((float8) arg1, arg2));
                               4194                 :                : }
                               4195                 :                : 
                               4196                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4197                 :           2666 : float48lt(PG_FUNCTION_ARGS)
                               4198                 :                : {
                               4199                 :           2666 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4200                 :           2666 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4201                 :                : 
 3021 tomas.vondra@postgre     4202                 :           2666 :     PG_RETURN_BOOL(float8_lt((float8) arg1, arg2));
                               4203                 :                : }
                               4204                 :                : 
                               4205                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4206                 :          17597 : float48le(PG_FUNCTION_ARGS)
                               4207                 :                : {
                               4208                 :          17597 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4209                 :          17597 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4210                 :                : 
 3021 tomas.vondra@postgre     4211                 :          17597 :     PG_RETURN_BOOL(float8_le((float8) arg1, arg2));
                               4212                 :                : }
                               4213                 :                : 
                               4214                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4215                 :           2814 : float48gt(PG_FUNCTION_ARGS)
                               4216                 :                : {
                               4217                 :           2814 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4218                 :           2814 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4219                 :                : 
 3021 tomas.vondra@postgre     4220                 :           2814 :     PG_RETURN_BOOL(float8_gt((float8) arg1, arg2));
                               4221                 :                : }
                               4222                 :                : 
                               4223                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4224                 :           3082 : float48ge(PG_FUNCTION_ARGS)
                               4225                 :                : {
                               4226                 :           3082 :     float4      arg1 = PG_GETARG_FLOAT4(0);
                               4227                 :           3082 :     float8      arg2 = PG_GETARG_FLOAT8(1);
                               4228                 :                : 
 3021 tomas.vondra@postgre     4229                 :           3082 :     PG_RETURN_BOOL(float8_ge((float8) arg1, arg2));
                               4230                 :                : }
                               4231                 :                : 
                               4232                 :                : /*
                               4233                 :                :  *      float84{eq,ne,lt,le,gt,ge}      - float8/float4 comparison operations
                               4234                 :                :  */
                               4235                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4236                 :           1208 : float84eq(PG_FUNCTION_ARGS)
                               4237                 :                : {
                               4238                 :           1208 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4239                 :           1208 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4240                 :                : 
 3021 tomas.vondra@postgre     4241                 :           1208 :     PG_RETURN_BOOL(float8_eq(arg1, (float8) arg2));
                               4242                 :                : }
                               4243                 :                : 
                               4244                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4245                 :UBC           0 : float84ne(PG_FUNCTION_ARGS)
                               4246                 :                : {
                               4247                 :              0 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4248                 :              0 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4249                 :                : 
 3021 tomas.vondra@postgre     4250                 :              0 :     PG_RETURN_BOOL(float8_ne(arg1, (float8) arg2));
                               4251                 :                : }
                               4252                 :                : 
                               4253                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4254                 :CBC        2132 : float84lt(PG_FUNCTION_ARGS)
                               4255                 :                : {
                               4256                 :           2132 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4257                 :           2132 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4258                 :                : 
 3021 tomas.vondra@postgre     4259                 :           2132 :     PG_RETURN_BOOL(float8_lt(arg1, (float8) arg2));
                               4260                 :                : }
                               4261                 :                : 
                               4262                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4263                 :           2532 : float84le(PG_FUNCTION_ARGS)
                               4264                 :                : {
                               4265                 :           2532 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4266                 :           2532 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4267                 :                : 
 3021 tomas.vondra@postgre     4268                 :           2532 :     PG_RETURN_BOOL(float8_le(arg1, (float8) arg2));
                               4269                 :                : }
                               4270                 :                : 
                               4271                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4272                 :           2132 : float84gt(PG_FUNCTION_ARGS)
                               4273                 :                : {
                               4274                 :           2132 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4275                 :           2132 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4276                 :                : 
 3021 tomas.vondra@postgre     4277                 :           2132 :     PG_RETURN_BOOL(float8_gt(arg1, (float8) arg2));
                               4278                 :                : }
                               4279                 :                : 
                               4280                 :                : Datum
 9592 tgl@sss.pgh.pa.us        4281                 :           2136 : float84ge(PG_FUNCTION_ARGS)
                               4282                 :                : {
                               4283                 :           2136 :     float8      arg1 = PG_GETARG_FLOAT8(0);
                               4284                 :           2136 :     float4      arg2 = PG_GETARG_FLOAT4(1);
                               4285                 :                : 
 3021 tomas.vondra@postgre     4286                 :           2136 :     PG_RETURN_BOOL(float8_ge(arg1, (float8) arg2));
                               4287                 :                : }
                               4288                 :                : 
                               4289                 :                : /*
                               4290                 :                :  * Implements the float8 version of the width_bucket() function
                               4291                 :                :  * defined by SQL2003. See also width_bucket_numeric().
                               4292                 :                :  *
                               4293                 :                :  * 'bound1' and 'bound2' are the lower and upper bounds of the
                               4294                 :                :  * histogram's range, respectively. 'count' is the number of buckets
                               4295                 :                :  * in the histogram. width_bucket() returns an integer indicating the
                               4296                 :                :  * bucket number that 'operand' belongs to in an equiwidth histogram
                               4297                 :                :  * with the specified characteristics. An operand smaller than the
                               4298                 :                :  * lower bound is assigned to bucket 0. An operand greater than or equal
                               4299                 :                :  * to the upper bound is assigned to an additional bucket (with number
                               4300                 :                :  * count+1). We don't allow the histogram bounds to be NaN or +/- infinity,
                               4301                 :                :  * but we do allow those values for the operand (taking NaN to be larger
                               4302                 :                :  * than any other value, as we do in comparisons).
                               4303                 :                :  */
                               4304                 :                : Datum
 7233 neilc@samurai.com        4305                 :            585 : width_bucket_float8(PG_FUNCTION_ARGS)
                               4306                 :                : {
 6930 bruce@momjian.us         4307                 :            585 :     float8      operand = PG_GETARG_FLOAT8(0);
                               4308                 :            585 :     float8      bound1 = PG_GETARG_FLOAT8(1);
                               4309                 :            585 :     float8      bound2 = PG_GETARG_FLOAT8(2);
                               4310                 :            585 :     int32       count = PG_GETARG_INT32(3);
                               4311                 :                :     int32       result;
                               4312                 :                : 
 1316 tgl@sss.pgh.pa.us        4313         [ +  + ]:            585 :     if (count <= 0)
 7233 neilc@samurai.com        4314         [ +  - ]:              8 :         ereport(ERROR,
                               4315                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
                               4316                 :                :                  errmsg("count must be greater than zero")));
                               4317                 :                : 
  491 tgl@sss.pgh.pa.us        4318   [ +  +  -  + ]:            577 :     if (isnan(bound1) || isnan(bound2))
 7233 neilc@samurai.com        4319         [ +  - ]:              4 :         ereport(ERROR,
                               4320                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
                               4321                 :                :                  errmsg("lower and upper bounds cannot be NaN")));
                               4322                 :                : 
 6469 tgl@sss.pgh.pa.us        4323   [ +  +  +  + ]:            573 :     if (isinf(bound1) || isinf(bound2))
 7233 neilc@samurai.com        4324         [ +  - ]:             12 :         ereport(ERROR,
                               4325                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
                               4326                 :                :                  errmsg("lower and upper bounds must be finite")));
                               4327                 :                : 
                               4328         [ +  + ]:            561 :     if (bound1 < bound2)
                               4329                 :                :     {
  491 tgl@sss.pgh.pa.us        4330   [ +  +  +  + ]:            404 :         if (isnan(operand) || operand >= bound2)
                               4331                 :                :         {
 1315                          4332         [ +  + ]:             86 :             if (pg_add_s32_overflow(count, 1, &result))
                               4333         [ +  - ]:              4 :                 ereport(ERROR,
                               4334                 :                :                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               4335                 :                :                          errmsg("integer out of range")));
                               4336                 :                :         }
  491                          4337         [ +  + ]:            318 :         else if (operand < bound1)
                               4338                 :             77 :             result = 0;
                               4339                 :                :         else
                               4340                 :                :         {
 1315                          4341   [ +  +  +  + ]:            241 :             if (!isinf(bound2 - bound1))
                               4342                 :                :             {
                               4343                 :                :                 /* The quotient is surely in [0,1], so this can't overflow */
                               4344                 :            229 :                 result = count * ((operand - bound1) / (bound2 - bound1));
                               4345                 :                :             }
                               4346                 :                :             else
                               4347                 :                :             {
                               4348                 :                :                 /*
                               4349                 :                :                  * We get here if bound2 - bound1 overflows DBL_MAX.  Since
                               4350                 :                :                  * both bounds are finite, their difference can't exceed twice
                               4351                 :                :                  * DBL_MAX; so we can perform the computation without overflow
                               4352                 :                :                  * by dividing all the inputs by 2.  That should be exact too,
                               4353                 :                :                  * except in the case where a very small operand underflows to
                               4354                 :                :                  * zero, which would have negligible impact on the result
                               4355                 :                :                  * given such large bounds.
                               4356                 :                :                  */
                               4357                 :             12 :                 result = count * ((operand / 2 - bound1 / 2) / (bound2 / 2 - bound1 / 2));
                               4358                 :                :             }
                               4359                 :                :             /* The quotient could round to 1.0, which would be a lie */
                               4360         [ +  + ]:            241 :             if (result >= count)
                               4361                 :              5 :                 result = count - 1;
                               4362                 :                :             /* Having done that, we can add 1 without fear of overflow */
                               4363                 :            241 :             result++;
                               4364                 :                :         }
                               4365                 :                :     }
 7233 neilc@samurai.com        4366         [ +  + ]:            157 :     else if (bound1 > bound2)
                               4367                 :                :     {
  491 tgl@sss.pgh.pa.us        4368   [ +  -  +  + ]:            153 :         if (isnan(operand) || operand > bound1)
 1315                          4369                 :              8 :             result = 0;
 7233 neilc@samurai.com        4370         [ +  + ]:            145 :         else if (operand <= bound2)
                               4371                 :                :         {
 1315 tgl@sss.pgh.pa.us        4372         [ +  + ]:             20 :             if (pg_add_s32_overflow(count, 1, &result))
                               4373         [ +  - ]:              4 :                 ereport(ERROR,
                               4374                 :                :                         (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                               4375                 :                :                          errmsg("integer out of range")));
                               4376                 :                :         }
                               4377                 :                :         else
                               4378                 :                :         {
                               4379   [ +  +  +  + ]:            125 :             if (!isinf(bound1 - bound2))
                               4380                 :            113 :                 result = count * ((bound1 - operand) / (bound1 - bound2));
                               4381                 :                :             else
                               4382                 :             12 :                 result = count * ((bound1 / 2 - operand / 2) / (bound1 / 2 - bound2 / 2));
                               4383         [ +  + ]:            125 :             if (result >= count)
                               4384                 :              5 :                 result = count - 1;
                               4385                 :            125 :             result++;
                               4386                 :                :         }
                               4387                 :                :     }
                               4388                 :                :     else
                               4389                 :                :     {
 7233 neilc@samurai.com        4390         [ +  - ]:              4 :         ereport(ERROR,
                               4391                 :                :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
                               4392                 :                :                  errmsg("lower bound cannot equal upper bound")));
                               4393                 :                :         result = 0;             /* keep the compiler quiet */
                               4394                 :                :     }
                               4395                 :                : 
                               4396                 :            549 :     PG_RETURN_INT32(result);
                               4397                 :                : }
        

Generated by: LCOV version 2.0-1