PostgreSQL Source Code  git master
float.h File Reference
#include <math.h>
Include dependency graph for float.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define M_PI   3.14159265358979323846
 
#define RADIANS_PER_DEGREE   0.0174532925199432957692
 

Functions

void float_overflow_error (void) pg_attribute_noreturn()
 
void float_underflow_error (void) pg_attribute_noreturn()
 
void float_zero_divide_error (void) pg_attribute_noreturn()
 
int is_infinite (float8 val)
 
float8 float8in_internal (char *num, char **endptr_p, const char *type_name, const char *orig_string)
 
float8 float8in_internal_opt_error (char *num, char **endptr_p, const char *type_name, const char *orig_string, bool *have_error)
 
char * float8out_internal (float8 num)
 
int float4_cmp_internal (float4 a, float4 b)
 
int float8_cmp_internal (float8 a, float8 b)
 
static float4 get_float4_infinity (void)
 
static float8 get_float8_infinity (void)
 
static float4 get_float4_nan (void)
 
static float8 get_float8_nan (void)
 
static float4 float4_pl (const float4 val1, const float4 val2)
 
static float8 float8_pl (const float8 val1, const float8 val2)
 
static float4 float4_mi (const float4 val1, const float4 val2)
 
static float8 float8_mi (const float8 val1, const float8 val2)
 
static float4 float4_mul (const float4 val1, const float4 val2)
 
static float8 float8_mul (const float8 val1, const float8 val2)
 
static float4 float4_div (const float4 val1, const float4 val2)
 
static float8 float8_div (const float8 val1, const float8 val2)
 
static bool float4_eq (const float4 val1, const float4 val2)
 
static bool float8_eq (const float8 val1, const float8 val2)
 
static bool float4_ne (const float4 val1, const float4 val2)
 
static bool float8_ne (const float8 val1, const float8 val2)
 
static bool float4_lt (const float4 val1, const float4 val2)
 
static bool float8_lt (const float8 val1, const float8 val2)
 
static bool float4_le (const float4 val1, const float4 val2)
 
static bool float8_le (const float8 val1, const float8 val2)
 
static bool float4_gt (const float4 val1, const float4 val2)
 
static bool float8_gt (const float8 val1, const float8 val2)
 
static bool float4_ge (const float4 val1, const float4 val2)
 
static bool float8_ge (const float8 val1, const float8 val2)
 
static float4 float4_min (const float4 val1, const float4 val2)
 
static float8 float8_min (const float8 val1, const float8 val2)
 
static float4 float4_max (const float4 val1, const float4 val2)
 
static float8 float8_max (const float8 val1, const float8 val2)
 

Variables

PGDLLIMPORT int extra_float_digits
 

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 22 of file float.h.

◆ RADIANS_PER_DEGREE

#define RADIANS_PER_DEGREE   0.0174532925199432957692

Definition at line 26 of file float.h.

Function Documentation

◆ float4_cmp_internal()

int float4_cmp_internal ( float4  a,
float4  b 
)

Definition at line 821 of file float.c.

822 {
823  if (float4_gt(a, b))
824  return 1;
825  if (float4_lt(a, b))
826  return -1;
827  return 0;
828 }
static bool float4_lt(const float4 val1, const float4 val2)
Definition: float.h:285
static bool float4_gt(const float4 val1, const float4 val2)
Definition: float.h:309
int b
Definition: isn.c:70
int a
Definition: isn.c:69

References a, b, float4_gt(), and float4_lt().

Referenced by btfloat4cmp(), and btfloat4fastcmp().

◆ float4_div()

static float4 float4_div ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 221 of file float.h.

222 {
223  float4 result;
224 
225  if (unlikely(val2 == 0.0f) && !isnan(val1))
227  result = val1 / val2;
228  if (unlikely(isinf(result)) && !isinf(val1))
230  if (unlikely(result == 0.0f) && val1 != 0.0f && !isinf(val2))
232 
233  return result;
234 }
#define unlikely(x)
Definition: c.h:273
float float4
Definition: c.h:564
void float_overflow_error(void) pg_attribute_noreturn()
Definition: float.c:85
void float_underflow_error(void) pg_attribute_noreturn()
Definition: float.c:93
void float_zero_divide_error(void) pg_attribute_noreturn()
Definition: float.c:101

References float_overflow_error(), float_underflow_error(), float_zero_divide_error(), and unlikely.

