PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

pg_noreturn void float_overflow_error (void)
 
pg_noreturn void float_underflow_error (void)
 
pg_noreturn void float_zero_divide_error (void)
 
int is_infinite (float8 val)
 
float8 float8in_internal (char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext)
 
float4 float4in_internal (char *num, char **endptr_p, const char *type_name, const char *orig_string, struct Node *escontext)
 
charfloat8out_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 
)
extern

Definition at line 833 of file float.c.

834{
835 if (float4_gt(a, b))
836 return 1;
837 if (float4_lt(a, b))
838 return -1;
839 return 0;
840}
static bool float4_lt(const float4 val1, const float4 val2)
Definition float.h:241
static bool float4_gt(const float4 val1, const float4 val2)
Definition float.h:265
int b
Definition isn.c:74
int a
Definition isn.c:73

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

Referenced by btfloat4cmp(), btfloat4fastcmp(), and gbt_float4_ssup_cmp().

◆ float4_div()

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

Definition at line 177 of file float.h.

178{
179 float4 result;
180
181 if (unlikely(val2 == 0.0f) && !isnan(val1))
183 result = val1 / val2;
184 if (unlikely(isinf(result)) && !isinf(val1))
186 if (unlikely(result == 0.0f) && val1 != 0.0f && !isinf(val2))
188
189 return result;
190}
#define unlikely(x)
Definition c.h:432
float float4
Definition c.h:715
pg_noreturn void float_overflow_error(void)
Definition float.c:103
pg_noreturn void float_underflow_error(void)
Definition float.c:111
pg_noreturn void float_zero_divide_error(void)
Definition float.c:119
static int fb(int x)

References fb(), 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 217 of file float.h.

218{
219 return isnan(val1) ? isnan(val2) : !isnan(val2) && val1 == val2;
220}

References fb().

Referenced by float4eq().

◆ float4_ge()

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

Definition at line 277 of file float.h.

278{
279 return isnan(val1) || (!isnan(val2) && val1 >= val2);
280}

References fb().

Referenced by float4ge().

◆ float4_gt()

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

Definition at line 265 of file float.h.

266{
267 return !isnan(val2) && (isnan(val1) || val1 > val2);
268}

References fb().

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 253 of file float.h.

254{
255 return isnan(val2) || (!isnan(val1) && val1 <= val2);
256}

References fb().

Referenced by float4le().

◆ float4_lt()

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

Definition at line 241 of file float.h.

242{
243 return !isnan(val1) && (isnan(val2) || val1 < val2);
244}

References fb().

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 301 of file float.h.

302{
303 return float4_gt(val1, val2) ? val1 : val2;
304}

References fb(), and float4_gt().

◆ float4_mi()

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

Definition at line 125 of file float.h.

126{
127 float4 result;
128
129 result = val1 - val2;
130 if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
132
133 return result;
134}

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

Referenced by float4mi().

◆ float4_min()

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

Definition at line 289 of file float.h.

290{
291 return float4_lt(val1, val2) ? val1 : val2;
292}

References fb(), and float4_lt().

◆ float4_mul()

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

Definition at line 149 of file float.h.

150{
151 float4 result;
152
153 result = val1 * val2;
154 if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
156 if (unlikely(result == 0.0f) && val1 != 0.0f && val2 != 0.0f)
158
159 return result;
160}

References fb(), 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 229 of file float.h.

230{
231 return isnan(val1) ? !isnan(val2) : isnan(val2) || val1 != val2;
232}

References fb().

Referenced by float4ne().

◆ float4_pl()

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

Definition at line 101 of file float.h.

102{
103 float4 result;
104
105 result = val1 + val2;
106 if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
108
109 return result;
110}

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

Referenced by float4pl().

◆ float4in_internal()

float4 float4in_internal ( char num,
char **  endptr_p,
const char type_name,
const char orig_string,
struct Node escontext 
)
extern

Definition at line 200 of file float.c.

