PostgreSQL Source Code  git master
float.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <float.h>
#include <math.h>
#include <limits.h>
#include "catalog/pg_type.h"
#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/sortsupport.h"
Include dependency graph for float.c:

Go to the source code of this file.

Macros

#define M_PI   3.14159265358979323846
 
#define RADIANS_PER_DEGREE   0.0174532925199432957692
 
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
 
#define cbrt   my_cbrt
 
#define INIT_DEGREE_CONSTANTS()
 

Functions

static double sind_q1 (double x)
 
static double cosd_q1 (double x)
 
static void init_degree_constants (void)
 
static double cbrt (double x)
 
double get_float8_infinity (void)
 
float get_float4_infinity (void)
 
double get_float8_nan (void)
 
float get_float4_nan (void)
 
int is_infinite (double val)
 
Datum float4in (PG_FUNCTION_ARGS)
 
Datum float4out (PG_FUNCTION_ARGS)
 
Datum float4recv (PG_FUNCTION_ARGS)
 
Datum float4send (PG_FUNCTION_ARGS)
 
Datum float8in (PG_FUNCTION_ARGS)
 
double float8in_internal (char *num, char **endptr_p, const char *type_name, const char *orig_string)
 
Datum float8out (PG_FUNCTION_ARGS)
 
char * float8out_internal (double num)
 
Datum float8recv (PG_FUNCTION_ARGS)
 
Datum float8send (PG_FUNCTION_ARGS)
 
Datum float4abs (PG_FUNCTION_ARGS)
 
Datum float4um (PG_FUNCTION_ARGS)
 
Datum float4up (PG_FUNCTION_ARGS)
 
Datum float4larger (PG_FUNCTION_ARGS)
 
Datum float4smaller (PG_FUNCTION_ARGS)
 
Datum float8abs (PG_FUNCTION_ARGS)
 
Datum float8um (PG_FUNCTION_ARGS)
 
Datum float8up (PG_FUNCTION_ARGS)
 
Datum float8larger (PG_FUNCTION_ARGS)
 
Datum float8smaller (PG_FUNCTION_ARGS)
 
Datum float4pl (PG_FUNCTION_ARGS)
 
Datum float4mi (PG_FUNCTION_ARGS)
 
Datum float4mul (PG_FUNCTION_ARGS)
 
Datum float4div (PG_FUNCTION_ARGS)
 
Datum float8pl (PG_FUNCTION_ARGS)
 
Datum float8mi (PG_FUNCTION_ARGS)
 
Datum float8mul (PG_FUNCTION_ARGS)
 
Datum float8div (PG_FUNCTION_ARGS)
 
int float4_cmp_internal (float4 a, float4 b)
 
Datum float4eq (PG_FUNCTION_ARGS)
 
Datum float4ne (PG_FUNCTION_ARGS)
 
Datum float4lt (PG_FUNCTION_ARGS)
 
Datum float4le (PG_FUNCTION_ARGS)
 
Datum float4gt (PG_FUNCTION_ARGS)
 
Datum float4ge (PG_FUNCTION_ARGS)
 
Datum btfloat4cmp (PG_FUNCTION_ARGS)
 
static int btfloat4fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum btfloat4sortsupport (PG_FUNCTION_ARGS)
 
int float8_cmp_internal (float8 a, float8 b)
 
Datum float8eq (PG_FUNCTION_ARGS)
 
Datum float8ne (PG_FUNCTION_ARGS)
 
Datum float8lt (PG_FUNCTION_ARGS)
 
Datum float8le (PG_FUNCTION_ARGS)
 
Datum float8gt (PG_FUNCTION_ARGS)
 
Datum float8ge (PG_FUNCTION_ARGS)
 
Datum btfloat8cmp (PG_FUNCTION_ARGS)
 
static int btfloat8fastcmp (Datum x, Datum y, SortSupport ssup)
 
Datum btfloat8sortsupport (PG_FUNCTION_ARGS)
 
Datum btfloat48cmp (PG_FUNCTION_ARGS)
 
Datum btfloat84cmp (PG_FUNCTION_ARGS)
 
Datum in_range_float8_float8 (PG_FUNCTION_ARGS)
 
Datum in_range_float4_float8 (PG_FUNCTION_ARGS)
 
Datum ftod (PG_FUNCTION_ARGS)
 
Datum dtof (PG_FUNCTION_ARGS)
 
Datum dtoi4 (PG_FUNCTION_ARGS)
 
Datum dtoi2 (PG_FUNCTION_ARGS)
 
Datum i4tod (PG_FUNCTION_ARGS)
 
Datum i2tod (PG_FUNCTION_ARGS)
 
Datum ftoi4 (PG_FUNCTION_ARGS)
 
Datum ftoi2 (PG_FUNCTION_ARGS)
 
Datum i4tof (PG_FUNCTION_ARGS)
 
Datum i2tof (PG_FUNCTION_ARGS)
 
Datum dround (PG_FUNCTION_ARGS)
 
Datum dceil (PG_FUNCTION_ARGS)
 
Datum dfloor (PG_FUNCTION_ARGS)
 
Datum dsign (PG_FUNCTION_ARGS)
 
Datum dtrunc (PG_FUNCTION_ARGS)
 
Datum dsqrt (PG_FUNCTION_ARGS)
 
Datum dcbrt (PG_FUNCTION_ARGS)
 
Datum dpow (PG_FUNCTION_ARGS)
 
Datum dexp (PG_FUNCTION_ARGS)
 
Datum dlog1 (PG_FUNCTION_ARGS)
 
Datum dlog10 (PG_FUNCTION_ARGS)
 
Datum dacos (PG_FUNCTION_ARGS)
 
Datum dasin (PG_FUNCTION_ARGS)
 
Datum datan (PG_FUNCTION_ARGS)
 
Datum datan2 (PG_FUNCTION_ARGS)
 
Datum dcos (PG_FUNCTION_ARGS)
 
Datum dcot (PG_FUNCTION_ARGS)
 
Datum dsin (PG_FUNCTION_ARGS)
 
Datum dtan (PG_FUNCTION_ARGS)
 
static double asind_q1 (double x)
 
static double acosd_q1 (double x)
 
Datum dacosd (PG_FUNCTION_ARGS)
 
Datum dasind (PG_FUNCTION_ARGS)
 
Datum datand (PG_FUNCTION_ARGS)
 
Datum datan2d (PG_FUNCTION_ARGS)
 
static double sind_0_to_30 (double x)
 
static double cosd_0_to_60 (double x)
 
Datum dcosd (PG_FUNCTION_ARGS)
 
Datum dcotd (PG_FUNCTION_ARGS)
 
Datum dsind (PG_FUNCTION_ARGS)
 
Datum dtand (PG_FUNCTION_ARGS)
 
Datum degrees (PG_FUNCTION_ARGS)
 
Datum dpi (PG_FUNCTION_ARGS)
 
Datum radians (PG_FUNCTION_ARGS)
 
Datum drandom (PG_FUNCTION_ARGS)
 
Datum setseed (PG_FUNCTION_ARGS)
 
static float8check_float8_array (ArrayType *transarray, const char *caller, int n)
 
Datum float8_combine (PG_FUNCTION_ARGS)
 
Datum float8_accum (PG_FUNCTION_ARGS)
 
Datum float4_accum (PG_FUNCTION_ARGS)
 
Datum float8_avg (PG_FUNCTION_ARGS)
 
Datum float8_var_pop (PG_FUNCTION_ARGS)
 
Datum float8_var_samp (PG_FUNCTION_ARGS)
 
Datum float8_stddev_pop (PG_FUNCTION_ARGS)
 
Datum float8_stddev_samp (PG_FUNCTION_ARGS)
 
Datum float8_regr_accum (PG_FUNCTION_ARGS)
 
Datum float8_regr_combine (PG_FUNCTION_ARGS)
 
Datum float8_regr_sxx (PG_FUNCTION_ARGS)
 
Datum float8_regr_syy (PG_FUNCTION_ARGS)
 
Datum float8_regr_sxy (PG_FUNCTION_ARGS)
 
Datum float8_regr_avgx (PG_FUNCTION_ARGS)
 
Datum float8_regr_avgy (PG_FUNCTION_ARGS)
 
Datum float8_covar_pop (PG_FUNCTION_ARGS)
 
Datum float8_covar_samp (PG_FUNCTION_ARGS)
 
Datum float8_corr (PG_FUNCTION_ARGS)
 
Datum float8_regr_r2 (PG_FUNCTION_ARGS)
 
Datum float8_regr_slope (PG_FUNCTION_ARGS)
 
Datum float8_regr_intercept (PG_FUNCTION_ARGS)
 
Datum float48pl (PG_FUNCTION_ARGS)
 
Datum float48mi (PG_FUNCTION_ARGS)
 
Datum float48mul (PG_FUNCTION_ARGS)
 
Datum float48div (PG_FUNCTION_ARGS)
 
Datum float84pl (PG_FUNCTION_ARGS)
 
Datum float84mi (PG_FUNCTION_ARGS)
 
Datum float84mul (PG_FUNCTION_ARGS)
 
Datum float84div (PG_FUNCTION_ARGS)
 
Datum float48eq (PG_FUNCTION_ARGS)
 
Datum float48ne (PG_FUNCTION_ARGS)
 
Datum float48lt (PG_FUNCTION_ARGS)
 
Datum float48le (PG_FUNCTION_ARGS)
 
Datum float48gt (PG_FUNCTION_ARGS)
 
Datum float48ge (PG_FUNCTION_ARGS)
 
Datum float84eq (PG_FUNCTION_ARGS)
 
Datum float84ne (PG_FUNCTION_ARGS)
 
Datum float84lt (PG_FUNCTION_ARGS)
 
Datum float84le (PG_FUNCTION_ARGS)
 
Datum float84gt (PG_FUNCTION_ARGS)
 
Datum float84ge (PG_FUNCTION_ARGS)
 
Datum width_bucket_float8 (PG_FUNCTION_ARGS)
 

Variables

int extra_float_digits = 0
 
static bool degree_consts_set = false
 
static float8 sin_30 = 0
 
static float8 one_minus_cos_60 = 0
 
static float8 asin_0_5 = 0
 
static float8 acos_0_5 = 0
 
static float8 atan_1_0 = 0
 
static float8 tan_45 = 0
 
static float8 cot_45 = 0
 
float8 degree_c_thirty = 30.0
 
float8 degree_c_forty_five = 45.0
 
float8 degree_c_sixty = 60.0
 
float8 degree_c_one_half = 0.5
 
float8 degree_c_one = 1.0
 

Macro Definition Documentation

◆ cbrt

#define cbrt   my_cbrt

Definition at line 103 of file float.c.

Referenced by dcbrt().

◆ CHECKFLOATVAL

#define CHECKFLOATVAL (   val,
  inf_is_valid,
  zero_is_valid 
)
Value:
do { \
if (isinf(val) && !(inf_is_valid)) \
ereport(ERROR, \
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), \
errmsg("value out of range: overflow"))); \
\
if ((val) == 0.0 && !(zero_is_valid)) \
ereport(ERROR, \
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), \
errmsg("value out of range: underflow"))); \
} while(0)
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int isinf(double x)
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689

Definition at line 50 of file float.c.

Referenced by dacos(), dacosd(), dasin(), dasind(), datan(), datan2(), datan2d(), datand(), dcbrt(), dcos(), dcosd(), dcot(), dcotd(), degrees(), dexp(), dlog1(), dlog10(), dpow(), dsin(), dsind(), dsqrt(), dtan(), dtand(), dtof(), float48div(), float48mi(), float48mul(), float48pl(), float4_accum(), float4div(), float4in(), float4mi(), float4mul(), float4pl(), float84div(), float84mi(), float84mul(), float84pl(), float8_accum(), float8_combine(), float8_corr(), float8_covar_pop(), float8_covar_samp(), float8_regr_accum(), float8_regr_combine(), float8_regr_intercept(), float8_regr_r2(), float8_regr_slope(), float8_regr_sxx(), float8_regr_sxy(), float8_regr_syy(), float8_stddev_pop(), float8_stddev_samp(), float8_var_pop(), float8_var_samp(), float8div(), float8mi(), float8mul(), float8pl(), and radians().

◆ INIT_DEGREE_CONSTANTS

#define INIT_DEGREE_CONSTANTS ( )
Value:
do { \
init_degree_constants(); \
} while(0)
static bool degree_consts_set
Definition: float.c:68

Definition at line 2012 of file float.c.

Referenced by dacosd(), dasind(), datan2d(), datand(), dcosd(), dcotd(), dsind(), and dtand().

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 32 of file float.c.

Referenced by dpi().

◆ RADIANS_PER_DEGREE

#define RADIANS_PER_DEGREE   0.0174532925199432957692

Definition at line 36 of file float.c.

Referenced by cosd_0_to_60(), degrees(), init_degree_constants(), radians(), and sind_0_to_30().

Function Documentation

◆ acosd_q1()

static double acosd_q1 ( double  x)
static

Definition at line 2062 of file float.c.

References acos_0_5, and asin_0_5.

Referenced by dacosd().

2063 {
2064  /*
2065  * Stitch together inverse sine and cosine functions for the ranges [0,
2066  * 0.5] and (0.5, 1]. Each expression below is guaranteed to return
2067  * exactly 60 for x=0.5, so the result is a continuous monotonic function
2068  * over the full range.
2069  */
2070  if (x <= 0.5)
2071  {
2072  volatile float8 asin_x = asin(x);
2073 
2074  return 90.0 - (asin_x / asin_0_5) * 30.0;
2075  }
2076  else
2077  {
2078  volatile float8 acos_x = acos(x);
2079 
2080  return (acos_x / acos_0_5) * 60.0;
2081  }
2082 }
static float8 acos_0_5
Definition: float.c:72
double float8
Definition: c.h:458
static float8 asin_0_5
Definition: float.c:71

◆ asind_q1()

static double asind_q1 ( double  x)
static

Definition at line 2029 of file float.c.

References acos_0_5, and asin_0_5.

Referenced by dacosd(), and dasind().

2030 {
2031  /*
2032  * Stitch together inverse sine and cosine functions for the ranges [0,
2033  * 0.5] and (0.5, 1]. Each expression below is guaranteed to return
2034  * exactly 30 for x=0.5, so the result is a continuous monotonic function
2035  * over the full range.
2036  */
2037  if (x <= 0.5)
2038  {
2039  volatile float8 asin_x = asin(x);
2040 
2041  return (asin_x / asin_0_5) * 30.0;
2042  }
2043  else
2044  {
2045  volatile float8 acos_x = acos(x);
2046 
2047  return 90.0 - (acos_x / acos_0_5) * 60.0;
2048  }
2049 }
static float8 acos_0_5
Definition: float.c:72
double float8
Definition: c.h:458
static float8 asin_0_5
Definition: float.c:71

◆ btfloat48cmp()

Datum btfloat48cmp ( PG_FUNCTION_ARGS  )

Definition at line 1160 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_INT32.