Referenced by float4div(), and g_box_consider_split().

◆ float4_eq()

static bool float4_eq ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 261 of file float.h.

262 {
263  return isnan(val1) ? isnan(val2) : !isnan(val2) && val1 == val2;
264 }

Referenced by float4eq().

◆ float4_ge()

static bool float4_ge ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 321 of file float.h.

322 {
323  return isnan(val1) || (!isnan(val2) && val1 >= val2);
324 }

Referenced by float4ge().

◆ float4_gt()

static bool float4_gt ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 309 of file float.h.

310 {
311  return !isnan(val2) && (isnan(val1) || val1 > val2);
312 }

Referenced by float4_cmp_internal(), float4_max(), float4gt(), and float4larger().

◆ float4_le()

static bool float4_le ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 297 of file float.h.

298 {
299  return isnan(val2) || (!isnan(val1) && val1 <= val2);
300 }

Referenced by float4le().

◆ float4_lt()

static bool float4_lt ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 285 of file float.h.

286 {
287  return !isnan(val1) && (isnan(val2) || val1 < val2);
288 }

Referenced by float4_cmp_internal(), float4_min(), float4lt(), and float4smaller().

◆ float4_max()

static float4 float4_max ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 345 of file float.h.

346 {
347  return float4_gt(val1, val2) ? val1 : val2;
348 }

References float4_gt().

◆ float4_mi()

static float4 float4_mi ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 169 of file float.h.

170 {
171  float4 result;
172 
173  result = val1 - val2;
174  if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
176 
177  return result;
178 }

References float_overflow_error(), and unlikely.

Referenced by float4mi().

◆ float4_min()

static float4 float4_min ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 333 of file float.h.

334 {
335  return float4_lt(val1, val2) ? val1 : val2;
336 }

References float4_lt().

◆ float4_mul()

static float4 float4_mul ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 193 of file float.h.

194 {
195  float4 result;
196 
197  result = val1 * val2;
198  if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
200  if (unlikely(result == 0.0f) && val1 != 0.0f && val2 != 0.0f)
202 
203  return result;
204 }

References float_overflow_error(), float_underflow_error(), and unlikely.

Referenced by float4mul().

◆ float4_ne()

static bool float4_ne ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 273 of file float.h.

274 {
275  return isnan(val1) ? !isnan(val2) : isnan(val2) || val1 != val2;
276 }

Referenced by float4ne().

◆ float4_pl()

static float4 float4_pl ( const float4  val1,
const float4  val2 
)
inlinestatic

Definition at line 145 of file float.h.

146 {
147  float4 result;
148 
149  result = val1 + val2;
150  if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
152 
153  return result;
154 }

References float_overflow_error(), and unlikely.

Referenced by float4pl().

◆ float8_cmp_internal()

int float8_cmp_internal ( float8  a,
float8  b 
)

Definition at line 915 of file float.c.

916 {
917  if (float8_gt(a, b))
918  return 1;
919  if (float8_lt(a, b))
920  return -1;
921  return 0;
922 }
static bool float8_lt(const float8 val1, const float8 val2)
Definition: float.h:291
static bool float8_gt(const float8 val1, const float8 val2)
Definition: float.h:315

References a, b, float8_gt(), and float8_lt().

Referenced by btfloat48cmp(), btfloat84cmp(), btfloat8cmp(), btfloat8fastcmp(), common_entry_cmp(), interval_cmp_lower(), interval_cmp_upper(), and pairingheap_GISTSearchItem_cmp().

◆ float8_div()

static float8 float8_div ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 237 of file float.h.

238 {
239  float8 result;
240 
241  if (unlikely(val2 == 0.0) && !isnan(val1))
243  result = val1 / val2;
244  if (unlikely(isinf(result)) && !isinf(val1))
246  if (unlikely(result == 0.0) && val1 != 0.0 && !isinf(val2))
248 
249  return result;
250 }
double float8
Definition: c.h:565

References float_overflow_error(), float_underflow_error(), float_zero_divide_error(), and unlikely.

Referenced by box_circle(), box_cn(), circle_box(), circle_div_pt(), circle_poly(), degrees(), float48div(), float84div(), float8div(), g_box_consider_split(), line_distance(), line_eq(), line_interpt_line(), line_invsl(), line_perp(), line_sl(), lseg_center(), lseg_inside_poly(), path_area(), point_div_point(), point_invsl(), point_sl(), and poly_to_circle().