203{
204 float val;
205 char *endptr;
206
207 /*
208 * endptr points to the first character _after_ the sequence we recognized
209 * as a valid floating point number. orig_string points to the original
210 * input string.
211 */
212
213 /* skip leading whitespace */
214 while (*num != '\0' && isspace((unsigned char) *num))
215 num++;
216
217 /*
218 * Check for an empty-string input to begin with, to avoid the vagaries of
219 * strtod() on different platforms.
220 */
221 if (*num == '\0')
222 ereturn(escontext, 0,
224 errmsg("invalid input syntax for type %s: \"%s\"",
225 type_name, orig_string)));
226
227 errno = 0;
228 val = strtof(num, &endptr);
229
230 /* did we not see anything that looks like a double? */
231 if (endptr == num || errno != 0)
232 {
233 int save_errno = errno;
234
235 /*
236 * C99 requires that strtof() accept NaN, [+-]Infinity, and [+-]Inf,
237 * but not all platforms support all of these (and some accept them
238 * but set ERANGE anyway...) Therefore, we check for these inputs
239 * ourselves if strtof() fails.
240 *
241 * Note: C99 also requires hexadecimal input as well as some extended
242 * forms of NaN, but we consider these forms unportable and don't try
243 * to support them. You can use 'em if your strtof() takes 'em.
244 */
245 if (pg_strncasecmp(num, "NaN", 3) == 0)
246 {
248 endptr = num + 3;
249 }
250 else if (pg_strncasecmp(num, "Infinity", 8) == 0)
251 {
253 endptr = num + 8;
254 }
255 else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
256 {
258 endptr = num + 9;
259 }
260 else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
261 {
263 endptr = num + 9;
264 }
265 else if (pg_strncasecmp(num, "inf", 3) == 0)
266 {
268 endptr = num + 3;
269 }
270 else if (pg_strncasecmp(num, "+inf", 4) == 0)
271 {
273 endptr = num + 4;
274 }
275 else if (pg_strncasecmp(num, "-inf", 4) == 0)
276 {
278 endptr = num + 4;
279 }
280 else if (save_errno == ERANGE)
281 {
282 /*
283 * Some platforms return ERANGE for denormalized numbers (those
284 * that are not zero, but are too close to zero to have full
285 * precision). We'd prefer not to throw error for that, so try to
286 * detect whether it's a "real" out-of-range condition by checking
287 * to see if the result is zero or huge.
288 */
289 if (val == 0.0 ||
290#if !defined(HUGE_VALF)
291 isinf(val)
292#else
293 (val >= HUGE_VALF || val <= -HUGE_VALF)
294#endif
295 )
296 {
297 /* see comments in float8in_internal for rationale */
298 char *errnumber = pstrdup(num);
299
300 errnumber[endptr - num] = '\0';
301
302 ereturn(escontext, 0,
304 errmsg("\"%s\" is out of range for type real",
305 errnumber)));
306 }
307 }
308 else
309 ereturn(escontext, 0,
311 errmsg("invalid input syntax for type %s: \"%s\"",
312 type_name, orig_string)));
313 }
314
315 /* skip trailing whitespace */
316 while (*endptr != '\0' && isspace((unsigned char) *endptr))
317 endptr++;
318
319 /* report stopping point if wanted, else complain if not end of string */
320 if (endptr_p)
321 *endptr_p = endptr;
322 else if (*endptr != '\0')
323 ereturn(escontext, 0,
325 errmsg("invalid input syntax for type %s: \"%s\"",
326 type_name, orig_string)));
327
328 return val;
329}
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereturn(context, dummy_value,...)
Definition elog.h:278
static float4 get_float4_infinity(void)
Definition float.h:58
static float4 get_float4_nan(void)
Definition float.h:77
long val
Definition informix.c:689
char * pstrdup(const char *in)
Definition mcxt.c:1781
static char * errmsg
int pg_strncasecmp(const char *s1, const char *s2, size_t n)