1161 {
1162  float4 arg1 = PG_GETARG_FLOAT4(0);
1163  float8 arg2 = PG_GETARG_FLOAT8(1);
1164 
1165  /* widen float4 to float8 and then compare */
1166  PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
1167 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ btfloat4cmp()

Datum btfloat4cmp ( PG_FUNCTION_ARGS  )

Definition at line 1019 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_INT32.

Referenced by leftmostvalue_float4().

1020 {
1021  float4 arg1 = PG_GETARG_FLOAT4(0);
1022  float4 arg2 = PG_GETARG_FLOAT4(1);
1023 
1024  PG_RETURN_INT32(float4_cmp_internal(arg1, arg2));
1025 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ btfloat4fastcmp()

static int btfloat4fastcmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1028 of file float.c.

References DatumGetFloat4, and float4_cmp_internal().

Referenced by btfloat4sortsupport().

1029 {
1030  float4 arg1 = DatumGetFloat4(x);
1031  float4 arg2 = DatumGetFloat4(y);
1032 
1033  return float4_cmp_internal(arg1, arg2);
1034 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
float float4
Definition: c.h:457
#define DatumGetFloat4(X)
Definition: postgres.h:665

◆ btfloat4sortsupport()

Datum btfloat4sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1037 of file float.c.

References btfloat4fastcmp(), SortSupportData::comparator, PG_GETARG_POINTER, and PG_RETURN_VOID.

1038 {
1040 
1041  ssup->comparator = btfloat4fastcmp;
1042  PG_RETURN_VOID();
1043 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
#define PG_RETURN_VOID()
Definition: fmgr.h:314
static int btfloat4fastcmp(Datum x, Datum y, SortSupport ssup)
Definition: float.c:1028

◆ btfloat84cmp()

Datum btfloat84cmp ( PG_FUNCTION_ARGS  )

Definition at line 1170 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_INT32.

1171 {
1172  float8 arg1 = PG_GETARG_FLOAT8(0);
1173  float4 arg2 = PG_GETARG_FLOAT4(1);
1174 
1175  /* widen float4 to float8 and then compare */
1176  PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
1177 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ btfloat8cmp()

Datum btfloat8cmp ( PG_FUNCTION_ARGS  )

Definition at line 1133 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_INT32.

Referenced by leftmostvalue_float8().

1134 {
1135  float8 arg1 = PG_GETARG_FLOAT8(0);
1136  float8 arg2 = PG_GETARG_FLOAT8(1);
1137 
1138  PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
1139 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458

◆ btfloat8fastcmp()

static int btfloat8fastcmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1142 of file float.c.

References DatumGetFloat8, and float8_cmp_internal().

Referenced by btfloat8sortsupport().

1143 {
1144  float8 arg1 = DatumGetFloat8(x);
1145  float8 arg2 = DatumGetFloat8(y);
1146 
1147  return float8_cmp_internal(arg1, arg2);
1148 }
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define DatumGetFloat8(X)
Definition: postgres.h:713

◆ btfloat8sortsupport()

Datum btfloat8sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1151 of file float.c.

References btfloat8fastcmp(), SortSupportData::comparator, PG_GETARG_POINTER, and PG_RETURN_VOID.

1152 {
1154 
1155  ssup->comparator = btfloat8fastcmp;
1156  PG_RETURN_VOID();
1157 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
static int btfloat8fastcmp(Datum x, Datum y, SortSupport ssup)
Definition: float.c:1142
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
#define PG_RETURN_VOID()
Definition: fmgr.h:314

◆ cbrt()

static double cbrt ( double  x)
static

Definition at line 3743 of file float.c.

3744 {
3745  int isneg = (x < 0.0);
3746  double absx = fabs(x);
3747  double tmpres = pow(absx, (double) 1.0 / (double) 3.0);
3748 
3749  /*
3750  * The result is somewhat inaccurate --- not really pow()'s fault, as the
3751  * exponent it's handed contains roundoff error. We can improve the
3752  * accuracy by doing one iteration of Newton's formula. Beware of zero
3753  * input however.
3754  */
3755  if (tmpres > 0.0)
3756  tmpres -= (tmpres - absx / (tmpres * tmpres)) / (double) 3.0;
3757 
3758  return isneg ? -tmpres : tmpres;
3759 }

◆ check_float8_array()

static float8* check_float8_array ( ArrayType transarray,
const char *  caller,
int  n 
)
static

Definition at line 2624 of file float.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, elog, and ERROR.

Referenced by float4_accum(), float8_accum(), float8_avg(), float8_combine(), float8_corr(), float8_covar_pop(), float8_covar_samp(), float8_regr_accum(), float8_regr_avgx(), float8_regr_avgy(), float8_regr_combine(), float8_regr_intercept(), float8_regr_r2(), float8_regr_slope(), float8_regr_sxx(), float8_regr_sxy(), float8_regr_syy(), float8_stddev_pop(), float8_stddev_samp(), float8_var_pop(), and float8_var_samp().

2625 {
2626  /*
2627  * We expect the input to be an N-element float array; verify that. We
2628  * don't need to use deconstruct_array() since the array data is just
2629  * going to look like a C array of N float8 values.
2630  */
2631  if (ARR_NDIM(transarray) != 1 ||
2632  ARR_DIMS(transarray)[0] != n ||
2633  ARR_HASNULL(transarray) ||
2634  ARR_ELEMTYPE(transarray) != FLOAT8OID)
2635  elog(ERROR, "%s: expected %d-element float8 array", caller, n);
2636  return (float8 *) ARR_DATA_PTR(transarray);
2637 }
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define ARR_DIMS(a)
Definition: array.h:279
#define ARR_DATA_PTR(a)
Definition: array.h:307
#define ARR_HASNULL(a)
Definition: array.h:276
#define ARR_NDIM(a)
Definition: array.h:275
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:277

◆ cosd_0_to_60()

static double cosd_0_to_60 ( double  x)
static

Definition at line 2239 of file float.c.

References one_minus_cos_60, and RADIANS_PER_DEGREE.

Referenced by cosd_q1(), and sind_q1().

2240 {
2241  volatile float8 one_minus_cos_x = 1.0 - cos(x * RADIANS_PER_DEGREE);
2242 
2243  return 1.0 - (one_minus_cos_x / one_minus_cos_60) / 2.0;
2244 }
double float8
Definition: c.h:458
#define RADIANS_PER_DEGREE
Definition: float.c:36
static float8 one_minus_cos_60
Definition: float.c:70

◆ cosd_q1()

static double cosd_q1 ( double  x)
static

Definition at line 2272 of file float.c.

References cosd_0_to_60(), and sind_0_to_30().

Referenced by dcosd(), dcotd(), dtand(), and init_degree_constants().

2273 {
2274  /*
2275  * Stitch together the sine and cosine functions for the ranges [0, 60]
2276  * and (60, 90]. These guarantee to return exact answers at their
2277  * endpoints, so the overall result is a continuous monotonic function
2278  * that gives exact results when x = 0, 60 and 90 degrees.
2279  */
2280  if (x <= 60.0)
2281  return cosd_0_to_60(x);
2282  else
2283  return sind_0_to_30(90.0 - x);
2284 }
static double cosd_0_to_60(double x)
Definition: float.c:2239
static double sind_0_to_30(double x)
Definition: float.c:2225

◆ dacos()

Datum dacos ( PG_FUNCTION_ARGS  )

Definition at line 1743 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1744 {
1745  float8 arg1 = PG_GETARG_FLOAT8(0);
1746  float8 result;
1747 
1748  /* Per the POSIX spec, return NaN if the input is NaN */
1749  if (isnan(arg1))
1751 
1752  /*
1753  * The principal branch of the inverse cosine function maps values in the
1754  * range [-1, 1] to values in the range [0, Pi], so we should reject any
1755  * inputs outside that range and the result will always be finite.
1756  */
1757  if (arg1 < -1.0 || arg1 > 1.0)
1758  ereport(ERROR,
1759  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1760  errmsg("input is out of range")));
1761 
1762  result = acos(arg1);
1763 
1764  CHECKFLOATVAL(result, false, true);
1765  PG_RETURN_FLOAT8(result);
1766 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dacosd()

Datum dacosd ( PG_FUNCTION_ARGS  )

Definition at line 2089 of file float.c.

References acosd_q1(), asind_q1(), CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), INIT_DEGREE_CONSTANTS, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

2090 {
2091  float8 arg1 = PG_GETARG_FLOAT8(0);
2092  float8 result;
2093 
2094  /* Per the POSIX spec, return NaN if the input is NaN */
2095  if (isnan(arg1))
2097 
2099 
2100  /*
2101  * The principal branch of the inverse cosine function maps values in the
2102  * range [-1, 1] to values in the range [0, 180], so we should reject any
2103  * inputs outside that range and the result will always be finite.
2104  */
2105  if (arg1 < -1.0 || arg1 > 1.0)
2106  ereport(ERROR,
2107  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2108  errmsg("input is out of range")));
2109 
2110  if (arg1 >= 0.0)
2111  result = acosd_q1(arg1);
2112  else
2113  result = 90.0 + asind_q1(-arg1);
2114 
2115  CHECKFLOATVAL(result, false, true);
2116  PG_RETURN_FLOAT8(result);
2117 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
static double asind_q1(double x)
Definition: float.c:2029
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
static double acosd_q1(double x)
Definition: float.c:2062
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dasin()

Datum dasin ( PG_FUNCTION_ARGS  )

Definition at line 1773 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1774 {
1775  float8 arg1 = PG_GETARG_FLOAT8(0);
1776  float8 result;
1777 
1778  /* Per the POSIX spec, return NaN if the input is NaN */
1779  if (isnan(arg1))
1781 
1782  /*
1783  * The principal branch of the inverse sine function maps values in the
1784  * range [-1, 1] to values in the range [-Pi/2, Pi/2], so we should reject
1785  * any inputs outside that range and the result will always be finite.
1786  */
1787  if (arg1 < -1.0 || arg1 > 1.0)
1788  ereport(ERROR,
1789  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1790  errmsg("input is out of range")));
1791 
1792  result = asin(arg1);
1793 
1794  CHECKFLOATVAL(result, false, true);
1795  PG_RETURN_FLOAT8(result);
1796 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dasind()

Datum dasind ( PG_FUNCTION_ARGS  )

Definition at line 2124 of file float.c.

References asind_q1(), CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), INIT_DEGREE_CONSTANTS, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

2125 {
2126  float8 arg1 = PG_GETARG_FLOAT8(0);
2127  float8 result;
2128 
2129  /* Per the POSIX spec, return NaN if the input is NaN */
2130  if (isnan(arg1))
2132 
2134 
2135  /*
2136  * The principal branch of the inverse sine function maps values in the
2137  * range [-1, 1] to values in the range [-90, 90], so we should reject any
2138  * inputs outside that range and the result will always be finite.
2139  */
2140  if (arg1 < -1.0 || arg1 > 1.0)
2141  ereport(ERROR,
2142  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2143  errmsg("input is out of range")));
2144 
2145  if (arg1 >= 0.0)
2146  result = asind_q1(arg1);
2147  else
2148  result = -asind_q1(-arg1);
2149 
2150  CHECKFLOATVAL(result, false, true);
2151  PG_RETURN_FLOAT8(result);
2152 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
static double asind_q1(double x)
Definition: float.c:2029
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ datan()

Datum datan ( PG_FUNCTION_ARGS  )

Definition at line 1803 of file float.c.

References CHECKFLOATVAL, get_float8_nan(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1804 {
1805  float8 arg1 = PG_GETARG_FLOAT8(0);
1806  float8 result;
1807 
1808  /* Per the POSIX spec, return NaN if the input is NaN */
1809  if (isnan(arg1))
1811 
1812  /*
1813  * The principal branch of the inverse tangent function maps all inputs to
1814  * values in the range [-Pi/2, Pi/2], so the result should always be
1815  * finite, even if the input is infinite.
1816  */
1817  result = atan(arg1);
1818 
1819  CHECKFLOATVAL(result, false, true);
1820  PG_RETURN_FLOAT8(result);
1821 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ datan2()

Datum datan2 ( PG_FUNCTION_ARGS  )

Definition at line 1828 of file float.c.

References CHECKFLOATVAL, get_float8_nan(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1829 {
1830  float8 arg1 = PG_GETARG_FLOAT8(0);
1831  float8 arg2 = PG_GETARG_FLOAT8(1);
1832  float8 result;
1833 
1834  /* Per the POSIX spec, return NaN if either input is NaN */
1835  if (isnan(arg1) || isnan(arg2))
1837 
1838  /*
1839  * atan2 maps all inputs to values in the range [-Pi, Pi], so the result
1840  * should always be finite, even if the inputs are infinite.
1841  */
1842  result = atan2(arg1, arg2);
1843 
1844  CHECKFLOATVAL(result, false, true);
1845  PG_RETURN_FLOAT8(result);
1846 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ datan2d()

Datum datan2d ( PG_FUNCTION_ARGS  )

Definition at line 2189 of file float.c.

References atan_1_0, CHECKFLOATVAL, get_float8_nan(), INIT_DEGREE_CONSTANTS, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

2190 {
2191  float8 arg1 = PG_GETARG_FLOAT8(0);
2192  float8 arg2 = PG_GETARG_FLOAT8(1);
2193  float8 result;
2194  volatile float8 atan2_arg1_arg2;
2195 
2196  /* Per the POSIX spec, return NaN if either input is NaN */
2197  if (isnan(arg1) || isnan(arg2))
2199 
2201 
2202  /*
2203  * atan2d maps all inputs to values in the range [-180, 180], so the
2204  * result should always be finite, even if the inputs are infinite.
2205  *
2206  * Note: this coding assumes that atan(1.0) is a suitable scaling constant
2207  * to get an exact result from atan2(). This might well fail on us at
2208  * some point, requiring us to decide exactly what inputs we think we're
2209  * going to guarantee an exact result for.
2210  */
2211  atan2_arg1_arg2 = atan2(arg1, arg2);
2212  result = (atan2_arg1_arg2 / atan_1_0) * 45.0;
2213 
2214  CHECKFLOATVAL(result, false, true);
2215  PG_RETURN_FLOAT8(result);
2216 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
static float8 atan_1_0
Definition: float.c:73
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ datand()

Datum datand ( PG_FUNCTION_ARGS  )

Definition at line 2159 of file float.c.

References atan_1_0, CHECKFLOATVAL, get_float8_nan(), INIT_DEGREE_CONSTANTS, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

2160 {
2161  float8 arg1 = PG_GETARG_FLOAT8(0);
2162  float8 result;
2163  volatile float8 atan_arg1;
2164 
2165  /* Per the POSIX spec, return NaN if the input is NaN */
2166  if (isnan(arg1))
2168 
2170 
2171  /*
2172  * The principal branch of the inverse tangent function maps all inputs to
2173  * values in the range [-90, 90], so the result should always be finite,
2174  * even if the input is infinite. Additionally, we take care to ensure
2175  * than when arg1 is 1, the result is exactly 45.
2176  */
2177  atan_arg1 = atan(arg1);
2178  result = (atan_arg1 / atan_1_0) * 45.0;
2179 
2180  CHECKFLOATVAL(result, false, true);
2181  PG_RETURN_FLOAT8(result);
2182 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
static float8 atan_1_0
Definition: float.c:73
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ dcbrt()

Datum dcbrt ( PG_FUNCTION_ARGS  )

Definition at line 1578 of file float.c.

References cbrt, CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1579 {
1580  float8 arg1 = PG_GETARG_FLOAT8(0);
1581  float8 result;
1582 
1583  result = cbrt(arg1);
1584  CHECKFLOATVAL(result, isinf(arg1), arg1 == 0);
1585  PG_RETURN_FLOAT8(result);
1586 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define cbrt
Definition: float.c:103
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ dceil()

Datum dceil ( PG_FUNCTION_ARGS  )

Definition at line 1491 of file float.c.

References PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1492 {
1493  float8 arg1 = PG_GETARG_FLOAT8(0);
1494 
1495  PG_RETURN_FLOAT8(ceil(arg1));
1496 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458

◆ dcos()

Datum dcos ( PG_FUNCTION_ARGS  )

Definition at line 1853 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1854 {
1855  float8 arg1 = PG_GETARG_FLOAT8(0);
1856  float8 result;
1857 
1858  /* Per the POSIX spec, return NaN if the input is NaN */
1859  if (isnan(arg1))
1861 
1862  /*
1863  * cos() is periodic and so theoretically can work for all finite inputs,
1864  * but some implementations may choose to throw error if the input is so
1865  * large that there are no significant digits in the result. So we should
1866  * check for errors. POSIX allows an error to be reported either via
1867  * errno or via fetestexcept(), but currently we only support checking
1868  * errno. (fetestexcept() is rumored to report underflow unreasonably
1869  * early on some platforms, so it's not clear that believing it would be a
1870  * net improvement anyway.)
1871  *
1872  * For infinite inputs, POSIX specifies that the trigonometric functions
1873  * should return a domain error; but we won't notice that unless the
1874  * platform reports via errno, so also explicitly test for infinite
1875  * inputs.
1876  */
1877  errno = 0;
1878  result = cos(arg1);
1879  if (errno != 0 || isinf(arg1))
1880  ereport(ERROR,
1881  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1882  errmsg("input is out of range")));
1883 
1884  CHECKFLOATVAL(result, false, true);
1885  PG_RETURN_FLOAT8(result);
1886 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dcosd()

Datum dcosd ( PG_FUNCTION_ARGS  )

Definition at line 2291 of file float.c.

References CHECKFLOATVAL, cosd_q1(), ereport, errcode(), errmsg(), ERROR, get_float8_nan(), INIT_DEGREE_CONSTANTS, isinf(), PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, and sign.

2292 {
2293  float8 arg1 = PG_GETARG_FLOAT8(0);
2294  float8 result;
2295  int sign = 1;
2296 
2297  /*
2298  * Per the POSIX spec, return NaN if the input is NaN and throw an error
2299  * if the input is infinite.
2300  */
2301  if (isnan(arg1))
2303 
2304  if (isinf(arg1))
2305  ereport(ERROR,
2306  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2307  errmsg("input is out of range")));
2308 
2310 
2311  /* Reduce the range of the input to [0,90] degrees */
2312  arg1 = fmod(arg1, 360.0);
2313 
2314  if (arg1 < 0.0)
2315  {
2316  /* cosd(-x) = cosd(x) */
2317  arg1 = -arg1;
2318  }
2319 
2320  if (arg1 > 180.0)
2321  {
2322  /* cosd(360-x) = cosd(x) */
2323  arg1 = 360.0 - arg1;
2324  }
2325 
2326  if (arg1 > 90.0)
2327  {
2328  /* cosd(180-x) = -cosd(x) */
2329  arg1 = 180.0 - arg1;
2330  sign = -sign;
2331  }
2332 
2333  result = sign * cosd_q1(arg1);
2334 
2335  CHECKFLOATVAL(result, false, true);
2336  PG_RETURN_FLOAT8(result);
2337 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
char sign
Definition: informix.c:693
#define ereport(elevel, rest)
Definition: elog.h:122
static double cosd_q1(double x)
Definition: float.c:2272
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dcot()

Datum dcot ( PG_FUNCTION_ARGS  )

Definition at line 1893 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1894 {
1895  float8 arg1 = PG_GETARG_FLOAT8(0);
1896  float8 result;
1897 
1898  /* Per the POSIX spec, return NaN if the input is NaN */
1899  if (isnan(arg1))
1901 
1902  /* Be sure to throw an error if the input is infinite --- see dcos() */
1903  errno = 0;
1904  result = tan(arg1);
1905  if (errno != 0 || isinf(arg1))
1906  ereport(ERROR,
1907  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1908  errmsg("input is out of range")));
1909 
1910  result = 1.0 / result;
1911  CHECKFLOATVAL(result, true /* cot(0) == Inf */ , true);
1912  PG_RETURN_FLOAT8(result);
1913 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dcotd()

Datum dcotd ( PG_FUNCTION_ARGS  )

Definition at line 2344 of file float.c.

References CHECKFLOATVAL, cosd_q1(), cot_45, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), INIT_DEGREE_CONSTANTS, isinf(), PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, sign, and sind_q1().

2345 {
2346  float8 arg1 = PG_GETARG_FLOAT8(0);
2347  float8 result;
2348  volatile float8 cot_arg1;
2349  int sign = 1;
2350 
2351  /*
2352  * Per the POSIX spec, return NaN if the input is NaN and throw an error
2353  * if the input is infinite.
2354  */
2355  if (isnan(arg1))
2357 
2358  if (isinf(arg1))
2359  ereport(ERROR,
2360  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2361  errmsg("input is out of range")));
2362 
2364 
2365  /* Reduce the range of the input to [0,90] degrees */
2366  arg1 = fmod(arg1, 360.0);
2367 
2368  if (arg1 < 0.0)
2369  {
2370  /* cotd(-x) = -cotd(x) */
2371  arg1 = -arg1;
2372  sign = -sign;
2373  }
2374 
2375  if (arg1 > 180.0)
2376  {
2377  /* cotd(360-x) = -cotd(x) */
2378  arg1 = 360.0 - arg1;
2379  sign = -sign;
2380  }
2381 
2382  if (arg1 > 90.0)
2383  {
2384  /* cotd(180-x) = -cotd(x) */
2385  arg1 = 180.0 - arg1;
2386  sign = -sign;
2387  }
2388 
2389  cot_arg1 = cosd_q1(arg1) / sind_q1(arg1);
2390  result = sign * (cot_arg1 / cot_45);
2391 
2392  /*
2393  * On some machines we get cotd(270) = minus zero, but this isn't always
2394  * true. For portability, and because the user constituency for this
2395  * function probably doesn't want minus zero, force it to plain zero.
2396  */
2397  if (result == 0.0)
2398  result = 0.0;
2399 
2400  CHECKFLOATVAL(result, true /* cotd(0) == Inf */ , true);
2401  PG_RETURN_FLOAT8(result);
2402 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
static float8 cot_45
Definition: float.c:75
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
static double sind_q1(double x)
Definition: float.c:2252
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
char sign
Definition: informix.c:693
#define ereport(elevel, rest)
Definition: elog.h:122
static double cosd_q1(double x)
Definition: float.c:2272
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ degrees()

Datum degrees ( PG_FUNCTION_ARGS  )

Definition at line 2528 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, and RADIANS_PER_DEGREE.

2529 {
2530  float8 arg1 = PG_GETARG_FLOAT8(0);
2531  float8 result;
2532 
2533  result = arg1 / RADIANS_PER_DEGREE;
2534 
2535  CHECKFLOATVAL(result, isinf(arg1), arg1 == 0);
2536  PG_RETURN_FLOAT8(result);
2537 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define RADIANS_PER_DEGREE
Definition: float.c:36
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ dexp()

Datum dexp ( PG_FUNCTION_ARGS  )

Definition at line 1665 of file float.c.

References CHECKFLOATVAL, get_float8_infinity(), isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1666 {
1667  float8 arg1 = PG_GETARG_FLOAT8(0);
1668  float8 result;
1669 
1670  errno = 0;
1671  result = exp(arg1);
1672  if (errno == ERANGE && result != 0 && !isinf(result))
1673  result = get_float8_infinity();
1674 
1675  CHECKFLOATVAL(result, isinf(arg1), false);
1676  PG_RETURN_FLOAT8(result);
1677 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
double get_float8_infinity(void)
Definition: float.c:118
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ dfloor()

Datum dfloor ( PG_FUNCTION_ARGS  )

Definition at line 1503 of file float.c.

References PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1504 {
1505  float8 arg1 = PG_GETARG_FLOAT8(0);
1506 
1507  PG_RETURN_FLOAT8(floor(arg1));
1508 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458

◆ dlog1()

Datum dlog1 ( PG_FUNCTION_ARGS  )

Definition at line 1684 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1685 {
1686  float8 arg1 = PG_GETARG_FLOAT8(0);
1687  float8 result;
1688 
1689  /*
1690  * Emit particular SQLSTATE error codes for ln(). This is required by the
1691  * SQL standard.
1692  */
1693  if (arg1 == 0.0)
1694  ereport(ERROR,
1695  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1696  errmsg("cannot take logarithm of zero")));
1697  if (arg1 < 0)
1698  ereport(ERROR,
1699  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1700  errmsg("cannot take logarithm of a negative number")));
1701 
1702  result = log(arg1);
1703 
1704  CHECKFLOATVAL(result, isinf(arg1), arg1 == 1);
1705  PG_RETURN_FLOAT8(result);
1706 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dlog10()

Datum dlog10 ( PG_FUNCTION_ARGS  )

Definition at line 1713 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1714 {
1715  float8 arg1 = PG_GETARG_FLOAT8(0);
1716  float8 result;
1717 
1718  /*
1719  * Emit particular SQLSTATE error codes for log(). The SQL spec doesn't
1720  * define log(), but it does define ln(), so it makes sense to emit the
1721  * same error code for an analogous error condition.
1722  */
1723  if (arg1 == 0.0)
1724  ereport(ERROR,
1725  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1726  errmsg("cannot take logarithm of zero")));
1727  if (arg1 < 0)
1728  ereport(ERROR,
1729  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1730  errmsg("cannot take logarithm of a negative number")));
1731 
1732  result = log10(arg1);
1733 
1734  CHECKFLOATVAL(result, isinf(arg1), arg1 == 1);
1735  PG_RETURN_FLOAT8(result);
1736 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dpi()

Definition at line 2544 of file float.c.

References M_PI, and PG_RETURN_FLOAT8.

2545 {
2547 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define M_PI
Definition: float.c:32

◆ dpow()

Datum dpow ( PG_FUNCTION_ARGS  )

Definition at line 1593 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_infinity(), get_float8_nan(), isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1594 {
1595  float8 arg1 = PG_GETARG_FLOAT8(0);
1596  float8 arg2 = PG_GETARG_FLOAT8(1);
1597  float8 result;
1598 
1599  /*
1600  * The POSIX spec says that NaN ^ 0 = 1, and 1 ^ NaN = 1, while all other
1601  * cases with NaN inputs yield NaN (with no error). Many older platforms
1602  * get one or more of these cases wrong, so deal with them via explicit
1603  * logic rather than trusting pow(3).
1604  */
1605  if (isnan(arg1))
1606  {
1607  if (isnan(arg2) || arg2 != 0.0)
1609  PG_RETURN_FLOAT8(1.0);
1610  }
1611  if (isnan(arg2))
1612  {
1613  if (arg1 != 1.0)
1615  PG_RETURN_FLOAT8(1.0);
1616  }
1617 
1618  /*
1619  * The SQL spec requires that we emit a particular SQLSTATE error code for
1620  * certain error conditions. Specifically, we don't return a
1621  * divide-by-zero error code for 0 ^ -1.
1622  */
1623  if (arg1 == 0 && arg2 < 0)
1624  ereport(ERROR,
1625  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
1626  errmsg("zero raised to a negative power is undefined")));
1627  if (arg1 < 0 && floor(arg2) != arg2)
1628  ereport(ERROR,
1629  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
1630  errmsg("a negative number raised to a non-integer power yields a complex result")));
1631 
1632  /*
1633  * pow() sets errno only on some platforms, depending on whether it
1634  * follows _IEEE_, _POSIX_, _XOPEN_, or _SVID_, so we try to avoid using
1635  * errno. However, some platform/CPU combinations return errno == EDOM
1636  * and result == NaN for negative arg1 and very large arg2 (they must be
1637  * using something different from our floor() test to decide it's
1638  * invalid). Other platforms (HPPA) return errno == ERANGE and a large
1639  * (HUGE_VAL) but finite result to signal overflow.
1640  */
1641  errno = 0;
1642  result = pow(arg1, arg2);
1643  if (errno == EDOM && isnan(result))
1644  {
1645  if ((fabs(arg1) > 1 && arg2 >= 0) || (fabs(arg1) < 1 && arg2 < 0))
1646  /* The sign of Inf is not significant in this case. */
1647  result = get_float8_infinity();
1648  else if (fabs(arg1) != 1)
1649  result = 0;
1650  else
1651  result = 1;
1652  }
1653  else if (errno == ERANGE && result != 0 && !isinf(result))
1654  result = get_float8_infinity();
1655 
1656  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), arg1 == 0);
1657  PG_RETURN_FLOAT8(result);
1658 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
double get_float8_infinity(void)
Definition: float.c:118
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ drandom()

Datum drandom ( PG_FUNCTION_ARGS  )

Definition at line 2570 of file float.c.

References MAX_RANDOM_VALUE, PG_RETURN_FLOAT8, and random().

2571 {
2572  float8 result;
2573 
2574  /* result [0.0 - 1.0) */
2575  result = (double) random() / ((double) MAX_RANDOM_VALUE + 1);
2576 
2577  PG_RETURN_FLOAT8(result);
2578 }
long random(void)
Definition: random.c:22
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
#define MAX_RANDOM_VALUE

◆ dround()

Datum dround ( PG_FUNCTION_ARGS  )

Definition at line 1479 of file float.c.

References PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, and rint().

1480 {
1481  float8 arg1 = PG_GETARG_FLOAT8(0);
1482 
1483  PG_RETURN_FLOAT8(rint(arg1));
1484 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
double rint(double x)
Definition: rint.c:22

◆ dsign()

Datum dsign ( PG_FUNCTION_ARGS  )

Definition at line 1516 of file float.c.

References PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1517 {
1518  float8 arg1 = PG_GETARG_FLOAT8(0);
1519  float8 result;
1520 
1521  if (arg1 > 0)
1522  result = 1.0;
1523  else if (arg1 < 0)
1524  result = -1.0;
1525  else
1526  result = 0.0;
1527 
1528  PG_RETURN_FLOAT8(result);
1529 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458

◆ dsin()

Datum dsin ( PG_FUNCTION_ARGS  )

Definition at line 1920 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1921 {
1922  float8 arg1 = PG_GETARG_FLOAT8(0);
1923  float8 result;
1924 
1925  /* Per the POSIX spec, return NaN if the input is NaN */
1926  if (isnan(arg1))
1928 
1929  /* Be sure to throw an error if the input is infinite --- see dcos() */
1930  errno = 0;
1931  result = sin(arg1);
1932  if (errno != 0 || isinf(arg1))
1933  ereport(ERROR,
1934  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1935  errmsg("input is out of range")));
1936 
1937  CHECKFLOATVAL(result, false, true);
1938  PG_RETURN_FLOAT8(result);
1939 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dsind()

Datum dsind ( PG_FUNCTION_ARGS  )

Definition at line 2409 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), INIT_DEGREE_CONSTANTS, isinf(), PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, sign, and sind_q1().

2410 {
2411  float8 arg1 = PG_GETARG_FLOAT8(0);
2412  float8 result;
2413  int sign = 1;
2414 
2415  /*
2416  * Per the POSIX spec, return NaN if the input is NaN and throw an error
2417  * if the input is infinite.
2418  */
2419  if (isnan(arg1))
2421 
2422  if (isinf(arg1))
2423  ereport(ERROR,
2424  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2425  errmsg("input is out of range")));
2426 
2428 
2429  /* Reduce the range of the input to [0,90] degrees */
2430  arg1 = fmod(arg1, 360.0);
2431 
2432  if (arg1 < 0.0)
2433  {
2434  /* sind(-x) = -sind(x) */
2435  arg1 = -arg1;
2436  sign = -sign;
2437  }
2438 
2439  if (arg1 > 180.0)
2440  {
2441  /* sind(360-x) = -sind(x) */
2442  arg1 = 360.0 - arg1;
2443  sign = -sign;
2444  }
2445 
2446  if (arg1 > 90.0)
2447  {
2448  /* sind(180-x) = sind(x) */
2449  arg1 = 180.0 - arg1;
2450  }
2451 
2452  result = sign * sind_q1(arg1);
2453 
2454  CHECKFLOATVAL(result, false, true);
2455  PG_RETURN_FLOAT8(result);
2456 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
static double sind_q1(double x)
Definition: float.c:2252
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
char sign
Definition: informix.c:693
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dsqrt()

Datum dsqrt ( PG_FUNCTION_ARGS  )

Definition at line 1557 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1558 {
1559  float8 arg1 = PG_GETARG_FLOAT8(0);
1560  float8 result;
1561 
1562  if (arg1 < 0)
1563  ereport(ERROR,
1564  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
1565  errmsg("cannot take square root of a negative number")));
1566 
1567  result = sqrt(arg1);
1568 
1569  CHECKFLOATVAL(result, isinf(arg1), arg1 == 0);
1570  PG_RETURN_FLOAT8(result);
1571 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dtan()

Datum dtan ( PG_FUNCTION_ARGS  )

Definition at line 1946 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float8_nan(), isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1947 {
1948  float8 arg1 = PG_GETARG_FLOAT8(0);
1949  float8 result;
1950 
1951  /* Per the POSIX spec, return NaN if the input is NaN */
1952  if (isnan(arg1))
1954 
1955  /* Be sure to throw an error if the input is infinite --- see dcos() */
1956  errno = 0;
1957  result = tan(arg1);
1958  if (errno != 0 || isinf(arg1))
1959  ereport(ERROR,
1960  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1961  errmsg("input is out of range")));
1962 
1963  CHECKFLOATVAL(result, true /* tan(pi/2) == Inf */ , true);
1964  PG_RETURN_FLOAT8(result);
1965 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dtand()

Datum dtand ( PG_FUNCTION_ARGS  )

Definition at line 2463 of file float.c.

References CHECKFLOATVAL, cosd_q1(), ereport, errcode(), errmsg(), ERROR, get_float8_nan(), INIT_DEGREE_CONSTANTS, isinf(), PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, sign, sind_q1(), and tan_45.

2464 {
2465  float8 arg1 = PG_GETARG_FLOAT8(0);
2466  float8 result;
2467  volatile float8 tan_arg1;
2468  int sign = 1;
2469 
2470  /*
2471  * Per the POSIX spec, return NaN if the input is NaN and throw an error
2472  * if the input is infinite.
2473  */
2474  if (isnan(arg1))
2476 
2477  if (isinf(arg1))
2478  ereport(ERROR,
2479  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2480  errmsg("input is out of range")));
2481 
2483 
2484  /* Reduce the range of the input to [0,90] degrees */
2485  arg1 = fmod(arg1, 360.0);
2486 
2487  if (arg1 < 0.0)
2488  {
2489  /* tand(-x) = -tand(x) */
2490  arg1 = -arg1;
2491  sign = -sign;
2492  }
2493 
2494  if (arg1 > 180.0)
2495  {
2496  /* tand(360-x) = -tand(x) */
2497  arg1 = 360.0 - arg1;
2498  sign = -sign;
2499  }
2500 
2501  if (arg1 > 90.0)
2502  {
2503  /* tand(180-x) = -tand(x) */
2504  arg1 = 180.0 - arg1;
2505  sign = -sign;
2506  }
2507 
2508  tan_arg1 = sind_q1(arg1) / cosd_q1(arg1);
2509  result = sign * (tan_arg1 / tan_45);
2510 
2511  /*
2512  * On some machines we get tand(180) = minus zero, but this isn't always
2513  * true. For portability, and because the user constituency for this
2514  * function probably doesn't want minus zero, force it to plain zero.
2515  */
2516  if (result == 0.0)
2517  result = 0.0;
2518 
2519  CHECKFLOATVAL(result, true /* tand(90) == Inf */ , true);
2520  PG_RETURN_FLOAT8(result);
2521 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
double get_float8_nan(void)
Definition: float.c:163
#define INIT_DEGREE_CONSTANTS()
Definition: float.c:2012
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
static double sind_q1(double x)
Definition: float.c:2252
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
char sign
Definition: informix.c:693
#define ereport(elevel, rest)
Definition: elog.h:122
static double cosd_q1(double x)
Definition: float.c:2272
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797
static float8 tan_45
Definition: float.c:74

◆ dtof()

Datum dtof ( PG_FUNCTION_ARGS  )

Definition at line 1340 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT4.

1341 {
1342  float8 num = PG_GETARG_FLOAT8(0);
1343 
1344  CHECKFLOATVAL((float4) num, isinf(num), num == 0);
1345 
1346  PG_RETURN_FLOAT4((float4) num);
1347 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
double float8
Definition: c.h:458
int isinf(double x)
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ dtoi2()

Datum dtoi2 ( PG_FUNCTION_ARGS  )

Definition at line 1374 of file float.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT8, PG_RETURN_INT16, and rint().

1375 {
1376  float8 num = PG_GETARG_FLOAT8(0);
1377 
1378  if (num < SHRT_MIN || num > SHRT_MAX || isnan(num))
1379  ereport(ERROR,
1380  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1381  errmsg("smallint out of range")));
1382 
1383  PG_RETURN_INT16((int16) rint(num));
1384 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
signed short int16
Definition: c.h:312
#define PG_RETURN_INT16(x)
Definition: fmgr.h:321
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dtoi4()

Datum dtoi4 ( PG_FUNCTION_ARGS  )

Definition at line 1354 of file float.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT8, PG_RETURN_INT32, and rint().

1355 {
1356  float8 num = PG_GETARG_FLOAT8(0);
1357  int32 result;
1358 
1359  /* 'Inf' is handled by INT_MAX */
1360  if (num < INT_MIN || num > INT_MAX || isnan(num))
1361  ereport(ERROR,
1362  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1363  errmsg("integer out of range")));
1364 
1365  result = (int32) rint(num);
1366  PG_RETURN_INT32(result);
1367 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ dtrunc()

Datum dtrunc ( PG_FUNCTION_ARGS  )

Definition at line 1539 of file float.c.

References PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

1540 {
1541  float8 arg1 = PG_GETARG_FLOAT8(0);
1542  float8 result;
1543 
1544  if (arg1 >= 0)
1545  result = floor(arg1);
1546  else
1547  result = -floor(-arg1);
1548 
1549  PG_RETURN_FLOAT8(result);
1550 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458

◆ float48div()

Datum float48div ( PG_FUNCTION_ARGS  )

Definition at line 3460 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3461 {
3462  float4 arg1 = PG_GETARG_FLOAT4(0);
3463  float8 arg2 = PG_GETARG_FLOAT8(1);
3464  float8 result;
3465 
3466  if (arg2 == 0.0)
3467  ereport(ERROR,
3468  (errcode(ERRCODE_DIVISION_BY_ZERO),
3469  errmsg("division by zero")));
3470 
3471  result = arg1 / arg2;
3472  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), arg1 == 0);
3473  PG_RETURN_FLOAT8(result);
3474 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ float48eq()

Datum float48eq ( PG_FUNCTION_ARGS  )

Definition at line 3550 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3551 {
3552  float4 arg1 = PG_GETARG_FLOAT4(0);
3553  float8 arg2 = PG_GETARG_FLOAT8(1);
3554 
3555  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);
3556 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float48ge()

Datum float48ge ( PG_FUNCTION_ARGS  )

Definition at line 3595 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3596 {
3597  float4 arg1 = PG_GETARG_FLOAT4(0);
3598  float8 arg2 = PG_GETARG_FLOAT8(1);
3599 
3600  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);
3601 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float48gt()

Datum float48gt ( PG_FUNCTION_ARGS  )

Definition at line 3586 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3587 {
3588  float4 arg1 = PG_GETARG_FLOAT4(0);
3589  float8 arg2 = PG_GETARG_FLOAT8(1);
3590 
3591  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);
3592 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float48le()

Datum float48le ( PG_FUNCTION_ARGS  )

Definition at line 3577 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3578 {
3579  float4 arg1 = PG_GETARG_FLOAT4(0);
3580  float8 arg2 = PG_GETARG_FLOAT8(1);
3581 
3582  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);
3583 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float48lt()

Datum float48lt ( PG_FUNCTION_ARGS  )

Definition at line 3568 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3569 {
3570  float4 arg1 = PG_GETARG_FLOAT4(0);
3571  float8 arg2 = PG_GETARG_FLOAT8(1);
3572 
3573  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);
3574 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float48mi()

Datum float48mi ( PG_FUNCTION_ARGS  )

Definition at line 3435 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3436 {
3437  float4 arg1 = PG_GETARG_FLOAT4(0);
3438  float8 arg2 = PG_GETARG_FLOAT8(1);
3439  float8 result;
3440 
3441  result = arg1 - arg2;
3442  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
3443  PG_RETURN_FLOAT8(result);
3444 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float48mul()

Datum float48mul ( PG_FUNCTION_ARGS  )

Definition at line 3447 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3448 {
3449  float4 arg1 = PG_GETARG_FLOAT4(0);
3450  float8 arg2 = PG_GETARG_FLOAT8(1);
3451  float8 result;
3452 
3453  result = arg1 * arg2;
3454  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2),
3455  arg1 == 0 || arg2 == 0);
3456  PG_RETURN_FLOAT8(result);
3457 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float48ne()

Datum float48ne ( PG_FUNCTION_ARGS  )

Definition at line 3559 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3560 {
3561  float4 arg1 = PG_GETARG_FLOAT4(0);
3562  float8 arg2 = PG_GETARG_FLOAT8(1);
3563 
3564  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);
3565 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float48pl()

Datum float48pl ( PG_FUNCTION_ARGS  )

Definition at line 3423 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3424 {
3425  float4 arg1 = PG_GETARG_FLOAT4(0);
3426  float8 arg2 = PG_GETARG_FLOAT8(1);
3427  float8 result;
3428 
3429  result = arg1 + arg2;
3430  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
3431  PG_RETURN_FLOAT8(result);
3432 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float4_accum()

Datum float4_accum ( PG_FUNCTION_ARGS  )

Definition at line 2735 of file float.c.

References AggCheckCallContext(), check_float8_array(), CHECKFLOATVAL, construct_array(), Float8GetDatumFast, isinf(), newval, PG_GETARG_ARRAYTYPE_P, PG_GETARG_FLOAT4, and PG_RETURN_ARRAYTYPE_P.

2736 {
2737  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2738 
2739  /* do computations as float8 */
2741  float8 *transvalues;
2742  float8 N,
2743  sumX,
2744  sumX2;
2745 
2746  transvalues = check_float8_array(transarray, "float4_accum", 3);
2747  N = transvalues[0];
2748  sumX = transvalues[1];
2749  sumX2 = transvalues[2];
2750 
2751  N += 1.0;
2752  sumX += newval;
2753  CHECKFLOATVAL(sumX, isinf(transvalues[1]) || isinf(newval), true);
2754  sumX2 += newval * newval;
2755  CHECKFLOATVAL(sumX2, isinf(transvalues[2]) || isinf(newval), true);
2756 
2757  /*
2758  * If we're invoked as an aggregate, we can cheat and modify our first
2759  * parameter in-place to reduce palloc overhead. Otherwise we construct a
2760  * new array with the updated transition data and return it.
2761  */
2762  if (AggCheckCallContext(fcinfo, NULL))
2763  {
2764  transvalues[0] = N;
2765  transvalues[1] = sumX;
2766  transvalues[2] = sumX2;
2767 
2768  PG_RETURN_ARRAYTYPE_P(transarray);
2769  }
2770  else
2771  {
2772  Datum transdatums[3];
2773  ArrayType *result;
2774 
2775  transdatums[0] = Float8GetDatumFast(N);
2776  transdatums[1] = Float8GetDatumFast(sumX);
2777  transdatums[2] = Float8GetDatumFast(sumX2);
2778 
2779  result = construct_array(transdatums, 3,
2780  FLOAT8OID,
2781  sizeof(float8), FLOAT8PASSBYVAL, 'd');
2782 
2783  PG_RETURN_ARRAYTYPE_P(result);
2784  }
2785 }
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
uintptr_t Datum
Definition: postgres.h:367
#define Float8GetDatumFast(X)
Definition: postgres.h:761
#define newval
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624

◆ float4_cmp_internal()

int float4_cmp_internal ( float4  a,
float4  b 
)

Definition at line 935 of file float.c.

Referenced by btfloat4cmp(), btfloat4fastcmp(), float4eq(), float4ge(), float4gt(), float4larger(), float4le(), float4lt(), float4ne(), and float4smaller().

936 {
937  /*
938  * We consider all NANs to be equal and larger than any non-NAN. This is
939  * somewhat arbitrary; the important thing is to have a consistent sort
940  * order.
941  */
942  if (isnan(a))
943  {
944  if (isnan(b))
945  return 0; /* NAN = NAN */
946  else
947  return 1; /* NAN > non-NAN */
948  }
949  else if (isnan(b))
950  {
951  return -1; /* non-NAN < NAN */
952  }
953  else
954  {
955  if (a > b)
956  return 1;
957  else if (a < b)
958  return -1;
959  else
960  return 0;
961  }
962 }

◆ float4abs()

Datum float4abs ( PG_FUNCTION_ARGS  )

Definition at line 661 of file float.c.

References PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

662 {
663  float4 arg1 = PG_GETARG_FLOAT4(0);
664 
665  PG_RETURN_FLOAT4((float4) fabs(arg1));
666 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ float4div()

Datum float4div ( PG_FUNCTION_ARGS  )

Definition at line 843 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

844 {
845  float4 arg1 = PG_GETARG_FLOAT4(0);
846  float4 arg2 = PG_GETARG_FLOAT4(1);
847  float4 result;
848 
849  if (arg2 == 0.0)
850  ereport(ERROR,
851  (errcode(ERRCODE_DIVISION_BY_ZERO),
852  errmsg("division by zero")));
853 
854  result = arg1 / arg2;
855 
856  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), arg1 == 0);
857  PG_RETURN_FLOAT4(result);
858 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ float4eq()

Datum float4eq ( PG_FUNCTION_ARGS  )

Definition at line 965 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_BOOL.

966 {
967  float4 arg1 = PG_GETARG_FLOAT4(0);
968  float4 arg2 = PG_GETARG_FLOAT4(1);
969 
970  PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) == 0);
971 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float4ge()

Datum float4ge ( PG_FUNCTION_ARGS  )

Definition at line 1010 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_BOOL.

1011 {
1012  float4 arg1 = PG_GETARG_FLOAT4(0);
1013  float4 arg2 = PG_GETARG_FLOAT4(1);
1014 
1015  PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) >= 0);
1016 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float4gt()

Datum float4gt ( PG_FUNCTION_ARGS  )

Definition at line 1001 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_BOOL.

1002 {
1003  float4 arg1 = PG_GETARG_FLOAT4(0);
1004  float4 arg2 = PG_GETARG_FLOAT4(1);
1005 
1006  PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) > 0);
1007 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float4in()

Datum float4in ( PG_FUNCTION_ARGS  )

Definition at line 216 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, get_float4_infinity(), get_float4_nan(), isinf(), PG_GETARG_CSTRING, PG_RETURN_FLOAT4, pg_strncasecmp(), and val.

Referenced by numeric_float4().

217 {
218  char *num = PG_GETARG_CSTRING(0);
219  char *orig_num;
220  double val;
221  char *endptr;
222 
223  /*
224  * endptr points to the first character _after_ the sequence we recognized
225  * as a valid floating point number. orig_num points to the original input
226  * string.
227  */
228  orig_num = num;
229 
230  /* skip leading whitespace */
231  while (*num != '\0' && isspace((unsigned char) *num))
232  num++;
233 
234  /*
235  * Check for an empty-string input to begin with, to avoid the vagaries of
236  * strtod() on different platforms.
237  */
238  if (*num == '\0')
239  ereport(ERROR,
240  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
241  errmsg("invalid input syntax for type %s: \"%s\"",
242  "real", orig_num)));
243 
244  errno = 0;
245  val = strtod(num, &endptr);
246 
247  /* did we not see anything that looks like a double? */
248  if (endptr == num || errno != 0)
249  {
250  int save_errno = errno;
251 
252  /*
253  * C99 requires that strtod() accept NaN, [+-]Infinity, and [+-]Inf,
254  * but not all platforms support all of these (and some accept them
255  * but set ERANGE anyway...) Therefore, we check for these inputs
256  * ourselves if strtod() fails.
257  *
258  * Note: C99 also requires hexadecimal input as well as some extended
259  * forms of NaN, but we consider these forms unportable and don't try
260  * to support them. You can use 'em if your strtod() takes 'em.
261  */
262  if (pg_strncasecmp(num, "NaN", 3) == 0)
263  {
264  val = get_float4_nan();
265  endptr = num + 3;
266  }
267  else if (pg_strncasecmp(num, "Infinity", 8) == 0)
268  {
269  val = get_float4_infinity();
270  endptr = num + 8;
271  }
272  else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
273  {
274  val = get_float4_infinity();
275  endptr = num + 9;
276  }
277  else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
278  {
279  val = -get_float4_infinity();
280  endptr = num + 9;
281  }
282  else if (pg_strncasecmp(num, "inf", 3) == 0)
283  {
284  val = get_float4_infinity();
285  endptr = num + 3;
286  }
287  else if (pg_strncasecmp(num, "+inf", 4) == 0)
288  {
289  val = get_float4_infinity();
290  endptr = num + 4;
291  }
292  else if (pg_strncasecmp(num, "-inf", 4) == 0)
293  {
294  val = -get_float4_infinity();
295  endptr = num + 4;
296  }
297  else if (save_errno == ERANGE)
298  {
299  /*
300  * Some platforms return ERANGE for denormalized numbers (those
301  * that are not zero, but are too close to zero to have full
302  * precision). We'd prefer not to throw error for that, so try to
303  * detect whether it's a "real" out-of-range condition by checking
304  * to see if the result is zero or huge.
305  */
306  if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL)
307  ereport(ERROR,
308  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
309  errmsg("\"%s\" is out of range for type real",
310  orig_num)));
311  }
312  else
313  ereport(ERROR,
314  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
315  errmsg("invalid input syntax for type %s: \"%s\"",
316  "real", orig_num)));
317  }
318 #ifdef HAVE_BUGGY_SOLARIS_STRTOD
319  else
320  {
321  /*
322  * Many versions of Solaris have a bug wherein strtod sets endptr to
323  * point one byte beyond the end of the string when given "inf" or
324  * "infinity".
325  */
326  if (endptr != num && endptr[-1] == '\0')
327  endptr--;
328  }
329 #endif /* HAVE_BUGGY_SOLARIS_STRTOD */
330 
331  /* skip trailing whitespace */
332  while (*endptr != '\0' && isspace((unsigned char) *endptr))
333  endptr++;
334 
335  /* if there is any junk left at the end of the string, bail out */
336  if (*endptr != '\0')
337  ereport(ERROR,
338  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
339  errmsg("invalid input syntax for type %s: \"%s\"",
340  "real", orig_num)));
341 
342  /*
343  * if we get here, we have a legal double, still need to check to see if
344  * it's a legal float4
345  */
346  CHECKFLOATVAL((float4) val, isinf(val), val == 0);
347 
348  PG_RETURN_FLOAT4((float4) val);
349 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
float get_float4_nan(void)
Definition: float.c:176
int errcode(int sqlerrcode)
Definition: elog.c:575
float get_float4_infinity(void)
Definition: float.c:143
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define ERROR
Definition: elog.h:43
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:247
long val
Definition: informix.c:689

◆ float4larger()

Datum float4larger ( PG_FUNCTION_ARGS  )

Definition at line 690 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

691 {
692  float4 arg1 = PG_GETARG_FLOAT4(0);
693  float4 arg2 = PG_GETARG_FLOAT4(1);
694  float4 result;
695 
696  if (float4_cmp_internal(arg1, arg2) > 0)
697  result = arg1;
698  else
699  result = arg2;
700  PG_RETURN_FLOAT4(result);
701 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ float4le()

Datum float4le ( PG_FUNCTION_ARGS  )

Definition at line 992 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_BOOL.

993 {
994  float4 arg1 = PG_GETARG_FLOAT4(0);
995  float4 arg2 = PG_GETARG_FLOAT4(1);
996 
997  PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) <= 0);
998 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float4lt()

Datum float4lt ( PG_FUNCTION_ARGS  )

Definition at line 983 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_BOOL.

984 {
985  float4 arg1 = PG_GETARG_FLOAT4(0);
986  float4 arg2 = PG_GETARG_FLOAT4(1);
987 
988  PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) < 0);
989 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float4mi()

Datum float4mi ( PG_FUNCTION_ARGS  )

Definition at line 818 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

819 {
820  float4 arg1 = PG_GETARG_FLOAT4(0);
821  float4 arg2 = PG_GETARG_FLOAT4(1);
822  float4 result;
823 
824  result = arg1 - arg2;
825  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
826  PG_RETURN_FLOAT4(result);
827 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float4mul()

Datum float4mul ( PG_FUNCTION_ARGS  )

Definition at line 830 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

831 {
832  float4 arg1 = PG_GETARG_FLOAT4(0);
833  float4 arg2 = PG_GETARG_FLOAT4(1);
834  float4 result;
835 
836  result = arg1 * arg2;
837  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2),
838  arg1 == 0 || arg2 == 0);
839  PG_RETURN_FLOAT4(result);
840 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float4ne()

Datum float4ne ( PG_FUNCTION_ARGS  )

Definition at line 974 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_BOOL.

975 {
976  float4 arg1 = PG_GETARG_FLOAT4(0);
977  float4 arg2 = PG_GETARG_FLOAT4(1);
978 
979  PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) != 0);
980 }
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float4out()

Datum float4out ( PG_FUNCTION_ARGS  )

Definition at line 356 of file float.c.

References ascii(), extra_float_digits, is_infinite(), PG_GETARG_FLOAT4, PG_RETURN_CSTRING, psprintf(), and pstrdup().

357 {
358  float4 num = PG_GETARG_FLOAT4(0);
359  char *ascii;
360 
361  if (isnan(num))
362  PG_RETURN_CSTRING(pstrdup("NaN"));
363 
364  switch (is_infinite(num))
365  {
366  case 1:
367  ascii = pstrdup("Infinity");
368  break;
369  case -1:
370  ascii = pstrdup("-Infinity");
371  break;
372  default:
373  {
374  int ndig = FLT_DIG + extra_float_digits;
375 
376  if (ndig < 1)
377  ndig = 1;
378 
379  ascii = psprintf("%.*g", ndig, num);
380  }
381  }
382 
383  PG_RETURN_CSTRING(ascii);
384 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
Datum ascii(PG_FUNCTION_ARGS)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
int extra_float_digits
Definition: float.c:65
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:327
int is_infinite(double val)
Definition: float.c:196

◆ float4pl()

Datum float4pl ( PG_FUNCTION_ARGS  )

Definition at line 798 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

799 {
800  float4 arg1 = PG_GETARG_FLOAT4(0);
801  float4 arg2 = PG_GETARG_FLOAT4(1);
802  float4 result;
803 
804  result = arg1 + arg2;
805 
806  /*
807  * There isn't any way to check for underflow of addition/subtraction
808  * because numbers near the underflow value have already been rounded to
809  * the point where we can't detect that the two values were originally
810  * different, e.g. on x86, '1e-45'::float4 == '2e-45'::float4 ==
811  * 1.4013e-45.
812  */
813  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
814  PG_RETURN_FLOAT4(result);
815 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float4recv()

Datum float4recv ( PG_FUNCTION_ARGS  )

Definition at line 390 of file float.c.

References buf, PG_GETARG_POINTER, PG_RETURN_FLOAT4, and pq_getmsgfloat4().

391 {
393 
395 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
static char * buf
Definition: pg_test_fsync.c:67
float4 pq_getmsgfloat4(StringInfo msg)
Definition: pqformat.c:471

◆ float4send()

Datum float4send ( PG_FUNCTION_ARGS  )

Definition at line 401 of file float.c.

References buf, PG_GETARG_FLOAT4, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendfloat4().

402 {
403  float4 num = PG_GETARG_FLOAT4(0);
405 
406  pq_begintypsend(&buf);
407  pq_sendfloat4(&buf, num);
409 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendfloat4(StringInfo buf, float4 f)
Definition: pqformat.c:254
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ float4smaller()

Datum float4smaller ( PG_FUNCTION_ARGS  )

Definition at line 704 of file float.c.

References float4_cmp_internal(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

705 {
706  float4 arg1 = PG_GETARG_FLOAT4(0);
707  float4 arg2 = PG_GETARG_FLOAT4(1);
708  float4 result;
709 
710  if (float4_cmp_internal(arg1, arg2) < 0)
711  result = arg1;
712  else
713  result = arg2;
714  PG_RETURN_FLOAT4(result);
715 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
int float4_cmp_internal(float4 a, float4 b)
Definition: float.c:935
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ float4um()

Datum float4um ( PG_FUNCTION_ARGS  )

Definition at line 672 of file float.c.

References PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

673 {
674  float4 arg1 = PG_GETARG_FLOAT4(0);
675  float4 result;
676 
677  result = -arg1;
678  PG_RETURN_FLOAT4(result);
679 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ float4up()

Datum float4up ( PG_FUNCTION_ARGS  )

Definition at line 682 of file float.c.

References arg, PG_GETARG_FLOAT4, and PG_RETURN_FLOAT4.

683 {
685 
686  PG_RETURN_FLOAT4(arg);
687 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
void * arg

◆ float84div()

Datum float84div ( PG_FUNCTION_ARGS  )

Definition at line 3523 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3524 {
3525  float8 arg1 = PG_GETARG_FLOAT8(0);
3526  float4 arg2 = PG_GETARG_FLOAT4(1);
3527  float8 result;
3528 
3529  if (arg2 == 0.0)
3530  ereport(ERROR,
3531  (errcode(ERRCODE_DIVISION_BY_ZERO),
3532  errmsg("division by zero")));
3533 
3534  result = arg1 / arg2;
3535 
3536  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), arg1 == 0);
3537  PG_RETURN_FLOAT8(result);
3538 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ float84eq()

Datum float84eq ( PG_FUNCTION_ARGS  )

Definition at line 3607 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3608 {
3609  float8 arg1 = PG_GETARG_FLOAT8(0);
3610  float4 arg2 = PG_GETARG_FLOAT4(1);
3611 
3612  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);
3613 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float84ge()

Datum float84ge ( PG_FUNCTION_ARGS  )

Definition at line 3652 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3653 {
3654  float8 arg1 = PG_GETARG_FLOAT8(0);
3655  float4 arg2 = PG_GETARG_FLOAT4(1);
3656 
3657  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);
3658 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float84gt()

Datum float84gt ( PG_FUNCTION_ARGS  )

Definition at line 3643 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3644 {
3645  float8 arg1 = PG_GETARG_FLOAT8(0);
3646  float4 arg2 = PG_GETARG_FLOAT4(1);
3647 
3648  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);
3649 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float84le()

Datum float84le ( PG_FUNCTION_ARGS  )

Definition at line 3634 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3635 {
3636  float8 arg1 = PG_GETARG_FLOAT8(0);
3637  float4 arg2 = PG_GETARG_FLOAT4(1);
3638 
3639  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);
3640 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float84lt()

Datum float84lt ( PG_FUNCTION_ARGS  )

Definition at line 3625 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3626 {
3627  float8 arg1 = PG_GETARG_FLOAT8(0);
3628  float4 arg2 = PG_GETARG_FLOAT4(1);
3629 
3630  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);
3631 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float84mi()

Datum float84mi ( PG_FUNCTION_ARGS  )

Definition at line 3496 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3497 {
3498  float8 arg1 = PG_GETARG_FLOAT8(0);
3499  float4 arg2 = PG_GETARG_FLOAT4(1);
3500  float8 result;
3501 
3502  result = arg1 - arg2;
3503 
3504  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
3505  PG_RETURN_FLOAT8(result);
3506 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float84mul()

Datum float84mul ( PG_FUNCTION_ARGS  )

Definition at line 3509 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3510 {
3511  float8 arg1 = PG_GETARG_FLOAT8(0);
3512  float4 arg2 = PG_GETARG_FLOAT4(1);
3513  float8 result;
3514 
3515  result = arg1 * arg2;
3516 
3517  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2),
3518  arg1 == 0 || arg2 == 0);
3519  PG_RETURN_FLOAT8(result);
3520 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float84ne()

Datum float84ne ( PG_FUNCTION_ARGS  )

Definition at line 3616 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

3617 {
3618  float8 arg1 = PG_GETARG_FLOAT8(0);
3619  float4 arg2 = PG_GETARG_FLOAT4(1);
3620 
3621  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);
3622 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float84pl()

Datum float84pl ( PG_FUNCTION_ARGS  )

Definition at line 3483 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

3484 {
3485  float8 arg1 = PG_GETARG_FLOAT8(0);
3486  float4 arg2 = PG_GETARG_FLOAT4(1);
3487  float8 result;
3488 
3489  result = arg1 + arg2;
3490 
3491  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
3492  PG_RETURN_FLOAT8(result);
3493 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float8_accum()

Datum float8_accum ( PG_FUNCTION_ARGS  )

Definition at line 2684 of file float.c.

References AggCheckCallContext(), check_float8_array(), CHECKFLOATVAL, construct_array(), Float8GetDatumFast, isinf(), newval, PG_GETARG_ARRAYTYPE_P, PG_GETARG_FLOAT8, and PG_RETURN_ARRAYTYPE_P.

2685 {
2686  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2688  float8 *transvalues;
2689  float8 N,
2690  sumX,
2691  sumX2;
2692 
2693  transvalues = check_float8_array(transarray, "float8_accum", 3);
2694  N = transvalues[0];
2695  sumX = transvalues[1];
2696  sumX2 = transvalues[2];
2697 
2698  N += 1.0;
2699  sumX += newval;
2700  CHECKFLOATVAL(sumX, isinf(transvalues[1]) || isinf(newval), true);
2701  sumX2 += newval * newval;
2702  CHECKFLOATVAL(sumX2, isinf(transvalues[2]) || isinf(newval), true);
2703 
2704  /*
2705  * If we're invoked as an aggregate, we can cheat and modify our first
2706  * parameter in-place to reduce palloc overhead. Otherwise we construct a
2707  * new array with the updated transition data and return it.
2708  */
2709  if (AggCheckCallContext(fcinfo, NULL))
2710  {
2711  transvalues[0] = N;
2712  transvalues[1] = sumX;
2713  transvalues[2] = sumX2;
2714 
2715  PG_RETURN_ARRAYTYPE_P(transarray);
2716  }
2717  else
2718  {
2719  Datum transdatums[3];
2720  ArrayType *result;
2721 
2722  transdatums[0] = Float8GetDatumFast(N);
2723  transdatums[1] = Float8GetDatumFast(sumX);
2724  transdatums[2] = Float8GetDatumFast(sumX2);
2725 
2726  result = construct_array(transdatums, 3,
2727  FLOAT8OID,
2728  sizeof(float8), FLOAT8PASSBYVAL, 'd');
2729 
2730  PG_RETURN_ARRAYTYPE_P(result);
2731  }
2732 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:367
#define Float8GetDatumFast(X)
Definition: postgres.h:761
#define newval
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624

◆ float8_avg()

Datum float8_avg ( PG_FUNCTION_ARGS  )

Definition at line 2788 of file float.c.

References check_float8_array(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

2789 {
2790  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2791  float8 *transvalues;
2792  float8 N,
2793  sumX;
2794 
2795  transvalues = check_float8_array(transarray, "float8_avg", 3);
2796  N = transvalues[0];
2797  sumX = transvalues[1];
2798  /* ignore sumX2 */
2799 
2800  /* SQL defines AVG of no values to be NULL */
2801  if (N == 0.0)
2802  PG_RETURN_NULL();
2803 
2804  PG_RETURN_FLOAT8(sumX / N);
2805 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_cmp_internal()

int float8_cmp_internal ( float8  a,
float8  b 
)

Definition at line 1049 of file float.c.

Referenced by btfloat48cmp(), btfloat84cmp(), btfloat8cmp(), btfloat8fastcmp(), float48eq(), float48ge(), float48gt(), float48le(), float48lt(), float48ne(), float84eq(), float84ge(), float84gt(), float84le(), float84lt(), float84ne(), float8eq(), float8ge(), float8gt(), float8larger(), float8le(), float8lt(), float8ne(), float8smaller(), interval_cmp_lower(), and interval_cmp_upper().

1050 {
1051  /*
1052  * We consider all NANs to be equal and larger than any non-NAN. This is
1053  * somewhat arbitrary; the important thing is to have a consistent sort
1054  * order.
1055  */
1056  if (isnan(a))
1057  {
1058  if (isnan(b))
1059  return 0; /* NAN = NAN */
1060  else
1061  return 1; /* NAN > non-NAN */
1062  }
1063  else if (isnan(b))
1064  {
1065  return -1; /* non-NAN < NAN */
1066  }
1067  else
1068  {
1069  if (a > b)
1070  return 1;
1071  else if (a < b)
1072  return -1;
1073  else
1074  return 0;
1075  }
1076 }

◆ float8_combine()

Datum float8_combine ( PG_FUNCTION_ARGS  )

Definition at line 2648 of file float.c.

References AggCheckCallContext(), check_float8_array(), CHECKFLOATVAL, elog, ERROR, isinf(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_ARRAYTYPE_P.

2649 {
2650  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
2651  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
2652  float8 *transvalues1;
2653  float8 *transvalues2;
2654  float8 N,
2655  sumX,
2656  sumX2;
2657 
2658  if (!AggCheckCallContext(fcinfo, NULL))
2659  elog(ERROR, "aggregate function called in non-aggregate context");
2660 
2661  transvalues1 = check_float8_array(transarray1, "float8_combine", 3);
2662  N = transvalues1[0];
2663  sumX = transvalues1[1];
2664  sumX2 = transvalues1[2];
2665 
2666  transvalues2 = check_float8_array(transarray2, "float8_combine", 3);
2667 
2668  N += transvalues2[0];
2669  sumX += transvalues2[1];
2670  CHECKFLOATVAL(sumX, isinf(transvalues1[1]) || isinf(transvalues2[1]),
2671  true);
2672  sumX2 += transvalues2[2];
2673  CHECKFLOATVAL(sumX2, isinf(transvalues1[2]) || isinf(transvalues2[2]),
2674  true);
2675 
2676  transvalues1[0] = N;
2677  transvalues1[1] = sumX;
2678  transvalues1[2] = sumX2;
2679 
2680  PG_RETURN_ARRAYTYPE_P(transarray1);
2681 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define elog
Definition: elog.h:219

◆ float8_corr()

Datum float8_corr ( PG_FUNCTION_ARGS  )

Definition at line 3256 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3257 {
3258  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3259  float8 *transvalues;
3260  float8 N,
3261  sumX,
3262  sumX2,
3263  sumY,
3264  sumY2,
3265  sumXY,
3266  numeratorX,
3267  numeratorY,
3268  numeratorXY;
3269 
3270  transvalues = check_float8_array(transarray, "float8_corr", 6);
3271  N = transvalues[0];
3272  sumX = transvalues[1];
3273  sumX2 = transvalues[2];
3274  sumY = transvalues[3];
3275  sumY2 = transvalues[4];
3276  sumXY = transvalues[5];
3277 
3278  /* if N is 0 we should return NULL */
3279  if (N < 1.0)
3280  PG_RETURN_NULL();
3281 
3282  numeratorX = N * sumX2 - sumX * sumX;
3283  CHECKFLOATVAL(numeratorX, isinf(sumX2) || isinf(sumX), true);
3284  numeratorY = N * sumY2 - sumY * sumY;
3285  CHECKFLOATVAL(numeratorY, isinf(sumY2) || isinf(sumY), true);
3286  numeratorXY = N * sumXY - sumX * sumY;
3287  CHECKFLOATVAL(numeratorXY, isinf(sumXY) || isinf(sumX) ||
3288  isinf(sumY), true);
3289  if (numeratorX <= 0 || numeratorY <= 0)
3290  PG_RETURN_NULL();
3291 
3292  PG_RETURN_FLOAT8(numeratorXY / sqrt(numeratorX * numeratorY));
3293 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_covar_pop()

Datum float8_covar_pop ( PG_FUNCTION_ARGS  )

Definition at line 3200 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3201 {
3202  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3203  float8 *transvalues;
3204  float8 N,
3205  sumX,
3206  sumY,
3207  sumXY,
3208  numerator;
3209 
3210  transvalues = check_float8_array(transarray, "float8_covar_pop", 6);
3211  N = transvalues[0];
3212  sumX = transvalues[1];
3213  sumY = transvalues[3];
3214  sumXY = transvalues[5];
3215 
3216  /* if N is 0 we should return NULL */
3217  if (N < 1.0)
3218  PG_RETURN_NULL();
3219 
3220  numerator = N * sumXY - sumX * sumY;
3221  CHECKFLOATVAL(numerator, isinf(sumXY) || isinf(sumX) ||
3222  isinf(sumY), true);
3223 
3224  PG_RETURN_FLOAT8(numerator / (N * N));
3225 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_covar_samp()

Datum float8_covar_samp ( PG_FUNCTION_ARGS  )

Definition at line 3228 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3229 {
3230  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3231  float8 *transvalues;
3232  float8 N,
3233  sumX,
3234  sumY,
3235  sumXY,
3236  numerator;
3237 
3238  transvalues = check_float8_array(transarray, "float8_covar_samp", 6);
3239  N = transvalues[0];
3240  sumX = transvalues[1];
3241  sumY = transvalues[3];
3242  sumXY = transvalues[5];
3243 
3244  /* if N is <= 1 we should return NULL */
3245  if (N < 2.0)
3246  PG_RETURN_NULL();
3247 
3248  numerator = N * sumXY - sumX * sumY;
3249  CHECKFLOATVAL(numerator, isinf(sumXY) || isinf(sumX) ||
3250  isinf(sumY), true);
3251 
3252  PG_RETURN_FLOAT8(numerator / (N * (N - 1.0)));
3253 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_accum()

Datum float8_regr_accum ( PG_FUNCTION_ARGS  )

Definition at line 2940 of file float.c.

References AggCheckCallContext(), check_float8_array(), CHECKFLOATVAL, construct_array(), Float8GetDatumFast, isinf(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_FLOAT8, and PG_RETURN_ARRAYTYPE_P.

2941 {
2942  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2943  float8 newvalY = PG_GETARG_FLOAT8(1);
2944  float8 newvalX = PG_GETARG_FLOAT8(2);
2945  float8 *transvalues;
2946  float8 N,
2947  sumX,
2948  sumX2,
2949  sumY,
2950  sumY2,
2951  sumXY;
2952 
2953  transvalues = check_float8_array(transarray, "float8_regr_accum", 6);
2954  N = transvalues[0];
2955  sumX = transvalues[1];
2956  sumX2 = transvalues[2];
2957  sumY = transvalues[3];
2958  sumY2 = transvalues[4];
2959  sumXY = transvalues[5];
2960 
2961  N += 1.0;
2962  sumX += newvalX;
2963  CHECKFLOATVAL(sumX, isinf(transvalues[1]) || isinf(newvalX), true);
2964  sumX2 += newvalX * newvalX;
2965  CHECKFLOATVAL(sumX2, isinf(transvalues[2]) || isinf(newvalX), true);
2966  sumY += newvalY;
2967  CHECKFLOATVAL(sumY, isinf(transvalues[3]) || isinf(newvalY), true);
2968  sumY2 += newvalY * newvalY;
2969  CHECKFLOATVAL(sumY2, isinf(transvalues[4]) || isinf(newvalY), true);
2970  sumXY += newvalX * newvalY;
2971  CHECKFLOATVAL(sumXY, isinf(transvalues[5]) || isinf(newvalX) ||
2972  isinf(newvalY), true);
2973 
2974  /*
2975  * If we're invoked as an aggregate, we can cheat and modify our first
2976  * parameter in-place to reduce palloc overhead. Otherwise we construct a
2977  * new array with the updated transition data and return it.
2978  */
2979  if (AggCheckCallContext(fcinfo, NULL))
2980  {
2981  transvalues[0] = N;
2982  transvalues[1] = sumX;
2983  transvalues[2] = sumX2;
2984  transvalues[3] = sumY;
2985  transvalues[4] = sumY2;
2986  transvalues[5] = sumXY;
2987 
2988  PG_RETURN_ARRAYTYPE_P(transarray);
2989  }
2990  else
2991  {
2992  Datum transdatums[6];
2993  ArrayType *result;
2994 
2995  transdatums[0] = Float8GetDatumFast(N);
2996  transdatums[1] = Float8GetDatumFast(sumX);
2997  transdatums[2] = Float8GetDatumFast(sumX2);
2998  transdatums[3] = Float8GetDatumFast(sumY);
2999  transdatums[4] = Float8GetDatumFast(sumY2);
3000  transdatums[5] = Float8GetDatumFast(sumXY);
3001 
3002  result = construct_array(transdatums, 6,
3003  FLOAT8OID,
3004  sizeof(float8), FLOAT8PASSBYVAL, 'd');
3005 
3006  PG_RETURN_ARRAYTYPE_P(result);
3007  }
3008 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
uintptr_t Datum
Definition: postgres.h:367
#define Float8GetDatumFast(X)
Definition: postgres.h:761
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624

◆ float8_regr_avgx()

Datum float8_regr_avgx ( PG_FUNCTION_ARGS  )

Definition at line 3162 of file float.c.

References check_float8_array(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3163 {
3164  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3165  float8 *transvalues;
3166  float8 N,
3167  sumX;
3168 
3169  transvalues = check_float8_array(transarray, "float8_regr_avgx", 6);
3170  N = transvalues[0];
3171  sumX = transvalues[1];
3172 
3173  /* if N is 0 we should return NULL */
3174  if (N < 1.0)
3175  PG_RETURN_NULL();
3176 
3177  PG_RETURN_FLOAT8(sumX / N);
3178 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_avgy()

Datum float8_regr_avgy ( PG_FUNCTION_ARGS  )

Definition at line 3181 of file float.c.

References check_float8_array(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3182 {
3183  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3184  float8 *transvalues;
3185  float8 N,
3186  sumY;
3187 
3188  transvalues = check_float8_array(transarray, "float8_regr_avgy", 6);
3189  N = transvalues[0];
3190  sumY = transvalues[3];
3191 
3192  /* if N is 0 we should return NULL */
3193  if (N < 1.0)
3194  PG_RETURN_NULL();
3195 
3196  PG_RETURN_FLOAT8(sumY / N);
3197 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_combine()

Datum float8_regr_combine ( PG_FUNCTION_ARGS  )

Definition at line 3019 of file float.c.

References AggCheckCallContext(), check_float8_array(), CHECKFLOATVAL, elog, ERROR, isinf(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_ARRAYTYPE_P.

3020 {
3021  ArrayType *transarray1 = PG_GETARG_ARRAYTYPE_P(0);
3022  ArrayType *transarray2 = PG_GETARG_ARRAYTYPE_P(1);
3023  float8 *transvalues1;
3024  float8 *transvalues2;
3025  float8 N,
3026  sumX,
3027  sumX2,
3028  sumY,
3029  sumY2,
3030  sumXY;
3031 
3032  if (!AggCheckCallContext(fcinfo, NULL))
3033  elog(ERROR, "aggregate function called in non-aggregate context");
3034 
3035  transvalues1 = check_float8_array(transarray1, "float8_regr_combine", 6);
3036  N = transvalues1[0];
3037  sumX = transvalues1[1];
3038  sumX2 = transvalues1[2];
3039  sumY = transvalues1[3];
3040  sumY2 = transvalues1[4];
3041  sumXY = transvalues1[5];
3042 
3043  transvalues2 = check_float8_array(transarray2, "float8_regr_combine", 6);
3044 
3045  N += transvalues2[0];
3046  sumX += transvalues2[1];
3047  CHECKFLOATVAL(sumX, isinf(transvalues1[1]) || isinf(transvalues2[1]),
3048  true);
3049  sumX2 += transvalues2[2];
3050  CHECKFLOATVAL(sumX2, isinf(transvalues1[2]) || isinf(transvalues2[2]),
3051  true);
3052  sumY += transvalues2[3];
3053  CHECKFLOATVAL(sumY, isinf(transvalues1[3]) || isinf(transvalues2[3]),
3054  true);
3055  sumY2 += transvalues2[4];
3056  CHECKFLOATVAL(sumY2, isinf(transvalues1[4]) || isinf(transvalues2[4]),
3057  true);
3058  sumXY += transvalues2[5];
3059  CHECKFLOATVAL(sumXY, isinf(transvalues1[5]) || isinf(transvalues2[5]),
3060  true);
3061 
3062  transvalues1[0] = N;
3063  transvalues1[1] = sumX;
3064  transvalues1[2] = sumX2;
3065  transvalues1[3] = sumY;
3066  transvalues1[4] = sumY2;
3067  transvalues1[5] = sumXY;
3068 
3069  PG_RETURN_ARRAYTYPE_P(transarray1);
3070 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:250
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:3492
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define elog
Definition: elog.h:219

◆ float8_regr_intercept()

Datum float8_regr_intercept ( PG_FUNCTION_ARGS  )

Definition at line 3375 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3376 {
3377  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3378  float8 *transvalues;
3379  float8 N,
3380  sumX,
3381  sumX2,
3382  sumY,
3383  sumXY,
3384  numeratorX,
3385  numeratorXXY;
3386 
3387  transvalues = check_float8_array(transarray, "float8_regr_intercept", 6);
3388  N = transvalues[0];
3389  sumX = transvalues[1];
3390  sumX2 = transvalues[2];
3391  sumY = transvalues[3];
3392  sumXY = transvalues[5];
3393 
3394  /* if N is 0 we should return NULL */
3395  if (N < 1.0)
3396  PG_RETURN_NULL();
3397 
3398  numeratorX = N * sumX2 - sumX * sumX;
3399  CHECKFLOATVAL(numeratorX, isinf(sumX2) || isinf(sumX), true);
3400  numeratorXXY = sumY * sumX2 - sumX * sumXY;
3401  CHECKFLOATVAL(numeratorXXY, isinf(sumY) || isinf(sumX2) ||
3402  isinf(sumX) || isinf(sumXY), true);
3403  if (numeratorX <= 0)
3404  PG_RETURN_NULL();
3405 
3406  PG_RETURN_FLOAT8(numeratorXXY / numeratorX);
3407 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_r2()

Datum float8_regr_r2 ( PG_FUNCTION_ARGS  )

Definition at line 3296 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3297 {
3298  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3299  float8 *transvalues;
3300  float8 N,
3301  sumX,
3302  sumX2,
3303  sumY,
3304  sumY2,
3305  sumXY,
3306  numeratorX,
3307  numeratorY,
3308  numeratorXY;
3309 
3310  transvalues = check_float8_array(transarray, "float8_regr_r2", 6);
3311  N = transvalues[0];
3312  sumX = transvalues[1];
3313  sumX2 = transvalues[2];
3314  sumY = transvalues[3];
3315  sumY2 = transvalues[4];
3316  sumXY = transvalues[5];
3317 
3318  /* if N is 0 we should return NULL */
3319  if (N < 1.0)
3320  PG_RETURN_NULL();
3321 
3322  numeratorX = N * sumX2 - sumX * sumX;
3323  CHECKFLOATVAL(numeratorX, isinf(sumX2) || isinf(sumX), true);
3324  numeratorY = N * sumY2 - sumY * sumY;
3325  CHECKFLOATVAL(numeratorY, isinf(sumY2) || isinf(sumY), true);
3326  numeratorXY = N * sumXY - sumX * sumY;
3327  CHECKFLOATVAL(numeratorXY, isinf(sumXY) || isinf(sumX) ||
3328  isinf(sumY), true);
3329  if (numeratorX <= 0)
3330  PG_RETURN_NULL();
3331  /* per spec, horizontal line produces 1.0 */
3332  if (numeratorY <= 0)
3333  PG_RETURN_FLOAT8(1.0);
3334 
3335  PG_RETURN_FLOAT8((numeratorXY * numeratorXY) /
3336  (numeratorX * numeratorY));
3337 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_slope()

Datum float8_regr_slope ( PG_FUNCTION_ARGS  )

Definition at line 3340 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3341 {
3342  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3343  float8 *transvalues;
3344  float8 N,
3345  sumX,
3346  sumX2,
3347  sumY,
3348  sumXY,
3349  numeratorX,
3350  numeratorXY;
3351 
3352  transvalues = check_float8_array(transarray, "float8_regr_slope", 6);
3353  N = transvalues[0];
3354  sumX = transvalues[1];
3355  sumX2 = transvalues[2];
3356  sumY = transvalues[3];
3357  sumXY = transvalues[5];
3358 
3359  /* if N is 0 we should return NULL */
3360  if (N < 1.0)
3361  PG_RETURN_NULL();
3362 
3363  numeratorX = N * sumX2 - sumX * sumX;
3364  CHECKFLOATVAL(numeratorX, isinf(sumX2) || isinf(sumX), true);
3365  numeratorXY = N * sumXY - sumX * sumY;
3366  CHECKFLOATVAL(numeratorXY, isinf(sumXY) || isinf(sumX) ||
3367  isinf(sumY), true);
3368  if (numeratorX <= 0)
3369  PG_RETURN_NULL();
3370 
3371  PG_RETURN_FLOAT8(numeratorXY / numeratorX);
3372 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_sxx()

Datum float8_regr_sxx ( PG_FUNCTION_ARGS  )

Definition at line 3074 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3075 {
3076  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3077  float8 *transvalues;
3078  float8 N,
3079  sumX,
3080  sumX2,
3081  numerator;
3082 
3083  transvalues = check_float8_array(transarray, "float8_regr_sxx", 6);
3084  N = transvalues[0];
3085  sumX = transvalues[1];
3086  sumX2 = transvalues[2];
3087 
3088  /* if N is 0 we should return NULL */
3089  if (N < 1.0)
3090  PG_RETURN_NULL();
3091 
3092  numerator = N * sumX2 - sumX * sumX;
3093  CHECKFLOATVAL(numerator, isinf(sumX2) || isinf(sumX), true);
3094 
3095  /* Watch out for roundoff error producing a negative numerator */
3096  if (numerator <= 0.0)
3097  PG_RETURN_FLOAT8(0.0);
3098 
3099  PG_RETURN_FLOAT8(numerator / N);
3100 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_sxy()

Datum float8_regr_sxy ( PG_FUNCTION_ARGS  )

Definition at line 3132 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3133 {
3134  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3135  float8 *transvalues;
3136  float8 N,
3137  sumX,
3138  sumY,
3139  sumXY,
3140  numerator;
3141 
3142  transvalues = check_float8_array(transarray, "float8_regr_sxy", 6);
3143  N = transvalues[0];
3144  sumX = transvalues[1];
3145  sumY = transvalues[3];
3146  sumXY = transvalues[5];
3147 
3148  /* if N is 0 we should return NULL */
3149  if (N < 1.0)
3150  PG_RETURN_NULL();
3151 
3152  numerator = N * sumXY - sumX * sumY;
3153  CHECKFLOATVAL(numerator, isinf(sumXY) || isinf(sumX) ||
3154  isinf(sumY), true);
3155 
3156  /* A negative result is valid here */
3157 
3158  PG_RETURN_FLOAT8(numerator / N);
3159 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_regr_syy()

Datum float8_regr_syy ( PG_FUNCTION_ARGS  )

Definition at line 3103 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

3104 {
3105  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
3106  float8 *transvalues;
3107  float8 N,
3108  sumY,
3109  sumY2,
3110  numerator;
3111 
3112  transvalues = check_float8_array(transarray, "float8_regr_syy", 6);
3113  N = transvalues[0];
3114  sumY = transvalues[3];
3115  sumY2 = transvalues[4];
3116 
3117  /* if N is 0 we should return NULL */
3118  if (N < 1.0)
3119  PG_RETURN_NULL();
3120 
3121  numerator = N * sumY2 - sumY * sumY;
3122  CHECKFLOATVAL(numerator, isinf(sumY2) || isinf(sumY), true);
3123 
3124  /* Watch out for roundoff error producing a negative numerator */
3125  if (numerator <= 0.0)
3126  PG_RETURN_FLOAT8(0.0);
3127 
3128  PG_RETURN_FLOAT8(numerator / N);
3129 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_stddev_pop()

Datum float8_stddev_pop ( PG_FUNCTION_ARGS  )

Definition at line 2866 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

2867 {
2868  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2869  float8 *transvalues;
2870  float8 N,
2871  sumX,
2872  sumX2,
2873  numerator;
2874 
2875  transvalues = check_float8_array(transarray, "float8_stddev_pop", 3);
2876  N = transvalues[0];
2877  sumX = transvalues[1];
2878  sumX2 = transvalues[2];
2879 
2880  /* Population stddev is undefined when N is 0, so return NULL */
2881  if (N == 0.0)
2882  PG_RETURN_NULL();
2883 
2884  numerator = N * sumX2 - sumX * sumX;
2885  CHECKFLOATVAL(numerator, isinf(sumX2) || isinf(sumX), true);
2886 
2887  /* Watch out for roundoff error producing a negative numerator */
2888  if (numerator <= 0.0)
2889  PG_RETURN_FLOAT8(0.0);
2890 
2891  PG_RETURN_FLOAT8(sqrt(numerator / (N * N)));
2892 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_stddev_samp()

Datum float8_stddev_samp ( PG_FUNCTION_ARGS  )

Definition at line 2895 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

2896 {
2897  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2898  float8 *transvalues;
2899  float8 N,
2900  sumX,
2901  sumX2,
2902  numerator;
2903 
2904  transvalues = check_float8_array(transarray, "float8_stddev_samp", 3);
2905  N = transvalues[0];
2906  sumX = transvalues[1];
2907  sumX2 = transvalues[2];
2908 
2909  /* Sample stddev is undefined when N is 0 or 1, so return NULL */
2910  if (N <= 1.0)
2911  PG_RETURN_NULL();
2912 
2913  numerator = N * sumX2 - sumX * sumX;
2914  CHECKFLOATVAL(numerator, isinf(sumX2) || isinf(sumX), true);
2915 
2916  /* Watch out for roundoff error producing a negative numerator */
2917  if (numerator <= 0.0)
2918  PG_RETURN_FLOAT8(0.0);
2919 
2920  PG_RETURN_FLOAT8(sqrt(numerator / (N * (N - 1.0))));
2921 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_var_pop()

Datum float8_var_pop ( PG_FUNCTION_ARGS  )

Definition at line 2808 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

2809 {
2810  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2811  float8 *transvalues;
2812  float8 N,
2813  sumX,
2814  sumX2,
2815  numerator;
2816 
2817  transvalues = check_float8_array(transarray, "float8_var_pop", 3);
2818  N = transvalues[0];
2819  sumX = transvalues[1];
2820  sumX2 = transvalues[2];
2821 
2822  /* Population variance is undefined when N is 0, so return NULL */
2823  if (N == 0.0)
2824  PG_RETURN_NULL();
2825 
2826  numerator = N * sumX2 - sumX * sumX;
2827  CHECKFLOATVAL(numerator, isinf(sumX2) || isinf(sumX), true);
2828 
2829  /* Watch out for roundoff error producing a negative numerator */
2830  if (numerator <= 0.0)
2831  PG_RETURN_FLOAT8(0.0);
2832 
2833  PG_RETURN_FLOAT8(numerator / (N * N));
2834 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8_var_samp()

Datum float8_var_samp ( PG_FUNCTION_ARGS  )

Definition at line 2837 of file float.c.

References check_float8_array(), CHECKFLOATVAL, isinf(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_FLOAT8, and PG_RETURN_NULL.

2838 {
2839  ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2840  float8 *transvalues;
2841  float8 N,
2842  sumX,
2843  sumX2,
2844  numerator;
2845 
2846  transvalues = check_float8_array(transarray, "float8_var_samp", 3);
2847  N = transvalues[0];
2848  sumX = transvalues[1];
2849  sumX2 = transvalues[2];
2850 
2851  /* Sample variance is undefined when N is 0 or 1, so return NULL */
2852  if (N <= 1.0)
2853  PG_RETURN_NULL();
2854 
2855  numerator = N * sumX2 - sumX * sumX;
2856  CHECKFLOATVAL(numerator, isinf(sumX2) || isinf(sumX), true);
2857 
2858  /* Watch out for roundoff error producing a negative numerator */
2859  if (numerator <= 0.0)
2860  PG_RETURN_FLOAT8(0.0);
2861 
2862  PG_RETURN_FLOAT8(numerator / (N * (N - 1.0)));
2863 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
static float8 * check_float8_array(ArrayType *transarray, const char *caller, int n)
Definition: float.c:2624
#define PG_RETURN_NULL()
Definition: fmgr.h:310

◆ float8abs()

Datum float8abs ( PG_FUNCTION_ARGS  )

Definition at line 727 of file float.c.

References PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

728 {
729  float8 arg1 = PG_GETARG_FLOAT8(0);
730 
731  PG_RETURN_FLOAT8(fabs(arg1));
732 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458

◆ float8div()

Datum float8div ( PG_FUNCTION_ARGS  )

Definition at line 907 of file float.c.

References CHECKFLOATVAL, ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

908 {
909  float8 arg1 = PG_GETARG_FLOAT8(0);
910  float8 arg2 = PG_GETARG_FLOAT8(1);
911  float8 result;
912 
913  if (arg2 == 0.0)
914  ereport(ERROR,
915  (errcode(ERRCODE_DIVISION_BY_ZERO),
916  errmsg("division by zero")));
917 
918  result = arg1 / arg2;
919 
920  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), arg1 == 0);
921  PG_RETURN_FLOAT8(result);
922 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ float8eq()

Datum float8eq ( PG_FUNCTION_ARGS  )

Definition at line 1079 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

1080 {
1081  float8 arg1 = PG_GETARG_FLOAT8(0);
1082  float8 arg2 = PG_GETARG_FLOAT8(1);
1083 
1084  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);
1085 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float8ge()

Datum float8ge ( PG_FUNCTION_ARGS  )

Definition at line 1124 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

1125 {
1126  float8 arg1 = PG_GETARG_FLOAT8(0);
1127  float8 arg2 = PG_GETARG_FLOAT8(1);
1128 
1129  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);
1130 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float8gt()

Datum float8gt ( PG_FUNCTION_ARGS  )

Definition at line 1115 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

1116 {
1117  float8 arg1 = PG_GETARG_FLOAT8(0);
1118  float8 arg2 = PG_GETARG_FLOAT8(1);
1119 
1120  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);
1121 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float8in()

Datum float8in ( PG_FUNCTION_ARGS  )

Definition at line 415 of file float.c.

References float8in_internal(), PG_GETARG_CSTRING, and PG_RETURN_FLOAT8.

Referenced by numeric_float8().

416 {
417  char *num = PG_GETARG_CSTRING(0);
418 
419  PG_RETURN_FLOAT8(float8in_internal(num, NULL, "double precision", num));
420 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8in_internal(char *num, char **endptr_p, const char *type_name, const char *orig_string)
Definition: float.c:441
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:247

◆ float8in_internal()

double float8in_internal ( char *  num,
char **  endptr_p,
const char *  type_name,
const char *  orig_string 
)

Definition at line 441 of file float.c.

References ereport, errcode(), errmsg(), ERROR, get_float8_infinity(), get_float8_nan(), pg_strncasecmp(), pstrdup(), and val.

Referenced by float8in(), pair_decode(), and single_decode().

443 {
444  double val;
445  char *endptr;
446 
447  /* skip leading whitespace */
448  while (*num != '\0' && isspace((unsigned char) *num))
449  num++;
450 
451  /*
452  * Check for an empty-string input to begin with, to avoid the vagaries of
453  * strtod() on different platforms.
454  */
455  if (*num == '\0')
456  ereport(ERROR,
457  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
458  errmsg("invalid input syntax for type %s: \"%s\"",
459  type_name, orig_string)));
460 
461  errno = 0;
462  val = strtod(num, &endptr);
463 
464  /* did we not see anything that looks like a double? */
465  if (endptr == num || errno != 0)
466  {
467  int save_errno = errno;
468 
469  /*
470  * C99 requires that strtod() accept NaN, [+-]Infinity, and [+-]Inf,
471  * but not all platforms support all of these (and some accept them
472  * but set ERANGE anyway...) Therefore, we check for these inputs
473  * ourselves if strtod() fails.
474  *
475  * Note: C99 also requires hexadecimal input as well as some extended
476  * forms of NaN, but we consider these forms unportable and don't try
477  * to support them. You can use 'em if your strtod() takes 'em.
478  */
479  if (pg_strncasecmp(num, "NaN", 3) == 0)
480  {
481  val = get_float8_nan();
482  endptr = num + 3;
483  }
484  else if (pg_strncasecmp(num, "Infinity", 8) == 0)
485  {
486  val = get_float8_infinity();
487  endptr = num + 8;
488  }
489  else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
490  {
491  val = get_float8_infinity();
492  endptr = num + 9;
493  }
494  else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
495  {
496  val = -get_float8_infinity();
497  endptr = num + 9;
498  }
499  else if (pg_strncasecmp(num, "inf", 3) == 0)
500  {
501  val = get_float8_infinity();
502  endptr = num + 3;
503  }
504  else if (pg_strncasecmp(num, "+inf", 4) == 0)
505  {
506  val = get_float8_infinity();
507  endptr = num + 4;
508  }
509  else if (pg_strncasecmp(num, "-inf", 4) == 0)
510  {
511  val = -get_float8_infinity();
512  endptr = num + 4;
513  }
514  else if (save_errno == ERANGE)
515  {
516  /*
517  * Some platforms return ERANGE for denormalized numbers (those
518  * that are not zero, but are too close to zero to have full
519  * precision). We'd prefer not to throw error for that, so try to
520  * detect whether it's a "real" out-of-range condition by checking
521  * to see if the result is zero or huge.
522  *
523  * On error, we intentionally complain about double precision not
524  * the given type name, and we print only the part of the string
525  * that is the current number.
526  */
527  if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL)
528  {
529  char *errnumber = pstrdup(num);
530 
531  errnumber[endptr - num] = '\0';
532  ereport(ERROR,
533  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
534  errmsg("\"%s\" is out of range for type double precision",
535  errnumber)));
536  }
537  }
538  else
539  ereport(ERROR,
540  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
541  errmsg("invalid input syntax for type %s: \"%s\"",
542  type_name, orig_string)));
543  }
544 #ifdef HAVE_BUGGY_SOLARIS_STRTOD
545  else
546  {
547  /*
548  * Many versions of Solaris have a bug wherein strtod sets endptr to
549  * point one byte beyond the end of the string when given "inf" or
550  * "infinity".
551  */
552  if (endptr != num && endptr[-1] == '\0')
553  endptr--;
554  }
555 #endif /* HAVE_BUGGY_SOLARIS_STRTOD */
556 
557  /* skip trailing whitespace */
558  while (*endptr != '\0' && isspace((unsigned char) *endptr))
559  endptr++;
560 
561  /* report stopping point if wanted, else complain if not end of string */
562  if (endptr_p)
563  *endptr_p = endptr;
564  else if (*endptr != '\0')
565  ereport(ERROR,
566  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
567  errmsg("invalid input syntax for type %s: \"%s\"",
568  type_name, orig_string)));
569 
570  return val;
571 }
double get_float8_nan(void)
Definition: float.c:163
char * pstrdup(const char *in)
Definition: mcxt.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
double get_float8_infinity(void)
Definition: float.c:118
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689

◆ float8larger()

Datum float8larger ( PG_FUNCTION_ARGS  )

Definition at line 757 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

758 {
759  float8 arg1 = PG_GETARG_FLOAT8(0);
760  float8 arg2 = PG_GETARG_FLOAT8(1);
761  float8 result;
762 
763  if (float8_cmp_internal(arg1, arg2) > 0)
764  result = arg1;
765  else
766  result = arg2;
767  PG_RETURN_FLOAT8(result);
768 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458

◆ float8le()

Datum float8le ( PG_FUNCTION_ARGS  )

Definition at line 1106 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

1107 {
1108  float8 arg1 = PG_GETARG_FLOAT8(0);
1109  float8 arg2 = PG_GETARG_FLOAT8(1);
1110 
1111  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);
1112 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float8lt()

Datum float8lt ( PG_FUNCTION_ARGS  )

Definition at line 1097 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

1098 {
1099  float8 arg1 = PG_GETARG_FLOAT8(0);
1100  float8 arg2 = PG_GETARG_FLOAT8(1);
1101 
1102  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);
1103 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float8mi()

Datum float8mi ( PG_FUNCTION_ARGS  )

Definition at line 880 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

881 {
882  float8 arg1 = PG_GETARG_FLOAT8(0);
883  float8 arg2 = PG_GETARG_FLOAT8(1);
884  float8 result;
885 
886  result = arg1 - arg2;
887 
888  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
889  PG_RETURN_FLOAT8(result);
890 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float8mul()

Datum float8mul ( PG_FUNCTION_ARGS  )

Definition at line 893 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

894 {
895  float8 arg1 = PG_GETARG_FLOAT8(0);
896  float8 arg2 = PG_GETARG_FLOAT8(1);
897  float8 result;
898 
899  result = arg1 * arg2;
900 
901  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2),
902  arg1 == 0 || arg2 == 0);
903  PG_RETURN_FLOAT8(result);
904 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float8ne()

Datum float8ne ( PG_FUNCTION_ARGS  )

Definition at line 1088 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_BOOL.

1089 {
1090  float8 arg1 = PG_GETARG_FLOAT8(0);
1091  float8 arg2 = PG_GETARG_FLOAT8(1);
1092 
1093  PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);
1094 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324

◆ float8out()

Datum float8out ( PG_FUNCTION_ARGS  )

Definition at line 578 of file float.c.

References float8out_internal(), PG_GETARG_FLOAT8, and PG_RETURN_CSTRING.

579 {
580  float8 num = PG_GETARG_FLOAT8(0);
581 
583 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
char * float8out_internal(double num)
Definition: float.c:593
double float8
Definition: c.h:458
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:327

◆ float8out_internal()

char* float8out_internal ( double  num)

Definition at line 593 of file float.c.

References ascii(), extra_float_digits, is_infinite(), psprintf(), and pstrdup().

Referenced by cube_out(), float8out(), line_out(), pair_encode(), and single_encode().

594 {
595  char *ascii;
596 
597  if (isnan(num))
598  return pstrdup("NaN");
599 
600  switch (is_infinite(num))
601  {
602  case 1:
603  ascii = pstrdup("Infinity");
604  break;
605  case -1:
606  ascii = pstrdup("-Infinity");
607  break;
608  default:
609  {
610  int ndig = DBL_DIG + extra_float_digits;
611 
612  if (ndig < 1)
613  ndig = 1;
614 
615  ascii = psprintf("%.*g", ndig, num);
616  }
617  }
618 
619  return ascii;
620 }
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
Datum ascii(PG_FUNCTION_ARGS)
int extra_float_digits
Definition: float.c:65
int is_infinite(double val)
Definition: float.c:196

◆ float8pl()

Datum float8pl ( PG_FUNCTION_ARGS  )

Definition at line 867 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

868 {
869  float8 arg1 = PG_GETARG_FLOAT8(0);
870  float8 arg2 = PG_GETARG_FLOAT8(1);
871  float8 result;
872 
873  result = arg1 + arg2;
874 
875  CHECKFLOATVAL(result, isinf(arg1) || isinf(arg2), true);
876  PG_RETURN_FLOAT8(result);
877 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ float8recv()

Datum float8recv ( PG_FUNCTION_ARGS  )

Definition at line 626 of file float.c.

References buf, PG_GETARG_POINTER, PG_RETURN_FLOAT8, and pq_getmsgfloat8().

627 {
629 
631 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
static char * buf
Definition: pg_test_fsync.c:67
float8 pq_getmsgfloat8(StringInfo msg)
Definition: pqformat.c:490

◆ float8send()

Datum float8send ( PG_FUNCTION_ARGS  )

Definition at line 637 of file float.c.

References buf, PG_GETARG_FLOAT8, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendfloat8().

638 {
639  float8 num = PG_GETARG_FLOAT8(0);
641 
642  pq_begintypsend(&buf);
643  pq_sendfloat8(&buf, num);
645 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendfloat8(StringInfo buf, float8 f)
Definition: pqformat.c:278
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
double float8
Definition: c.h:458
static char * buf
Definition: pg_test_fsync.c:67

◆ float8smaller()

Datum float8smaller ( PG_FUNCTION_ARGS  )

Definition at line 771 of file float.c.

References float8_cmp_internal(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

772 {
773  float8 arg1 = PG_GETARG_FLOAT8(0);
774  float8 arg2 = PG_GETARG_FLOAT8(1);
775  float8 result;
776 
777  if (float8_cmp_internal(arg1, arg2) < 0)
778  result = arg1;
779  else
780  result = arg2;
781  PG_RETURN_FLOAT8(result);
782 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
int float8_cmp_internal(float8 a, float8 b)
Definition: float.c:1049
double float8
Definition: c.h:458

◆ float8um()

Datum float8um ( PG_FUNCTION_ARGS  )

Definition at line 739 of file float.c.

References PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

740 {
741  float8 arg1 = PG_GETARG_FLOAT8(0);
742  float8 result;
743 
744  result = -arg1;
745  PG_RETURN_FLOAT8(result);
746 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458

◆ float8up()

Datum float8up ( PG_FUNCTION_ARGS  )

Definition at line 749 of file float.c.

References arg, PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

750 {
752 
753  PG_RETURN_FLOAT8(arg);
754 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
void * arg

◆ ftod()

Datum ftod ( PG_FUNCTION_ARGS  )

Definition at line 1328 of file float.c.

References PG_GETARG_FLOAT4, and PG_RETURN_FLOAT8.

1329 {
1330  float4 num = PG_GETARG_FLOAT4(0);
1331 
1332  PG_RETURN_FLOAT8((float8) num);
1333 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
float float4
Definition: c.h:457

◆ ftoi2()

Datum ftoi2 ( PG_FUNCTION_ARGS  )

Definition at line 1432 of file float.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT4, PG_RETURN_INT16, and rint().

1433 {
1434  float4 num = PG_GETARG_FLOAT4(0);
1435 
1436  if (num < SHRT_MIN || num > SHRT_MAX || isnan(num))
1437  ereport(ERROR,
1438  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1439  errmsg("smallint out of range")));
1440 
1441  PG_RETURN_INT16((int16) rint(num));
1442 }
signed short int16
Definition: c.h:312
#define PG_RETURN_INT16(x)
Definition: fmgr.h:321
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
double rint(double x)
Definition: rint.c:22
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ ftoi4()

Datum ftoi4 ( PG_FUNCTION_ARGS  )

Definition at line 1415 of file float.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_FLOAT4, PG_RETURN_INT32, and rint().

1416 {
1417  float4 num = PG_GETARG_FLOAT4(0);
1418 
1419  if (num < INT_MIN || num > INT_MAX || isnan(num))
1420  ereport(ERROR,
1421  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1422  errmsg("integer out of range")));
1423 
1424  PG_RETURN_INT32((int32) rint(num));
1425 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
double rint(double x)
Definition: rint.c:22
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ get_float4_infinity()

float get_float4_infinity ( void  )

Definition at line 143 of file float.c.

Referenced by float4in(), gistpenalty(), leftmostvalue_float4(), and range_gist_penalty().

144 {
145 #ifdef INFINITY
146  /* C99 standard way */
147  return (float) INFINITY;
148 #else
149 #if (_MSC_VER >= 1800)
150 #pragma warning(default:4756)
151 #endif
152 
153  /*
154  * On some platforms, HUGE_VAL is an infinity, elsewhere it's just the
155  * largest normal double. We assume forcing an overflow will get us a
156  * true infinity.
157  */
158  return (float) (HUGE_VAL * HUGE_VAL);
159 #endif
160 }

◆ get_float4_nan()

float get_float4_nan ( void  )

Definition at line 176 of file float.c.

Referenced by float4in(), and numeric_float4().

177 {
178 #ifdef NAN
179  /* C99 standard way */
180  return (float) NAN;
181 #else
182  /* Assume we can get a NAN via zero divide */
183  return (float) (0.0 / 0.0);
184 #endif
185 }

◆ get_float8_infinity()

double get_float8_infinity ( void  )

Definition at line 118 of file float.c.

Referenced by compute_range_stats(), dexp(), dpow(), float8in_internal(), gbt_ts_dist(), get_distance(), gistindex_keytest(), initRectBox(), leftmostvalue_float8(), NonFiniteTimestampTzPart(), pg_hypot(), and size_box().

119 {
120 #ifdef INFINITY
121  /* C99 standard way */
122  return (double) INFINITY;
123 #else
124 
125  /*
126  * On some platforms, HUGE_VAL is an infinity, elsewhere it's just the
127  * largest normal double. We assume forcing an overflow will get us a
128  * true infinity.
129  */
130  return (double) (HUGE_VAL * HUGE_VAL);
131 #endif
132 }

◆ get_float8_nan()

double get_float8_nan ( void  )

Definition at line 163 of file float.c.

Referenced by dacos(), dacosd(), dasin(), dasind(), datan(), datan2(), datan2d(), datand(), dcos(), dcosd(), dcot(), dcotd(), dpow(), dsin(), dsind(), dtan(), dtand(), float8in_internal(), numeric_float8(), numeric_float8_no_overflow(), and pg_hypot().

164 {
165  /* (double) NAN doesn't work on some NetBSD/MIPS releases */
166 #if defined(NAN) && !(defined(__NetBSD__) && defined(__mips__))
167  /* C99 standard way */
168  return (double) NAN;
169 #else
170  /* Assume we can get a NAN via zero divide */
171  return (double) (0.0 / 0.0);
172 #endif
173 }

◆ i2tod()

Datum i2tod ( PG_FUNCTION_ARGS  )

Definition at line 1403 of file float.c.

References PG_GETARG_INT16, and PG_RETURN_FLOAT8.

1404 {
1405  int16 num = PG_GETARG_INT16(0);
1406 
1407  PG_RETURN_FLOAT8((float8) num);
1408 }
signed short int16
Definition: c.h:312
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
#define PG_GETARG_INT16(n)
Definition: fmgr.h:241

◆ i2tof()

Datum i2tof ( PG_FUNCTION_ARGS  )

Definition at line 1461 of file float.c.

References PG_GETARG_INT16, and PG_RETURN_FLOAT4.

1462 {
1463  int16 num = PG_GETARG_INT16(0);
1464 
1465  PG_RETURN_FLOAT4((float4) num);
1466 }
signed short int16
Definition: c.h:312
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
float float4
Definition: c.h:457
#define PG_GETARG_INT16(n)
Definition: fmgr.h:241

◆ i4tod()

Datum i4tod ( PG_FUNCTION_ARGS  )

Definition at line 1391 of file float.c.

References PG_GETARG_INT32, and PG_RETURN_FLOAT8.

1392 {
1393  int32 num = PG_GETARG_INT32(0);
1394 
1395  PG_RETURN_FLOAT8((float8) num);
1396 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
signed int int32
Definition: c.h:313
double float8
Definition: c.h:458

◆ i4tof()

Datum i4tof ( PG_FUNCTION_ARGS  )

Definition at line 1449 of file float.c.

References PG_GETARG_INT32, and PG_RETURN_FLOAT4.

1450 {
1451  int32 num = PG_GETARG_INT32(0);
1452 
1453  PG_RETURN_FLOAT4((float4) num);
1454 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:330
signed int int32
Definition: c.h:313
float float4
Definition: c.h:457

◆ in_range_float4_float8()

Datum in_range_float4_float8 ( PG_FUNCTION_ARGS  )

Definition at line 1255 of file float.c.

References ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_BOOL, PG_GETARG_FLOAT4, PG_GETARG_FLOAT8, PG_RETURN_BOOL, and val.

1256 {
1258  float4 base = PG_GETARG_FLOAT4(1);
1259  float8 offset = PG_GETARG_FLOAT8(2);
1260  bool sub = PG_GETARG_BOOL(3);
1261  bool less = PG_GETARG_BOOL(4);
1262  float8 sum;
1263 
1264  /*
1265  * Reject negative or NaN offset. Negative is per spec, and NaN is
1266  * because appropriate semantics for that seem non-obvious.
1267  */
1268  if (isnan(offset) || offset < 0)
1269  ereport(ERROR,
1270  (errcode(ERRCODE_INVALID_PRECEDING_FOLLOWING_SIZE),
1271  errmsg("invalid preceding or following size in window function")));
1272 
1273  /*
1274  * Deal with cases where val and/or base is NaN, following the rule that
1275  * NaN sorts after non-NaN (cf float8_cmp_internal). The offset cannot
1276  * affect the conclusion.
1277  */
1278  if (isnan(val))
1279  {
1280  if (isnan(base))
1281  PG_RETURN_BOOL(true); /* NAN = NAN */
1282  else
1283  PG_RETURN_BOOL(!less); /* NAN > non-NAN */
1284  }
1285  else if (isnan(base))
1286  {
1287  PG_RETURN_BOOL(less); /* non-NAN < NAN */
1288  }
1289 
1290  /*
1291  * Deal with infinite offset (necessarily +inf, at this point). We must
1292  * special-case this because if base happens to be -inf, their sum would
1293  * be NaN, which is an overflow-ish condition we should avoid.
1294  */
1295  if (isinf(offset))
1296  {
1297  PG_RETURN_BOOL(sub ? !less : less);
1298  }
1299 
1300  /*
1301  * Otherwise it should be safe to compute base +/- offset. We trust the
1302  * FPU to cope if base is +/-inf or the true sum would overflow, and
1303  * produce a suitably signed infinity, which will compare properly against
1304  * val whether or not that's infinity.
1305  */
1306  if (sub)
1307  sum = base - offset;
1308  else
1309  sum = base + offset;
1310 
1311  if (less)
1312  PG_RETURN_BOOL(val <= sum);
1313  else
1314  PG_RETURN_BOOL(val >= sum);
1315 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:250
#define ereport(elevel, rest)
Definition: elog.h:122
float float4
Definition: c.h:457
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689

◆ in_range_float8_float8()

Datum in_range_float8_float8 ( PG_FUNCTION_ARGS  )

Definition at line 1186 of file float.c.

References ereport, errcode(), errmsg(), ERROR, isinf(), PG_GETARG_BOOL, PG_GETARG_FLOAT8, PG_RETURN_BOOL, and val.

1187 {
1189  float8 base = PG_GETARG_FLOAT8(1);
1190  float8 offset = PG_GETARG_FLOAT8(2);
1191  bool sub = PG_GETARG_BOOL(3);
1192  bool less = PG_GETARG_BOOL(4);
1193  float8 sum;
1194 
1195  /*
1196  * Reject negative or NaN offset. Negative is per spec, and NaN is
1197  * because appropriate semantics for that seem non-obvious.
1198  */
1199  if (isnan(offset) || offset < 0)
1200  ereport(ERROR,
1201  (errcode(ERRCODE_INVALID_PRECEDING_FOLLOWING_SIZE),
1202  errmsg("invalid preceding or following size in window function")));
1203 
1204  /*
1205  * Deal with cases where val and/or base is NaN, following the rule that
1206  * NaN sorts after non-NaN (cf float8_cmp_internal). The offset cannot
1207  * affect the conclusion.
1208  */
1209  if (isnan(val))
1210  {
1211  if (isnan(base))
1212  PG_RETURN_BOOL(true); /* NAN = NAN */
1213  else
1214  PG_RETURN_BOOL(!less); /* NAN > non-NAN */
1215  }
1216  else if (isnan(base))
1217  {
1218  PG_RETURN_BOOL(less); /* non-NAN < NAN */
1219  }
1220 
1221  /*
1222  * Deal with infinite offset (necessarily +inf, at this point). We must
1223  * special-case this because if base happens to be -inf, their sum would
1224  * be NaN, which is an overflow-ish condition we should avoid.
1225  */
1226  if (isinf(offset))
1227  {
1228  PG_RETURN_BOOL(sub ? !less : less);
1229  }
1230 
1231  /*
1232  * Otherwise it should be safe to compute base +/- offset. We trust the
1233  * FPU to cope if base is +/-inf or the true sum would overflow, and
1234  * produce a suitably signed infinity, which will compare properly against
1235  * val whether or not that's infinity.
1236  */
1237  if (sub)
1238  sum = base - offset;
1239  else
1240  sum = base + offset;
1241 
1242  if (less)
1243  PG_RETURN_BOOL(val <= sum);
1244  else
1245  PG_RETURN_BOOL(val >= sum);
1246 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:244
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:324
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689

◆ init_degree_constants()

static void init_degree_constants ( void  )
static

Definition at line 2000 of file float.c.

References acos_0_5, asin_0_5, atan_1_0, cosd_q1(), cot_45, degree_c_forty_five, degree_c_one, degree_c_one_half, degree_c_sixty, degree_c_thirty, degree_consts_set, one_minus_cos_60, RADIANS_PER_DEGREE, sin_30, sind_q1(), and tan_45.

2001 {
2004  asin_0_5 = asin(degree_c_one_half);
2005  acos_0_5 = acos(degree_c_one_half);
2006  atan_1_0 = atan(degree_c_one);
2009  degree_consts_set = true;
2010 }
float8 degree_c_sixty
Definition: float.c:85
static float8 cot_45
Definition: float.c:75
float8 degree_c_one
Definition: float.c:87
float8 degree_c_one_half
Definition: float.c:86
static bool degree_consts_set
Definition: float.c:68
static double sind_q1(double x)
Definition: float.c:2252
static float8 acos_0_5
Definition: float.c:72
float8 degree_c_thirty
Definition: float.c:83
static float8 atan_1_0
Definition: float.c:73
#define RADIANS_PER_DEGREE
Definition: float.c:36
float8 degree_c_forty_five
Definition: float.c:84
static float8 sin_30
Definition: float.c:69
static float8 one_minus_cos_60
Definition: float.c:70
static double cosd_q1(double x)
Definition: float.c:2272
static float8 asin_0_5
Definition: float.c:71
static float8 tan_45
Definition: float.c:74

◆ is_infinite()

int is_infinite ( double  val)

Definition at line 196 of file float.c.

References isinf().

Referenced by calc_length_hist_frac(), float4_to_char(), float4out(), float8_to_char(), float8out_internal(), and get_len_position().

197 {
198  int inf = isinf(val);
199 
200  if (inf == 0)
201  return 0;
202  else if (val > 0)
203  return 1;
204  else
205  return -1;
206 }
int isinf(double x)
long val
Definition: informix.c:689

◆ radians()

Datum radians ( PG_FUNCTION_ARGS  )

Definition at line 2554 of file float.c.

References CHECKFLOATVAL, isinf(), PG_GETARG_FLOAT8, PG_RETURN_FLOAT8, and RADIANS_PER_DEGREE.

2555 {
2556  float8 arg1 = PG_GETARG_FLOAT8(0);
2557  float8 result;
2558 
2559  result = arg1 * RADIANS_PER_DEGREE;
2560 
2561  CHECKFLOATVAL(result, isinf(arg1), arg1 == 0);
2562  PG_RETURN_FLOAT8(result);
2563 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:331
double float8
Definition: c.h:458
int isinf(double x)
#define RADIANS_PER_DEGREE
Definition: float.c:36
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)
Definition: float.c:50

◆ setseed()

Datum setseed ( PG_FUNCTION_ARGS  )

Definition at line 2585 of file float.c.

References elog, ERROR, MAX_RANDOM_VALUE, PG_GETARG_FLOAT8, PG_RETURN_VOID, and srandom().

Referenced by assign_random_seed().

2586 {
2587  float8 seed = PG_GETARG_FLOAT8(0);
2588  int iseed;
2589 
2590  if (seed < -1 || seed > 1)
2591  elog(ERROR, "setseed parameter %f out of range [-1,1]", seed);
2592 
2593  iseed = (int) (seed * MAX_RANDOM_VALUE);
2594  srandom((unsigned int) iseed);
2595 
2596  PG_RETURN_VOID();
2597 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
#define MAX_RANDOM_VALUE
#define PG_RETURN_VOID()
Definition: fmgr.h:314
void srandom(unsigned int seed)
Definition: srandom.c:22
#define elog
Definition: elog.h:219

◆ sind_0_to_30()

static double sind_0_to_30 ( double  x)
static

Definition at line 2225 of file float.c.

References RADIANS_PER_DEGREE, and sin_30.

Referenced by cosd_q1(), and sind_q1().

2226 {
2227  volatile float8 sin_x = sin(x * RADIANS_PER_DEGREE);
2228 
2229  return (sin_x / sin_30) / 2.0;
2230 }
double float8
Definition: c.h:458
#define RADIANS_PER_DEGREE
Definition: float.c:36
static float8 sin_30
Definition: float.c:69

◆ sind_q1()

static double sind_q1 ( double  x)
static

Definition at line 2252 of file float.c.

References cosd_0_to_60(), and sind_0_to_30().

Referenced by dcotd(), dsind(), dtand(), and init_degree_constants().

2253 {
2254  /*
2255  * Stitch together the sine and cosine functions for the ranges [0, 30]
2256  * and (30, 90]. These guarantee to return exact answers at their
2257  * endpoints, so the overall result is a continuous monotonic function
2258  * that gives exact results when x = 0, 30 and 90 degrees.
2259  */
2260  if (x <= 30.0)
2261  return sind_0_to_30(x);
2262  else
2263  return cosd_0_to_60(90.0 - x);
2264 }
static double cosd_0_to_60(double x)
Definition: float.c:2239
static double sind_0_to_30(double x)
Definition: float.c:2225

◆ width_bucket_float8()

Datum width_bucket_float8 ( PG_FUNCTION_ARGS  )

Definition at line 3675 of file float.c.

References ereport, errcode(), errmsg(), ERROR, isinf(), pg_add_s32_overflow(), PG_GETARG_FLOAT8, PG_GETARG_INT32, and PG_RETURN_INT32.

3676 {
3677  float8 operand = PG_GETARG_FLOAT8(0);
3678  float8 bound1 = PG_GETARG_FLOAT8(1);
3679  float8 bound2 = PG_GETARG_FLOAT8(2);
3680  int32 count = PG_GETARG_INT32(3);
3681  int32 result;
3682 
3683  if (count <= 0.0)
3684  ereport(ERROR,
3685  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
3686  errmsg("count must be greater than zero")));
3687 
3688  if (isnan(operand) || isnan(bound1) || isnan(bound2))
3689  ereport(ERROR,
3690  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
3691  errmsg("operand, lower bound, and upper bound cannot be NaN")));
3692 
3693  /* Note that we allow "operand" to be infinite */
3694  if (isinf(bound1) || isinf(bound2))
3695  ereport(ERROR,
3696  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
3697  errmsg("lower and upper bounds must be finite")));
3698 
3699  if (bound1 < bound2)
3700  {
3701  if (operand < bound1)
3702  result = 0;
3703  else if (operand >= bound2)
3704  {
3705  if (pg_add_s32_overflow(count, 1, &result))
3706  ereport(ERROR,
3707  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3708  errmsg("integer out of range")));
3709  }
3710  else
3711  result = ((float8) count * (operand - bound1) / (bound2 - bound1)) + 1;
3712  }
3713  else if (bound1 > bound2)
3714  {
3715  if (operand > bound1)
3716  result = 0;
3717  else if (operand <= bound2)
3718  {
3719  if (pg_add_s32_overflow(count, 1, &result))
3720  ereport(ERROR,
3721  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3722  errmsg("integer out of range")));
3723  }
3724  else
3725  result = ((float8) count * (bound1 - operand) / (bound1 - bound2)) + 1;
3726  }
3727  else
3728  {
3729  ereport(ERROR,
3730  (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
3731  errmsg("lower bound cannot equal upper bound")));
3732  result = 0; /* keep the compiler quiet */
3733  }
3734 
3735  PG_RETURN_INT32(result);
3736 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:251
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define PG_RETURN_INT32(x)
Definition: fmgr.h:319
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:313
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:458
int isinf(double x)
#define ereport(elevel, rest)
Definition: elog.h:122
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:98
int errmsg(const char *fmt,...)
Definition: elog.c:797

Variable Documentation

◆ acos_0_5

float8 acos_0_5 = 0
static

Definition at line 72 of file float.c.

Referenced by acosd_q1(), asind_q1(), and init_degree_constants().

◆ asin_0_5

float8 asin_0_5 = 0
static

Definition at line 71 of file float.c.

Referenced by acosd_q1(), asind_q1(), and init_degree_constants().

◆ atan_1_0

float8 atan_1_0 = 0
static

Definition at line 73 of file float.c.

Referenced by datan2d(), datand(), and init_degree_constants().

◆ cot_45

float8 cot_45 = 0
static

Definition at line 75 of file float.c.

Referenced by dcotd(), and init_degree_constants().

◆ degree_c_forty_five

float8 degree_c_forty_five = 45.0

Definition at line 84 of file float.c.

Referenced by init_degree_constants().

◆ degree_c_one

float8 degree_c_one = 1.0

Definition at line 87 of file float.c.

Referenced by init_degree_constants().

◆ degree_c_one_half

float8 degree_c_one_half = 0.5

Definition at line 86 of file float.c.

Referenced by init_degree_constants().

◆ degree_c_sixty

float8 degree_c_sixty = 60.0

Definition at line 85 of file float.c.

Referenced by init_degree_constants().

◆ degree_c_thirty

float8 degree_c_thirty = 30.0