◆ float8_eq()

static bool float8_eq ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 267 of file float.h.

268 {
269  return isnan(val1) ? isnan(val2) : !isnan(val2) && val1 == val2;
270 }

Referenced by float48eq(), float84eq(), float8eq(), gist_box_picksplit(), gist_box_same(), line_eq(), and point_eq_point().

◆ float8_ge()

static bool float8_ge ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 327 of file float.h.

328 {
329  return isnan(val1) || (!isnan(val2) && val1 >= val2);
330 }

Referenced by float48ge(), float84ge(), float8ge(), and gist_box_picksplit().

◆ float8_gt()

static bool float8_gt ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 315 of file float.h.

316 {
317  return !isnan(val2) && (isnan(val1) || val1 > val2);
318 }

Referenced by adjustBox(), box_construct(), float48gt(), float84gt(), float8_cmp_internal(), float8_max(), float8gt(), float8larger(), gist_box_picksplit(), and make_bound_box().

◆ float8_le()

static bool float8_le ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 303 of file float.h.

304 {
305  return isnan(val2) || (!isnan(val1) && val1 <= val2);
306 }

Referenced by float48le(), float84le(), float8le(), gist_box_picksplit(), and size_box().

◆ float8_lt()

static bool float8_lt ( const float8  val1,
const float8  val2 
)
inlinestatic

◆ float8_max()

static float8 float8_max ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 351 of file float.h.

352 {
353  return float8_gt(val1, val2) ? val1 : val2;
354 }

References float8_gt().

Referenced by box_interpt_lseg(), box_intersect(), boxes_bound_box(), path_inter(), and rt_box_union().

◆ float8_mi()

◆ float8_min()

static float8 float8_min ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 339 of file float.h.

340 {
341  return float8_lt(val1, val2) ? val1 : val2;
342 }

References float8_lt().

Referenced by box_interpt_lseg(), box_intersect(), boxes_bound_box(), path_inter(), and rt_box_union().

◆ float8_mul()

static float8 float8_mul ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 207 of file float.h.

208 {
209  float8 result;
210 
211  result = val1 * val2;
212  if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
214  if (unlikely(result == 0.0) && val1 != 0.0 && val2 != 0.0)
216 
217  return result;
218 }

References float_overflow_error(), float_underflow_error(), and unlikely.

Referenced by box_ar(), circle_ar(), circle_diameter(), circle_mul_pt(), circle_poly(), float48mul(), float84mul(), float8mul(), line_construct(), line_contain_point(), line_distance(), line_eq(), line_interpt_line(), line_perp(), lseg_crossing(), path_area(), point_div_point(), point_mul_point(), radians(), and size_box().

◆ float8_ne()

static bool float8_ne ( const float8  val1,
const float8  val2 
)
inlinestatic

Definition at line 279 of file float.h.

280 {
281  return isnan(val1) ? !isnan(val2) : isnan(val2) || val1 != val2;
282 }

Referenced by float48ne(), float84ne(), and float8ne().

◆ float8_pl()

◆ float8in_internal()

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

Definition at line 514 of file float.c.

516 {
517  return float8in_internal_opt_error(num, endptr_p, type_name,
518  orig_string, NULL);
519 }
double float8in_internal_opt_error(char *num, char **endptr_p, const char *type_name, const char *orig_string, bool *have_error)
Definition: float.c:380

References float8in_internal_opt_error().

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

◆ float8in_internal_opt_error()

float8 float8in_internal_opt_error ( char *  num,
char **  endptr_p,
const char *  type_name,
const char *  orig_string,
bool have_error 
)

Definition at line 380 of file float.c.