References ereturn, errcode(), errmsg, fb(), get_float4_infinity(), get_float4_nan(), pg_strncasecmp(), pstrdup(), and val.

Referenced by float4in().

◆ float8_cmp_internal()

int float8_cmp_internal ( float8  a,
float8  b 
)
extern

Definition at line 927 of file float.c.

928{
929 if (float8_gt(a, b))
930 return 1;
931 if (float8_lt(a, b))
932 return -1;
933 return 0;
934}
static bool float8_lt(const float8 val1, const float8 val2)
Definition float.h:247
static bool float8_gt(const float8 val1, const float8 val2)
Definition float.h:271

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

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

◆ float8_div()

◆ float8_eq()

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

Definition at line 223 of file float.h.

224{
225 return isnan(val1) ? isnan(val2) : !isnan(val2) && val1 == val2;
226}

References fb().

Referenced by float48eq(), float84eq(), float8_regr_combine(), 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 283 of file float.h.

284{
285 return isnan(val1) || (!isnan(val2) && val1 >= val2);
286}

References fb().

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 271 of file float.h.

272{
273 return !isnan(val2) && (isnan(val1) || val1 > val2);
274}

References fb().

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 259 of file float.h.

260{
261 return isnan(val2) || (!isnan(val1) && val1 <= val2);
262}

References fb().

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

◆ float8_lt()

◆ float8_max()

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

Definition at line 307 of file float.h.

308{
309 return float8_gt(val1, val2) ? val1 : val2;
310}

References fb(), and 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 295 of file float.h.

296{
297 return float8_lt(val1, val2) ? val1 : val2;
298}

References fb(), and float8_lt().

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

◆ float8_mul()

◆ float8_ne()

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

Definition at line 235 of file float.h.

236{
237 return isnan(val1) ? !isnan(val2) : isnan(val2) || val1 != val2;
238}

References fb().

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,
struct Node escontext 
)
extern

Definition at line 412 of file float.c.

415{
416 double val;
417 char *endptr;
418
419 /* skip leading whitespace */
420 while (*num != '\0' && isspace((unsigned char) *num))
421 num++;
422
423 /*
424 * Check for an empty-string input to begin with, to avoid the vagaries of
425 * strtod() on different platforms.
426 */
427 if (*num == '\0')
428 ereturn(escontext, 0,
430 errmsg("invalid input syntax for type %s: \"%s\"",
431 type_name, orig_string)));
432
433 errno = 0;
434 val = strtod(num, &endptr);
435
436 /* did we not see anything that looks like a double? */
437 if (endptr == num || errno != 0)
438 {
439 int save_errno = errno;
440
441 /*
442 * C99 requires that strtod() accept NaN, [+-]Infinity, and [+-]Inf,
443 * but not all platforms support all of these (and some accept them
444 * but set ERANGE anyway...) Therefore, we check for these inputs
445 * ourselves if strtod() fails.
446 *
447 * Note: C99 also requires hexadecimal input as well as some extended
448 * forms of NaN, but we consider these forms unportable and don't try
449 * to support them. You can use 'em if your strtod() takes 'em.
450 */
451 if (pg_strncasecmp(num, "NaN", 3) == 0)
452 {
454 endptr = num + 3;
455 }
456 else if (pg_strncasecmp(num, "Infinity", 8) == 0)
457 {
459 endptr = num + 8;
460 }
461 else if (pg_strncasecmp(num, "+Infinity", 9) == 0)
462 {
464 endptr = num + 9;
465 }
466 else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
467 {
469 endptr = num + 9;
470 }
471 else if (pg_strncasecmp(num, "inf", 3) == 0)
472 {
474 endptr = num + 3;
475 }
476 else if (pg_strncasecmp(num, "+inf", 4) == 0)
477 {
479 endptr = num + 4;
480 }
481 else if (pg_strncasecmp(num, "-inf", 4) == 0)
482 {
484 endptr = num + 4;
485 }
486 else if (save_errno == ERANGE)
487 {
488 /*
489 * Some platforms return ERANGE for denormalized numbers (those
490 * that are not zero, but are too close to zero to have full
491 * precision). We'd prefer not to throw error for that, so try to
492 * detect whether it's a "real" out-of-range condition by checking
493 * to see if the result is zero or huge.
494 *
495 * On error, we intentionally complain about double precision not
496 * the given type name, and we print only the part of the string
497 * that is the current number.
498 */
499 if (val == 0.0 || val >= HUGE_VAL || val <= -HUGE_VAL)
500 {
501 char *errnumber = pstrdup(num);
502
503 errnumber[endptr - num] = '\0';
504 ereturn(escontext, 0,
506 errmsg("\"%s\" is out of range for type double precision",
507 errnumber)));
508 }
509 }
510 else
511 ereturn(escontext, 0,
513 errmsg("invalid input syntax for type %s: \"%s\"",
514 type_name, orig_string)));
515 }
516
517 /* skip trailing whitespace */
518 while (*endptr != '\0' && isspace((unsigned char) *endptr))
519 endptr++;
520
521 /* report stopping point if wanted, else complain if not end of string */
522 if (endptr_p)
523 *endptr_p = endptr;
524 else if (*endptr != '\0')
525 ereturn(escontext, 0,
527 errmsg("invalid input syntax for type %s: \"%s\"",
528 type_name, orig_string)));
529
530 return val;
531}
static float8 get_float8_infinity(void)
Definition float.h:65
static float8 get_float8_nan(void)
Definition float.h:84