383 {
384  double val;
385  char *endptr;
386 
387  if (have_error)
388  *have_error = false;
389 
390  /* skip leading whitespace */
391  while (*num != '\0' && isspace((unsigned char) *num))
392  num++;
393 
394  /*
395  * Check for an empty-string input to begin with, to avoid the vagaries of
396  * strtod() on different platforms.
397  */
398  if (*num == '\0')
400  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
401  errmsg("invalid input syntax for type %s: \"%s\"",
402  type_name, orig_string))),
403  have_error);
404 
405  errno = 0;
406  val = strtod(num, &endptr);
407 
408  /* did we not see anything that looks like a double? */
409  if (endptr == num || errno != 0)
410  {
411  int save_errno = errno;
412 
413  /*
414  * C99 requires that strtod() accept NaN, [+-]Infinity, and [+-]Inf,
415  * but not all platforms support all of these (and some accept them
416  * but set ERANGE anyway...) Therefore, we check for these inputs
417  * ourselves if strtod() fails.
418  *
419  * Note: C99 also requires hexadecimal input as well as some extended
420  * forms of NaN, but we consider these forms unportable and don't try
421  * to support them. You can use 'em if your strtod() takes 'em.
422  */
423  if (pg_strncasecmp(num, "NaN", 3) == 0)
424  {
425  val = get_float8_nan();
426  endptr = num + 3;
427  }
428  else if (pg_strncasecmp(num, "Infinity", 8) == 0)
429  {
431  endptr = num + 8;
432  }
433  else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
434  {
436  endptr = num + 9;
437  }
438  else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
439  {
441  endptr = num + 9;
442  }
443  else if (pg_strncasecmp(num, "inf", 3) == 0)
444  {
446  endptr = num + 3;
447  }
448  else if (pg_strncasecmp(num, "+inf", 4) == 0)
449  {
451  endptr = num + 4;
452  }
453  else if (pg_strncasecmp(num, "-inf", 4) == 0)
454  {
456  endptr = num + 4;
457  }
458  else if (save_errno == ERANGE)
459  {
460  /*
461  * Some platforms return ERANGE for denormalized numbers (those
462  * that are not zero, but are too close to zero to have full
463  * precision). We'd prefer not to throw error for that, so try to
464  * detect whether it's a "real" out-of-range condition by checking
465  * to see if the result is zero or huge.
466  *
467  * On error, we intentionally complain about double precision not
468  * the given type name, and we print only the part of the string
469  * that is the current number.
470  */
471  if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL)
472  {
473  char *errnumber = pstrdup(num);
474 
475  errnumber[endptr - num] = '\0';
477  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
478  errmsg("\"%s\" is out of range for type double precision",
479  errnumber))),
480  have_error);
481  }
482  }
483  else
485  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
486  errmsg("invalid input syntax for type "
487  "%s: \"%s\"",
488  type_name, orig_string))),
489  have_error);
490  }
491 
492  /* skip trailing whitespace */
493  while (*endptr != '\0' && isspace((unsigned char) *endptr))
494  endptr++;
495 
496  /* report stopping point if wanted, else complain if not end of string */
497  if (endptr_p)
498  *endptr_p = endptr;
499  else if (*endptr != '\0')
501  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
502  errmsg("invalid input syntax for type "
503  "%s: \"%s\"",
504  type_name, orig_string))),
505  have_error);
506 
507  return val;
508 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define RETURN_ERROR(throw_error, have_error)
Definition: float.c:348
static float8 get_float8_infinity(void)
Definition: float.h:93
static float8 get_float8_nan(void)
Definition: float.h:122
long val
Definition: informix.c:664
char * pstrdup(const char *in)
Definition: mcxt.c:1305
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69

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

Referenced by executeItemOptUnwrapTarget(), and float8in_internal().

◆ float8out_internal()

char* float8out_internal ( float8  num)

Definition at line 542 of file float.c.

543 {
544  char *ascii = (char *) palloc(32);
545  int ndig = DBL_DIG + extra_float_digits;
546 
547  if (extra_float_digits > 0)
548  {
550  return ascii;
551  }
552 
553  (void) pg_strfromd(ascii, 32, ndig, num);
554  return ascii;
555 }
int double_to_shortest_decimal_buf(double f, char *result)
Definition: d2s.c:1053
int extra_float_digits
Definition: float.c:43
void * palloc(Size size)
Definition: mcxt.c:1068
Datum ascii(PG_FUNCTION_ARGS)
int pg_strfromd(char *str, size_t count, int precision, double value)
Definition: snprintf.c:1289

References ascii(), double_to_shortest_decimal_buf(), extra_float_digits, palloc(), and pg_strfromd().

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

◆ float_overflow_error()

◆ float_underflow_error()

void float_underflow_error ( void  )

Definition at line 93 of file float.c.

94 {
95  ereport(ERROR,
96  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
97  errmsg("value out of range: underflow")));
98 }