References ereturn, errcode(), errmsg, fb(), get_float8_infinity(), get_float8_nan(), pg_strncasecmp(), pstrdup(), and val.

Referenced by dependencies_scalar(), executeItemOptUnwrapTarget(), float8in(), and single_decode().

◆ float8out_internal()

char * float8out_internal ( float8  num)
extern

Definition at line 554 of file float.c.

555{
556 char *ascii = (char *) palloc(32);
558
559 if (extra_float_digits > 0)
560 {
562 return ascii;
563 }
564
565 (void) pg_strfromd(ascii, 32, ndig, num);
566 return ascii;
567}
int double_to_shortest_decimal_buf(double f, char *result)
Definition d2s.c:1053
int extra_float_digits
Definition float.c:57
void * palloc(Size size)
Definition mcxt.c:1387
Datum ascii(PG_FUNCTION_ARGS)
int pg_strfromd(char *str, size_t count, int precision, double value)
Definition snprintf.c:1276

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

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

◆ float_overflow_error()

◆ float_underflow_error()

pg_noreturn void float_underflow_error ( void  )
extern

Definition at line 111 of file float.c.

112{
115 errmsg("value out of range: underflow")));
116}

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

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

◆ float_zero_divide_error()

pg_noreturn void float_zero_divide_error ( void  )
extern

Definition at line 119 of file float.c.

120{
123 errmsg("division by zero")));
124}

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

Referenced by float4_div(), and float8_div().

◆ get_float4_infinity()

static float4 get_float4_infinity ( void  )
inlinestatic

Definition at line 58 of file float.h.

59{
60 /* C99 standard way */
61 return (float4) INFINITY;
62}

References fb().

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

◆ get_float4_nan()

static float4 get_float4_nan ( void  )
inlinestatic

Definition at line 77 of file float.h.

78{
79 /* C99 standard way */
80 return (float4) NAN;
81}

References fb().

Referenced by float4in_internal(), and numeric_float4().

◆ get_float8_infinity()

◆ get_float8_nan()

◆ is_infinite()

int is_infinite ( float8  val)
extern

Definition at line 135 of file float.c.

136{
137 int inf = isinf(val);
138
139 if (inf == 0)
140 return 0;
141 else if (val > 0)
142 return 1;
143 else
144 return -1;
145}

References fb(), and val.

Variable Documentation

◆ extra_float_digits

PGDLLIMPORT int extra_float_digits
extern

Definition at line 57 of file float.c.

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