References ereport, errcode(), errmsg(), and ERROR.

Referenced by dcbrt(), dcosh(), dexp(), dlog1(), dlog10(), dpow(), dsqrt(), dtof(), float4_div(), float4_mul(), float8_div(), float8_mul(), and pg_hypot().

◆ float_zero_divide_error()

void float_zero_divide_error ( void  )

Definition at line 101 of file float.c.

102 {
103  ereport(ERROR,
104  (errcode(ERRCODE_DIVISION_BY_ZERO),
105  errmsg("division by zero")));
106 }

References ereport, errcode(), errmsg(), and ERROR.

Referenced by float4_div(), and float8_div().

◆ get_float4_infinity()

static float4 get_float4_infinity ( void  )
inlinestatic

Definition at line 73 of file float.h.

74 {
75 #ifdef INFINITY
76  /* C99 standard way */
77  return (float4) INFINITY;
78 #else
79 #ifdef _MSC_VER
80 #pragma warning(default:4756)
81 #endif
82 
83  /*
84  * On some platforms, HUGE_VAL is an infinity, elsewhere it's just the
85  * largest normal float8. We assume forcing an overflow will get us a
86  * true infinity.
87  */
88  return (float4) (HUGE_VAL * HUGE_VAL);
89 #endif
90 }

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

◆ get_float4_nan()

static float4 get_float4_nan ( void  )
inlinestatic

Definition at line 110 of file float.h.

111 {
112 #ifdef NAN
113  /* C99 standard way */
114  return (float4) NAN;
115 #else
116  /* Assume we can get a NAN via zero divide */
117  return (float4) (0.0 / 0.0);
118 #endif
119 }

Referenced by float4in(), and numeric_float4().

◆ get_float8_infinity()

static float8 get_float8_infinity ( void  )
inlinestatic

Definition at line 93 of file float.h.

94 {
95 #ifdef INFINITY
96  /* C99 standard way */
97  return (float8) INFINITY;
98 #else
99 
100  /*
101  * On some platforms, HUGE_VAL is an infinity, elsewhere it's just the
102  * largest normal float8. We assume forcing an overflow will get us a
103  * true infinity.
104  */
105  return (float8) (HUGE_VAL * HUGE_VAL);
106 #endif
107 }

Referenced by brin_minmax_multi_distance_float4(), brin_minmax_multi_distance_float8(), compute_range_stats(), datanh(), dcosh(), dsinh(), float8in_internal_opt_error(), gbt_ts_dist(), get_distance(), gistindex_keytest(), initRectBox(), leftmostvalue_float8(), line_invsl(), line_sl(), NonFiniteTimestampTzPart(), numeric_float8(), pg_hypot(), point_invsl(), point_sl(), size_box(), spg_kd_inner_consistent(), spg_quad_inner_consistent(), and spgbeginscan().

◆ get_float8_nan()

static float8 get_float8_nan ( void  )
inlinestatic

Definition at line 122 of file float.h.

123 {
124  /* (float8) NAN doesn't work on some NetBSD/MIPS releases */
125 #if defined(NAN) && !(defined(__NetBSD__) && defined(__mips__))
126  /* C99 standard way */
127  return (float8) NAN;
128 #else
129  /* Assume we can get a NaN via zero divide */
130  return (float8) (0.0 / 0.0);
131 #endif
132 }

Referenced by dacos(), dacosd(), dasin(), dasind(), datan(), datan2(), datan2d(), datand(), dcos(), dcosd(), dcot(), dcotd(), dpow(), dsin(), dsind(), dtan(), dtand(), float4_accum(), float8_accum(), float8_regr_accum(), float8in_internal_opt_error(), hashfloat4(), hashfloat4extended(), hashfloat8(), hashfloat8extended(), line_closept_point(), numeric_float8(), numeric_float8_no_overflow(), pg_hypot(), and point_box_distance().

◆ is_infinite()

int is_infinite ( float8  val)

Definition at line 117 of file float.c.

118 {
119  int inf = isinf(val);
120 
121  if (inf == 0)
122  return 0;
123  else if (val > 0)
124  return 1;
125  else
126  return -1;
127 }

References val.

Variable Documentation

◆ extra_float_digits

PGDLLIMPORT int extra_float_digits
extern

Definition at line 43 of file float.c.

Referenced by float4out(), float8out_internal(), and set_transmission_modes().