float.c 45.1 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * float.c
4
 *	  Functions for the built-in floating-point types.
5
 *
P
 
PostgreSQL Daemon 已提交
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.120 2006/02/03 12:45:47 momjian Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15
/*----------
16
 * OLD COMMENTS
17
 *		Basic float4 ops:
18 19
 *		 float4in, float4out, float4recv, float4send
 *		 float4abs, float4um, float4up
20
 *		Basic float8 ops:
21 22
 *		 float8in, float8out, float8recv, float8send
 *		 float8abs, float8um, float8up
23 24 25 26
 *		Arithmetic operators:
 *		 float4pl, float4mi, float4mul, float4div
 *		 float8pl, float8mi, float8mul, float8div
 *		Comparison operators:
27 28
 *		 float4eq, float4ne, float4lt, float4le, float4gt, float4ge, float4cmp
 *		 float8eq, float8ne, float8lt, float8le, float8gt, float8ge, float8cmp
29 30
 *		Conversion routines:
 *		 ftod, dtof, i4tod, dtoi4, i2tod, dtoi2, itof, ftoi, i2tof, ftoi2
31
 *
32 33 34 35 36 37 38 39
 *		Random float8 ops:
 *		 dround, dtrunc, dsqrt, dcbrt, dpow, dexp, dlog1
 *		Arithmetic operators:
 *		 float48pl, float48mi, float48mul, float48div
 *		 float84pl, float84mi, float84mul, float84div
 *		Comparison operators:
 *		 float48eq, float48ne, float48lt, float48le, float48gt, float48ge
 *		 float84eq, float84ne, float84lt, float84le, float84gt, float84ge
40
 *
41
 *		(You can do the arithmetic and comparison stuff using conversion
42 43
 *		 routines, but then you pay the overhead of invoking a separate
 *		 conversion function...)
44 45
 *
 * XXX GLUESOME STUFF. FIX IT! -AY '94
46
 *
47 48 49 50
 *		Added some additional conversion routines and cleaned up
 *		 a bit of the existing code. Need to change the error checking
 *		 for calls to pow(), exp() since on some machines (my Linux box
 *		 included) these routines do not set errno. - tgl 97/05/10
51
 *----------
52
 */
53 54
#include "postgres.h"

55 56
#include <ctype.h>
#include <errno.h>
57
#include <float.h>
58
#include <math.h>
59
#include <limits.h>
60 61
/* for finite() on Solaris */
#ifdef HAVE_IEEEFP_H
B
Bruce Momjian 已提交
62
#include <ieeefp.h>
63 64
#endif

65
#include "catalog/pg_type.h"
66
#include "fmgr.h"
67
#include "libpq/pqformat.h"
68
#include "utils/array.h"
B
Bruce Momjian 已提交
69
#include "utils/builtins.h"
70 71


72 73 74 75 76 77 78 79 80 81 82 83
#ifndef M_PI
/* from my RH5.2 gcc math.h file - thomas 2000-04-03 */
#define M_PI 3.14159265358979323846
#endif

#ifndef SHRT_MAX
#define SHRT_MAX 32767
#endif
#ifndef SHRT_MIN
#define SHRT_MIN (-32768)
#endif

84 85 86 87 88 89
/* Recent HPUXen have isfinite() macro in place of more standard finite() */
#if !defined(HAVE_FINITE) && defined(isfinite)
#define finite(x) isfinite(x)
#define HAVE_FINITE 1
#endif

90 91 92 93
/* not sure what the following should be, but better to make it over-sufficient */
#define MAXFLOATWIDTH	64
#define MAXDOUBLEWIDTH	128

94
/* ========== USER I/O ROUTINES ========== */
95 96 97 98 99 100 101


#define FLOAT4_MAX		 FLT_MAX
#define FLOAT4_MIN		 FLT_MIN
#define FLOAT8_MAX		 DBL_MAX
#define FLOAT8_MIN		 DBL_MIN

102

103
/* Configurable GUC parameter */
B
Bruce Momjian 已提交
104
int			extra_float_digits = 0;		/* Added to DBL_DIG or FLT_DIG */
105 106 107 108


static void CheckFloat4Val(double val);
static void CheckFloat8Val(double val);
B
Bruce Momjian 已提交
109 110
static int	float4_cmp_internal(float4 a, float4 b);
static int	float8_cmp_internal(float8 a, float8 b);
B
Bruce Momjian 已提交
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#ifndef HAVE_CBRT
static double cbrt(double x);
#endif   /* HAVE_CBRT */


/*
 * Routines to provide reasonably platform-independent handling of
 * infinity and NaN.  We assume that isinf() and isnan() are available
 * and work per spec.  (On some platforms, we have to supply our own;
 * see src/port.)  However, generating an Infinity or NaN in the first
 * place is less well standardized; pre-C99 systems tend not to have C99's
 * INFINITY and NAN macros.  We centralize our workarounds for this here.
 */

double
get_float8_infinity(void)
{
#ifdef INFINITY
	/* C99 standard way */
	return (double) INFINITY;
#else
B
Bruce Momjian 已提交
133

134 135
	/*
	 * On some platforms, HUGE_VAL is an infinity, elsewhere it's just the
B
Bruce Momjian 已提交
136 137
	 * largest normal double.  We assume forcing an overflow will get us a
	 * true infinity.
138 139 140 141 142 143 144 145 146 147 148 149
	 */
	return (double) (HUGE_VAL * HUGE_VAL);
#endif
}

float
get_float4_infinity(void)
{
#ifdef INFINITY
	/* C99 standard way */
	return (float) INFINITY;
#else
B
Bruce Momjian 已提交
150

151 152
	/*
	 * On some platforms, HUGE_VAL is an infinity, elsewhere it's just the
B
Bruce Momjian 已提交
153 154
	 * largest normal double.  We assume forcing an overflow will get us a
	 * true infinity.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	 */
	return (float) (HUGE_VAL * HUGE_VAL);
#endif
}

double
get_float8_nan(void)
{
#ifdef NAN
	/* C99 standard way */
	return (double) NAN;
#else
	/* Assume we can get a NAN via zero divide */
	return (double) (0.0 / 0.0);
#endif
}

float
get_float4_nan(void)
{
#ifdef NAN
	/* C99 standard way */
	return (float) NAN;
#else
	/* Assume we can get a NAN via zero divide */
	return (float) (0.0 / 0.0);
#endif
}

184

185 186 187 188 189 190 191
/*
 * Returns -1 if 'val' represents negative infinity, 1 if 'val'
 * represents (positive) infinity, and 0 otherwise. On some platforms,
 * this is equivalent to the isinf() macro, but not everywhere: C99
 * does not specify that isinf() needs to distinguish between positive
 * and negative infinity.
 */
192
int
193 194
is_infinite(double val)
{
B
Bruce Momjian 已提交
195
	int			inf = isinf(val);
196 197 198 199 200 201 202 203 204

	if (inf == 0)
		return 0;

	if (val > 0)
		return 1;

	return -1;
}
205

206

207
/*
208 209
 * check to see if a float4 val is outside of the FLOAT4_MIN,
 * FLOAT4_MAX bounds.
210
 *
211 212
 * raise an ereport() error if it is
 */
213 214
static void
CheckFloat4Val(double val)
215
{
216
	if (fabs(val) > FLOAT4_MAX)
217 218
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
219
				 errmsg("type \"real\" value out of range: overflow")));
220
	if (val != 0.0 && fabs(val) < FLOAT4_MIN)
221 222
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
223
				 errmsg("type \"real\" value out of range: underflow")));
224 225 226
}

/*
227 228
 * check to see if a float8 val is outside of the FLOAT8_MIN,
 * FLOAT8_MAX bounds.
229
 *
230
 * raise an ereport() error if it is
231
 */
232
static void
233
CheckFloat8Val(double val)
234
{
235
	if (fabs(val) > FLOAT8_MAX)
236 237
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
B
Bruce Momjian 已提交
238
		  errmsg("type \"double precision\" value out of range: overflow")));
239
	if (val != 0.0 && fabs(val) < FLOAT8_MIN)
240 241
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
B
Bruce Momjian 已提交
242
		 errmsg("type \"double precision\" value out of range: underflow")));
243 244 245
}

/*
246 247 248 249 250
 *		float4in		- converts "num" to float
 *						  restricted syntax:
 *						  {<sp>} [+|-] {digit} [.{digit}] [<exp>]
 *						  where <sp> is a space, digit is 0-9,
 *						  <exp> is "e" or "E" followed by an integer.
251
 */
252 253
Datum
float4in(PG_FUNCTION_ARGS)
254
{
255
	char	   *num = PG_GETARG_CSTRING(0);
256
	char	   *orig_num;
257 258
	double		val;
	char	   *endptr;
259

260
	/*
B
Bruce Momjian 已提交
261 262 263
	 * endptr points to the first character _after_ the sequence we recognized
	 * as a valid floating point number. orig_num points to the original input
	 * string.
264 265 266 267
	 */
	orig_num = num;

	/*
B
Bruce Momjian 已提交
268 269
	 * Check for an empty-string input to begin with, to avoid the vagaries of
	 * strtod() on different platforms.
270 271
	 */
	if (*num == '\0')
272 273 274 275
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
				 errmsg("invalid input syntax for type real: \"%s\"",
						orig_num)));
276 277

	/* skip leading whitespace */
278
	while (*num != '\0' && isspace((unsigned char) *num))
279 280
		num++;

281
	errno = 0;
282
	val = strtod(num, &endptr);
283

284
	/* did we not see anything that looks like a double? */
285
	if (endptr == num || errno != 0)
286
	{
287
		/*
B
Bruce Momjian 已提交
288 289 290
		 * C99 requires that strtod() accept NaN and [-]Infinity, but not all
		 * platforms support that yet (and some accept them but set ERANGE
		 * anyway...)  Therefore, we check for these inputs ourselves.
291
		 */
292
		if (pg_strncasecmp(num, "NaN", 3) == 0)
293
		{
294
			val = get_float4_nan();
295 296
			endptr = num + 3;
		}
297
		else if (pg_strncasecmp(num, "Infinity", 8) == 0)
298
		{
299
			val = get_float4_infinity();
300 301
			endptr = num + 8;
		}
302
		else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
303
		{
B
Bruce Momjian 已提交
304
			val = -get_float4_infinity();
305 306
			endptr = num + 9;
		}
307 308 309 310 311
		else if (errno == ERANGE)
			ereport(ERROR,
					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
					 errmsg("\"%s\" is out of range for type real",
							orig_num)));
312
		else
313 314
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
315
					 errmsg("invalid input syntax for type real: \"%s\"",
316
							orig_num)));
317
	}
318 319 320 321
#ifdef HAVE_BUGGY_SOLARIS_STRTOD
	else
	{
		/*
B
Bruce Momjian 已提交
322 323 324
		 * Many versions of Solaris have a bug wherein strtod sets endptr to
		 * point one byte beyond the end of the string when given "inf" or
		 * "infinity".
325 326 327 328
		 */
		if (endptr != num && endptr[-1] == '\0')
			endptr--;
	}
B
Bruce Momjian 已提交
329
#endif   /* HAVE_BUGGY_SOLARIS_STRTOD */
330

331
	/* skip trailing whitespace */
332
	while (*endptr != '\0' && isspace((unsigned char) *endptr))
333 334 335 336 337 338 339
		endptr++;

	/* if there is any junk left at the end of the string, bail out */
	if (*endptr != '\0')
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
				 errmsg("invalid input syntax for type real: \"%s\"",
340
						orig_num)));
341

342
	/*
B
Bruce Momjian 已提交
343 344
	 * if we get here, we have a legal double, still need to check to see if
	 * it's a legal float4
345
	 */
346 347
	if (!isinf(val))
		CheckFloat4Val(val);
348

349
	PG_RETURN_FLOAT4((float4) val);
350 351 352
}

/*
353 354
 *		float4out		- converts a float4 number to a string
 *						  using a standard output format
355
 */
356 357
Datum
float4out(PG_FUNCTION_ARGS)
358
{
359
	float4		num = PG_GETARG_FLOAT4(0);
360
	char	   *ascii = (char *) palloc(MAXFLOATWIDTH + 1);
361

362 363 364
	if (isnan(num))
		PG_RETURN_CSTRING(strcpy(ascii, "NaN"));

365 366 367 368 369 370 371 372 373
	switch (is_infinite(num))
	{
		case 1:
			strcpy(ascii, "Infinity");
			break;
		case -1:
			strcpy(ascii, "-Infinity");
			break;
		default:
B
Bruce Momjian 已提交
374 375
			{
				int			ndig = FLT_DIG + extra_float_digits;
376

B
Bruce Momjian 已提交
377 378 379 380 381
				if (ndig < 1)
					ndig = 1;

				sprintf(ascii, "%.*g", ndig, num);
			}
382
	}
383

384
	PG_RETURN_CSTRING(ascii);
385 386
}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
/*
 *		float4recv			- converts external binary format to float4
 */
Datum
float4recv(PG_FUNCTION_ARGS)
{
	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);

	PG_RETURN_FLOAT4(pq_getmsgfloat4(buf));
}

/*
 *		float4send			- converts float4 to binary format
 */
Datum
float4send(PG_FUNCTION_ARGS)
{
	float4		num = PG_GETARG_FLOAT4(0);
	StringInfoData buf;

	pq_begintypsend(&buf);
	pq_sendfloat4(&buf, num);
	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

412
/*
413 414 415 416 417
 *		float8in		- converts "num" to float8
 *						  restricted syntax:
 *						  {<sp>} [+|-] {digit} [.{digit}] [<exp>]
 *						  where <sp> is a space, digit is 0-9,
 *						  <exp> is "e" or "E" followed by an integer.
418
 */
419 420
Datum
float8in(PG_FUNCTION_ARGS)
421
{
422
	char	   *num = PG_GETARG_CSTRING(0);
423
	char	   *orig_num;
424 425
	double		val;
	char	   *endptr;
426

427
	/*
B
Bruce Momjian 已提交
428 429 430
	 * endptr points to the first character _after_ the sequence we recognized
	 * as a valid floating point number. orig_num points to the original input
	 * string.
431 432 433 434
	 */
	orig_num = num;

	/*
B
Bruce Momjian 已提交
435 436
	 * Check for an empty-string input to begin with, to avoid the vagaries of
	 * strtod() on different platforms.
437 438
	 */
	if (*num == '\0')
439 440
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
B
Bruce Momjian 已提交
441 442
			 errmsg("invalid input syntax for type double precision: \"%s\"",
					orig_num)));
443 444

	/* skip leading whitespace */
445
	while (*num != '\0' && isspace((unsigned char) *num))
446 447
		num++;

448
	errno = 0;
449
	val = strtod(num, &endptr);
450

451
	/* did we not see anything that looks like a double? */
452
	if (endptr == num || errno != 0)
453
	{
454
		/*
B
Bruce Momjian 已提交
455 456 457
		 * C99 requires that strtod() accept NaN and [-]Infinity, but not all
		 * platforms support that yet (and some accept them but set ERANGE
		 * anyway...)  Therefore, we check for these inputs ourselves.
458
		 */
459
		if (pg_strncasecmp(num, "NaN", 3) == 0)
460
		{
461
			val = get_float8_nan();
462 463
			endptr = num + 3;
		}
464
		else if (pg_strncasecmp(num, "Infinity", 8) == 0)
465
		{
466
			val = get_float8_infinity();
467 468
			endptr = num + 8;
		}
469
		else if (pg_strncasecmp(num, "-Infinity", 9) == 0)
470
		{
B
Bruce Momjian 已提交
471
			val = -get_float8_infinity();
472 473
			endptr = num + 9;
		}
474 475 476
		else if (errno == ERANGE)
			ereport(ERROR,
					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
B
Bruce Momjian 已提交
477 478
				   errmsg("\"%s\" is out of range for type double precision",
						  orig_num)));
479
		else
480 481
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
B
Bruce Momjian 已提交
482 483
			 errmsg("invalid input syntax for type double precision: \"%s\"",
					orig_num)));
484
	}
485 486 487 488
#ifdef HAVE_BUGGY_SOLARIS_STRTOD
	else
	{
		/*
B
Bruce Momjian 已提交
489 490 491
		 * Many versions of Solaris have a bug wherein strtod sets endptr to
		 * point one byte beyond the end of the string when given "inf" or
		 * "infinity".
492 493 494 495
		 */
		if (endptr != num && endptr[-1] == '\0')
			endptr--;
	}
B
Bruce Momjian 已提交
496
#endif   /* HAVE_BUGGY_SOLARIS_STRTOD */
497

498
	/* skip trailing whitespace */
499
	while (*endptr != '\0' && isspace((unsigned char) *endptr))
500 501 502 503 504 505
		endptr++;

	/* if there is any junk left at the end of the string, bail out */
	if (*endptr != '\0')
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
B
Bruce Momjian 已提交
506 507
			 errmsg("invalid input syntax for type double precision: \"%s\"",
					orig_num)));
508

509 510
	if (!isinf(val))
		CheckFloat8Val(val);
511

512
	PG_RETURN_FLOAT8(val);
513 514 515
}

/*
516 517
 *		float8out		- converts float8 number to a string
 *						  using a standard output format
518
 */
519 520
Datum
float8out(PG_FUNCTION_ARGS)
521
{
522
	float8		num = PG_GETARG_FLOAT8(0);
523
	char	   *ascii = (char *) palloc(MAXDOUBLEWIDTH + 1);
524

525 526
	if (isnan(num))
		PG_RETURN_CSTRING(strcpy(ascii, "NaN"));
527

528 529 530 531 532 533 534 535 536
	switch (is_infinite(num))
	{
		case 1:
			strcpy(ascii, "Infinity");
			break;
		case -1:
			strcpy(ascii, "-Infinity");
			break;
		default:
B
Bruce Momjian 已提交
537 538
			{
				int			ndig = DBL_DIG + extra_float_digits;
539

B
Bruce Momjian 已提交
540 541 542 543 544
				if (ndig < 1)
					ndig = 1;

				sprintf(ascii, "%.*g", ndig, num);
			}
545
	}
546

547
	PG_RETURN_CSTRING(ascii);
548 549
}

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
/*
 *		float8recv			- converts external binary format to float8
 */
Datum
float8recv(PG_FUNCTION_ARGS)
{
	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);

	PG_RETURN_FLOAT8(pq_getmsgfloat8(buf));
}

/*
 *		float8send			- converts float8 to binary format
 */
Datum
float8send(PG_FUNCTION_ARGS)
{
	float8		num = PG_GETARG_FLOAT8(0);
	StringInfoData buf;

	pq_begintypsend(&buf);
	pq_sendfloat8(&buf, num);
	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}


576 577 578 579
/* ========== PUBLIC ROUTINES ========== */


/*
580 581 582
 *		======================
 *		FLOAT4 BASE OPERATIONS
 *		======================
583 584 585
 */

/*
586
 *		float4abs		- returns |arg1| (absolute value)
587
 */
588 589
Datum
float4abs(PG_FUNCTION_ARGS)
590
{
591
	float4		arg1 = PG_GETARG_FLOAT4(0);
592

593
	PG_RETURN_FLOAT4((float4) fabs(arg1));
594 595 596
}

/*
597
 *		float4um		- returns -arg1 (unary minus)
598
 */
599 600
Datum
float4um(PG_FUNCTION_ARGS)
601
{
602
	float4		arg1 = PG_GETARG_FLOAT4(0);
603

604
	PG_RETURN_FLOAT4((float4) -arg1);
605 606
}

607 608 609 610
Datum
float4up(PG_FUNCTION_ARGS)
{
	float4		arg = PG_GETARG_FLOAT4(0);
611

612 613 614
	PG_RETURN_FLOAT4(arg);
}

615 616
Datum
float4larger(PG_FUNCTION_ARGS)
617
{
618 619 620
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	float4		result;
621

622 623 624 625
	if (float4_cmp_internal(arg1, arg2) > 0)
		result = arg1;
	else
		result = arg2;
626
	PG_RETURN_FLOAT4(result);
627 628
}

629 630
Datum
float4smaller(PG_FUNCTION_ARGS)
631
{
632 633 634
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	float4		result;
635

636 637 638 639
	if (float4_cmp_internal(arg1, arg2) < 0)
		result = arg1;
	else
		result = arg2;
640
	PG_RETURN_FLOAT4(result);
641 642 643
}

/*
644 645 646
 *		======================
 *		FLOAT8 BASE OPERATIONS
 *		======================
647 648 649
 */

/*
650
 *		float8abs		- returns |arg1| (absolute value)
651
 */
652 653
Datum
float8abs(PG_FUNCTION_ARGS)
654
{
655 656
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
657

658
	result = fabs(arg1);
659

660 661
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
662 663 664 665
}


/*
666
 *		float8um		- returns -arg1 (unary minus)
667
 */
668 669
Datum
float8um(PG_FUNCTION_ARGS)
670
{
671 672
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
673

674
	result = ((arg1 != 0) ? -(arg1) : arg1);
675

676 677
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
678 679
}

680 681 682 683
Datum
float8up(PG_FUNCTION_ARGS)
{
	float8		arg = PG_GETARG_FLOAT8(0);
684

685 686 687
	PG_RETURN_FLOAT8(arg);
}

688 689
Datum
float8larger(PG_FUNCTION_ARGS)
690
{
691 692 693
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
694

695 696 697 698
	if (float8_cmp_internal(arg1, arg2) > 0)
		result = arg1;
	else
		result = arg2;
699
	PG_RETURN_FLOAT8(result);
700 701
}

702 703
Datum
float8smaller(PG_FUNCTION_ARGS)
704
{
705 706 707
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
708

709 710 711 712
	if (float8_cmp_internal(arg1, arg2) < 0)
		result = arg1;
	else
		result = arg2;
713
	PG_RETURN_FLOAT8(result);
714 715 716 717
}


/*
718 719 720
 *		====================
 *		ARITHMETIC OPERATORS
 *		====================
721 722 723
 */

/*
724 725 726 727
 *		float4pl		- returns arg1 + arg2
 *		float4mi		- returns arg1 - arg2
 *		float4mul		- returns arg1 * arg2
 *		float4div		- returns arg1 / arg2
728
 */
729 730
Datum
float4pl(PG_FUNCTION_ARGS)
731
{
732 733 734
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	double		result;
735

736 737 738
	result = arg1 + arg2;
	CheckFloat4Val(result);
	PG_RETURN_FLOAT4((float4) result);
739 740
}

741 742
Datum
float4mi(PG_FUNCTION_ARGS)
743
{
744 745 746
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	double		result;
747

748 749 750
	result = arg1 - arg2;
	CheckFloat4Val(result);
	PG_RETURN_FLOAT4((float4) result);
751 752
}

753 754
Datum
float4mul(PG_FUNCTION_ARGS)
755
{
756 757 758
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	double		result;
759

760 761 762
	result = arg1 * arg2;
	CheckFloat4Val(result);
	PG_RETURN_FLOAT4((float4) result);
763 764
}

765 766
Datum
float4div(PG_FUNCTION_ARGS)
767
{
768 769 770
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	double		result;
771

772
	if (arg2 == 0.0)
773 774 775
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
776

777 778
	/* Do division in float8, then check for overflow */
	result = (float8) arg1 / (float8) arg2;
779

780 781
	CheckFloat4Val(result);
	PG_RETURN_FLOAT4((float4) result);
782 783 784
}

/*
785 786 787 788
 *		float8pl		- returns arg1 + arg2
 *		float8mi		- returns arg1 - arg2
 *		float8mul		- returns arg1 * arg2
 *		float8div		- returns arg1 / arg2
789
 */
790 791
Datum
float8pl(PG_FUNCTION_ARGS)
792
{
793 794 795
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
796

797
	result = arg1 + arg2;
798

799 800
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
801 802
}

803 804
Datum
float8mi(PG_FUNCTION_ARGS)
805
{
806 807 808
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
809

810
	result = arg1 - arg2;
811

812 813
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
814 815
}

816 817
Datum
float8mul(PG_FUNCTION_ARGS)
818
{
819 820 821
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
822

823
	result = arg1 * arg2;
824

825 826
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
827 828
}

829 830
Datum
float8div(PG_FUNCTION_ARGS)
831
{
832 833 834
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
835

836
	if (arg2 == 0.0)
837 838 839
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
840

841 842 843 844
	result = arg1 / arg2;

	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
845 846
}

847 848

/*
849 850 851
 *		====================
 *		COMPARISON OPERATORS
 *		====================
852 853 854
 */

/*
855
 *		float4{eq,ne,lt,le,gt,ge}		- float4/float4 comparison operations
856
 */
857 858 859 860
static int
float4_cmp_internal(float4 a, float4 b)
{
	/*
B
Bruce Momjian 已提交
861 862 863
	 * We consider all NANs to be equal and larger than any non-NAN. This is
	 * somewhat arbitrary; the important thing is to have a consistent sort
	 * order.
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	 */
	if (isnan(a))
	{
		if (isnan(b))
			return 0;			/* NAN = NAN */
		else
			return 1;			/* NAN > non-NAN */
	}
	else if (isnan(b))
	{
		return -1;				/* non-NAN < NAN */
	}
	else
	{
		if (a > b)
			return 1;
880
		else if (a < b)
881
			return -1;
882 883
		else
			return 0;
884 885 886
	}
}

887 888
Datum
float4eq(PG_FUNCTION_ARGS)
889
{
890 891
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
892

893
	PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) == 0);
894 895
}

896 897
Datum
float4ne(PG_FUNCTION_ARGS)
898
{
899 900
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
901

902
	PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) != 0);
903 904
}

905 906
Datum
float4lt(PG_FUNCTION_ARGS)
907
{
908 909
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
910

911
	PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) < 0);
912 913
}

914 915
Datum
float4le(PG_FUNCTION_ARGS)
916
{
917 918
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
919

920
	PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) <= 0);
921 922
}

923 924
Datum
float4gt(PG_FUNCTION_ARGS)
925
{
926 927
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
928

929
	PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) > 0);
930 931
}

932 933
Datum
float4ge(PG_FUNCTION_ARGS)
934
{
935 936
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
937

938 939 940 941 942 943 944 945 946 947
	PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) >= 0);
}

Datum
btfloat4cmp(PG_FUNCTION_ARGS)
{
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);

	PG_RETURN_INT32(float4_cmp_internal(arg1, arg2));
948 949 950
}

/*
951
 *		float8{eq,ne,lt,le,gt,ge}		- float8/float8 comparison operations
952
 */
953 954 955 956
static int
float8_cmp_internal(float8 a, float8 b)
{
	/*
B
Bruce Momjian 已提交
957 958 959
	 * We consider all NANs to be equal and larger than any non-NAN. This is
	 * somewhat arbitrary; the important thing is to have a consistent sort
	 * order.
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
	 */
	if (isnan(a))
	{
		if (isnan(b))
			return 0;			/* NAN = NAN */
		else
			return 1;			/* NAN > non-NAN */
	}
	else if (isnan(b))
	{
		return -1;				/* non-NAN < NAN */
	}
	else
	{
		if (a > b)
			return 1;
976
		else if (a < b)
977
			return -1;
978 979
		else
			return 0;
980 981 982
	}
}

983 984
Datum
float8eq(PG_FUNCTION_ARGS)
985
{
986 987
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
988

989
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);
990 991
}

992 993
Datum
float8ne(PG_FUNCTION_ARGS)
994
{
995 996
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
997

998
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);
999 1000
}

1001 1002
Datum
float8lt(PG_FUNCTION_ARGS)
1003
{
1004 1005
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
1006

1007
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);
1008 1009
}

1010 1011
Datum
float8le(PG_FUNCTION_ARGS)
1012
{
1013 1014
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
1015

1016
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);
1017 1018
}

1019 1020
Datum
float8gt(PG_FUNCTION_ARGS)
1021
{
1022 1023
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
1024

1025
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);
1026 1027
}

1028 1029
Datum
float8ge(PG_FUNCTION_ARGS)
1030
{
1031 1032
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
1033

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);
}

Datum
btfloat8cmp(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);

	PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
1044 1045
}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
Datum
btfloat48cmp(PG_FUNCTION_ARGS)
{
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);

	/* widen float4 to float8 and then compare */
	PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
}

Datum
btfloat84cmp(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);

	/* widen float4 to float8 and then compare */
	PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));
}

1066 1067

/*
1068 1069 1070
 *		===================
 *		CONVERSION ROUTINES
 *		===================
1071 1072 1073
 */

/*
1074
 *		ftod			- converts a float4 number to a float8 number
1075
 */
1076 1077
Datum
ftod(PG_FUNCTION_ARGS)
1078
{
1079
	float4		num = PG_GETARG_FLOAT4(0);
1080

1081
	PG_RETURN_FLOAT8((float8) num);
1082 1083 1084 1085
}


/*
1086
 *		dtof			- converts a float8 number to a float4 number
1087
 */
1088 1089
Datum
dtof(PG_FUNCTION_ARGS)
1090
{
1091
	float8		num = PG_GETARG_FLOAT8(0);
1092

1093
	CheckFloat4Val(num);
1094

1095
	PG_RETURN_FLOAT4((float4) num);
1096 1097 1098 1099
}


/*
1100
 *		dtoi4			- converts a float8 number to an int4 number
1101
 */
1102 1103
Datum
dtoi4(PG_FUNCTION_ARGS)
1104
{
1105
	float8		num = PG_GETARG_FLOAT8(0);
1106
	int32		result;
1107

1108
	if ((num < INT_MIN) || (num > INT_MAX))
1109 1110 1111
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("integer out of range")));
1112

1113 1114
	result = (int32) rint(num);
	PG_RETURN_INT32(result);
1115 1116 1117 1118
}


/*
1119
 *		dtoi2			- converts a float8 number to an int2 number
1120
 */
1121 1122
Datum
dtoi2(PG_FUNCTION_ARGS)
1123
{
1124
	float8		num = PG_GETARG_FLOAT8(0);
1125
	int16		result;
1126

1127
	if ((num < SHRT_MIN) || (num > SHRT_MAX))
1128 1129
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1130
				 errmsg("smallint out of range")));
1131

1132 1133
	result = (int16) rint(num);
	PG_RETURN_INT16(result);
1134 1135 1136 1137
}


/*
1138
 *		i4tod			- converts an int4 number to a float8 number
1139
 */
1140 1141
Datum
i4tod(PG_FUNCTION_ARGS)
1142
{
1143 1144
	int32		num = PG_GETARG_INT32(0);
	float8		result;
1145

1146 1147
	result = num;
	PG_RETURN_FLOAT8(result);
1148 1149 1150 1151
}


/*
1152
 *		i2tod			- converts an int2 number to a float8 number
1153
 */
1154 1155
Datum
i2tod(PG_FUNCTION_ARGS)
1156
{
1157 1158
	int16		num = PG_GETARG_INT16(0);
	float8		result;
1159

1160 1161
	result = num;
	PG_RETURN_FLOAT8(result);
1162 1163 1164 1165
}


/*
1166
 *		ftoi4			- converts a float4 number to an int4 number
1167
 */
1168 1169
Datum
ftoi4(PG_FUNCTION_ARGS)
1170
{
1171
	float4		num = PG_GETARG_FLOAT4(0);
1172
	int32		result;
1173

1174
	if ((num < INT_MIN) || (num > INT_MAX))
1175 1176 1177
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("integer out of range")));
1178

1179 1180
	result = (int32) rint(num);
	PG_RETURN_INT32(result);
1181 1182 1183 1184
}


/*
1185
 *		ftoi2			- converts a float4 number to an int2 number
1186
 */
1187 1188
Datum
ftoi2(PG_FUNCTION_ARGS)
1189
{
1190
	float4		num = PG_GETARG_FLOAT4(0);
1191
	int16		result;
1192

1193
	if ((num < SHRT_MIN) || (num > SHRT_MAX))
1194 1195
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1196
				 errmsg("smallint out of range")));
1197

1198 1199
	result = (int16) rint(num);
	PG_RETURN_INT16(result);
1200 1201 1202 1203
}


/*
1204
 *		i4tof			- converts an int4 number to a float8 number
1205
 */
1206 1207
Datum
i4tof(PG_FUNCTION_ARGS)
1208
{
1209 1210
	int32		num = PG_GETARG_INT32(0);
	float4		result;
1211

1212 1213
	result = num;
	PG_RETURN_FLOAT4(result);
1214 1215 1216 1217
}


/*
1218
 *		i2tof			- converts an int2 number to a float4 number
1219
 */
1220 1221
Datum
i2tof(PG_FUNCTION_ARGS)
1222
{
1223 1224
	int16		num = PG_GETARG_INT16(0);
	float4		result;
1225

1226 1227
	result = num;
	PG_RETURN_FLOAT4(result);
1228 1229 1230
}


1231 1232 1233
/*
 *		float8_text		- converts a float8 number to a text string
 */
1234 1235
Datum
float8_text(PG_FUNCTION_ARGS)
1236
{
1237
	float8		num = PG_GETARG_FLOAT8(0);
B
Bruce Momjian 已提交
1238 1239 1240
	text	   *result;
	int			len;
	char	   *str;
1241

1242 1243 1244
	str = DatumGetCString(DirectFunctionCall1(float8out,
											  Float8GetDatum(num)));

1245
	len = strlen(str) + VARHDRSZ;
1246

1247
	result = (text *) palloc(len);
1248

J
TOAST  
Jan Wieck 已提交
1249
	VARATT_SIZEP(result) = len;
1250
	memcpy(VARDATA(result), str, (len - VARHDRSZ));
1251 1252

	pfree(str);
1253 1254 1255

	PG_RETURN_TEXT_P(result);
}
1256 1257 1258 1259 1260


/*
 *		text_float8		- converts a text string to a float8 number
 */
1261 1262
Datum
text_float8(PG_FUNCTION_ARGS)
1263
{
1264
	text	   *string = PG_GETARG_TEXT_P(0);
1265
	Datum		result;
B
Bruce Momjian 已提交
1266 1267
	int			len;
	char	   *str;
1268 1269 1270

	len = (VARSIZE(string) - VARHDRSZ);
	str = palloc(len + 1);
1271
	memcpy(str, VARDATA(string), len);
1272 1273
	*(str + len) = '\0';

1274
	result = DirectFunctionCall1(float8in, CStringGetDatum(str));
1275

1276 1277
	pfree(str);

1278
	PG_RETURN_DATUM(result);
1279
}
1280 1281 1282 1283 1284


/*
 *		float4_text		- converts a float4 number to a text string
 */
1285 1286
Datum
float4_text(PG_FUNCTION_ARGS)
1287
{
1288
	float4		num = PG_GETARG_FLOAT4(0);
B
Bruce Momjian 已提交
1289 1290 1291
	text	   *result;
	int			len;
	char	   *str;
1292

1293 1294 1295
	str = DatumGetCString(DirectFunctionCall1(float4out,
											  Float4GetDatum(num)));

1296
	len = strlen(str) + VARHDRSZ;
1297

1298
	result = (text *) palloc(len);
1299

J
TOAST  
Jan Wieck 已提交
1300
	VARATT_SIZEP(result) = len;
1301
	memcpy(VARDATA(result), str, (len - VARHDRSZ));
1302 1303

	pfree(str);
1304 1305 1306

	PG_RETURN_TEXT_P(result);
}
1307 1308 1309 1310 1311


/*
 *		text_float4		- converts a text string to a float4 number
 */
1312 1313
Datum
text_float4(PG_FUNCTION_ARGS)
1314
{
1315
	text	   *string = PG_GETARG_TEXT_P(0);
1316
	Datum		result;
B
Bruce Momjian 已提交
1317 1318
	int			len;
	char	   *str;
1319 1320 1321

	len = (VARSIZE(string) - VARHDRSZ);
	str = palloc(len + 1);
1322
	memcpy(str, VARDATA(string), len);
1323 1324
	*(str + len) = '\0';

1325
	result = DirectFunctionCall1(float4in, CStringGetDatum(str));
1326

1327 1328
	pfree(str);

1329
	PG_RETURN_DATUM(result);
1330
}
1331 1332


1333
/*
1334 1335 1336
 *		=======================
 *		RANDOM FLOAT8 OPERATORS
 *		=======================
1337 1338 1339
 */

/*
1340
 *		dround			- returns	ROUND(arg1)
1341
 */
1342 1343
Datum
dround(PG_FUNCTION_ARGS)
1344
{
1345 1346
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1347

1348
	result = rint(arg1);
1349

1350
	PG_RETURN_FLOAT8(result);
1351 1352
}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
/*
 *		dceil			- returns the smallest integer greater than or
 *						  equal to the specified float
 */
Datum
dceil(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);

	PG_RETURN_FLOAT8(ceil(arg1));
}

/*
 *		dfloor			- returns the largest integer lesser than or
 *						  equal to the specified float
 */
Datum
dfloor(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);

	PG_RETURN_FLOAT8(floor(arg1));
}

/*
 *		dsign			- returns -1 if the argument is less than 0, 0
 *						  if the argument is equal to 0, and 1 if the
 *						  argument is greater than zero.
 */
Datum
dsign(PG_FUNCTION_ARGS)
{
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;

	if (arg1 > 0)
		result = 1.0;
	else if (arg1 < 0)
		result = -1.0;
	else
		result = 0.0;

	PG_RETURN_FLOAT8(result);
}
1397 1398

/*
1399 1400
 *		dtrunc			- returns truncation-towards-zero of arg1,
 *						  arg1 >= 0 ... the greatest integer less
1401
 *										than or equal to arg1
1402
 *						  arg1 < 0	... the least integer greater
1403
 *										than or equal to arg1
1404
 */
1405 1406
Datum
dtrunc(PG_FUNCTION_ARGS)
1407
{
1408 1409
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1410

1411 1412
	if (arg1 >= 0)
		result = floor(arg1);
1413
	else
1414 1415 1416
		result = -floor(-arg1);

	PG_RETURN_FLOAT8(result);
1417 1418 1419 1420
}


/*
1421
 *		dsqrt			- returns square root of arg1
1422
 */
1423 1424
Datum
dsqrt(PG_FUNCTION_ARGS)
1425
{
1426 1427
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1428

1429
	if (arg1 < 0)
1430
		ereport(ERROR,
1431
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
1432
				 errmsg("cannot take square root of a negative number")));
1433

1434
	result = sqrt(arg1);
1435

1436 1437
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
1438 1439 1440 1441
}


/*
1442
 *		dcbrt			- returns cube root of arg1
1443
 */
1444 1445
Datum
dcbrt(PG_FUNCTION_ARGS)
1446
{
1447 1448
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1449

1450 1451
	result = cbrt(arg1);
	PG_RETURN_FLOAT8(result);
1452 1453 1454 1455
}


/*
1456
 *		dpow			- returns pow(arg1,arg2)
1457
 */
1458 1459
Datum
dpow(PG_FUNCTION_ARGS)
1460
{
1461 1462 1463
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
1464

1465
	/*
B
Bruce Momjian 已提交
1466 1467
	 * The SQL spec requires that we emit a particular SQLSTATE error code for
	 * certain error conditions.
1468 1469 1470 1471 1472 1473 1474
	 */
	if ((arg1 == 0 && arg2 < 0) ||
		(arg1 < 0 && floor(arg2) != arg2))
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
				 errmsg("invalid argument for power function")));

1475
	/*
B
Bruce Momjian 已提交
1476 1477
	 * We must check both for errno getting set and for a NaN result, in order
	 * to deal with the vagaries of different platforms...
1478
	 */
1479
	errno = 0;
1480
	result = pow(arg1, arg2);
1481 1482
	if (errno != 0
#ifdef HAVE_FINITE
1483
		|| !finite(result)
1484
#endif
1485
		)
1486 1487 1488
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("result is out of range")));
1489

1490 1491
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
1492 1493 1494 1495
}


/*
1496
 *		dexp			- returns the exponential function of arg1
1497
 */
1498 1499
Datum
dexp(PG_FUNCTION_ARGS)
1500
{
1501 1502
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1503

1504
	/*
B
Bruce Momjian 已提交
1505 1506 1507
	 * We must check both for errno getting set and for a NaN result, in order
	 * to deal with the vagaries of different platforms. Also, a zero result
	 * implies unreported underflow.
1508
	 */
1509
	errno = 0;
1510 1511
	result = exp(arg1);
	if (errno != 0 || result == 0.0
1512
#ifdef HAVE_FINITE
1513
		|| !finite(result)
1514
#endif
1515
		)
1516 1517 1518
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("result is out of range")));
1519

1520 1521
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
1522 1523 1524 1525
}


/*
1526
 *		dlog1			- returns the natural logarithm of arg1
1527
 */
1528 1529
Datum
dlog1(PG_FUNCTION_ARGS)
1530
{
1531 1532
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1533

1534
	/*
B
Bruce Momjian 已提交
1535 1536
	 * Emit particular SQLSTATE error codes for ln(). This is required by the
	 * SQL standard.
1537
	 */
1538
	if (arg1 == 0.0)
1539
		ereport(ERROR,
1540
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1541
				 errmsg("cannot take logarithm of zero")));
1542
	if (arg1 < 0)
1543
		ereport(ERROR,
1544
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1545
				 errmsg("cannot take logarithm of a negative number")));
1546

1547 1548 1549 1550 1551
	result = log(arg1);

	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1552

1553

1554
/*
1555
 *		dlog10			- returns the base 10 logarithm of arg1
1556
 */
1557 1558
Datum
dlog10(PG_FUNCTION_ARGS)
1559
{
1560 1561
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1562

1563
	/*
B
Bruce Momjian 已提交
1564 1565 1566
	 * Emit particular SQLSTATE error codes for log(). The SQL spec doesn't
	 * define log(), but it does define ln(), so it makes sense to emit the
	 * same error code for an analogous error condition.
1567
	 */
1568
	if (arg1 == 0.0)
1569
		ereport(ERROR,
1570
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1571
				 errmsg("cannot take logarithm of zero")));
1572
	if (arg1 < 0)
1573
		ereport(ERROR,
1574
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
1575
				 errmsg("cannot take logarithm of a negative number")));
1576

1577 1578 1579 1580 1581
	result = log10(arg1);

	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1582

1583

1584
/*
1585
 *		dacos			- returns the arccos of arg1 (radians)
1586
 */
1587 1588
Datum
dacos(PG_FUNCTION_ARGS)
1589
{
1590 1591
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1592

1593
	errno = 0;
1594
	result = acos(arg1);
1595 1596
	if (errno != 0
#ifdef HAVE_FINITE
1597
		|| !finite(result)
1598 1599
#endif
		)
1600 1601 1602
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1603

1604 1605 1606
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1607 1608 1609


/*
1610
 *		dasin			- returns the arcsin of arg1 (radians)
1611
 */
1612 1613
Datum
dasin(PG_FUNCTION_ARGS)
1614
{
1615 1616
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1617

1618
	errno = 0;
1619
	result = asin(arg1);
1620 1621
	if (errno != 0
#ifdef HAVE_FINITE
1622
		|| !finite(result)
1623 1624
#endif
		)
1625 1626 1627
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1628

1629 1630 1631
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1632 1633 1634


/*
1635
 *		datan			- returns the arctan of arg1 (radians)
1636
 */
1637 1638
Datum
datan(PG_FUNCTION_ARGS)
1639
{
1640 1641
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1642

1643
	errno = 0;
1644
	result = atan(arg1);
1645 1646
	if (errno != 0
#ifdef HAVE_FINITE
1647
		|| !finite(result)
1648 1649
#endif
		)
1650 1651 1652
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1653

1654 1655 1656
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1657 1658 1659


/*
1660
 *		atan2			- returns the arctan2 of arg1 (radians)
1661
 */
1662 1663
Datum
datan2(PG_FUNCTION_ARGS)
1664
{
1665 1666 1667
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
1668

1669
	errno = 0;
1670
	result = atan2(arg1, arg2);
1671 1672
	if (errno != 0
#ifdef HAVE_FINITE
1673
		|| !finite(result)
1674 1675
#endif
		)
1676 1677 1678
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1679

1680 1681 1682
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1683 1684 1685


/*
1686
 *		dcos			- returns the cosine of arg1 (radians)
1687
 */
1688 1689
Datum
dcos(PG_FUNCTION_ARGS)
1690
{
1691 1692
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1693

1694
	errno = 0;
1695
	result = cos(arg1);
1696 1697
	if (errno != 0
#ifdef HAVE_FINITE
1698
		|| !finite(result)
1699 1700
#endif
		)
1701 1702 1703
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1704

1705 1706 1707
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1708 1709 1710


/*
1711
 *		dcot			- returns the cotangent of arg1 (radians)
1712
 */
1713 1714
Datum
dcot(PG_FUNCTION_ARGS)
1715
{
1716 1717
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1718

1719
	errno = 0;
1720 1721
	result = tan(arg1);
	if (errno != 0 || result == 0.0
1722
#ifdef HAVE_FINITE
1723
		|| !finite(result)
1724 1725
#endif
		)
1726 1727 1728
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1729

1730 1731 1732 1733
	result = 1.0 / result;
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1734 1735 1736


/*
1737
 *		dsin			- returns the sine of arg1 (radians)
1738
 */
1739 1740
Datum
dsin(PG_FUNCTION_ARGS)
1741
{
1742 1743
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1744

1745
	errno = 0;
1746
	result = sin(arg1);
1747 1748
	if (errno != 0
#ifdef HAVE_FINITE
1749
		|| !finite(result)
1750 1751
#endif
		)
1752 1753 1754
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1755

1756 1757 1758
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1759 1760 1761


/*
1762
 *		dtan			- returns the tangent of arg1 (radians)
1763
 */
1764 1765
Datum
dtan(PG_FUNCTION_ARGS)
1766
{
1767 1768
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1769

1770
	errno = 0;
1771
	result = tan(arg1);
1772 1773
	if (errno != 0
#ifdef HAVE_FINITE
1774
		|| !finite(result)
1775 1776
#endif
		)
1777 1778 1779
		ereport(ERROR,
				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
				 errmsg("input is out of range")));
1780

1781 1782 1783
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1784 1785 1786


/*
1787
 *		degrees		- returns degrees converted from radians
1788
 */
1789 1790
Datum
degrees(PG_FUNCTION_ARGS)
1791
{
1792 1793
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1794

1795
	result = arg1 * (180.0 / M_PI);
1796

1797 1798 1799
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1800 1801 1802


/*
1803
 *		dpi				- returns the constant PI
1804
 */
1805 1806
Datum
dpi(PG_FUNCTION_ARGS)
1807
{
1808 1809
	PG_RETURN_FLOAT8(M_PI);
}
1810 1811 1812


/*
1813
 *		radians		- returns radians converted from degrees
1814
 */
1815 1816
Datum
radians(PG_FUNCTION_ARGS)
1817
{
1818 1819
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float8		result;
1820

1821
	result = arg1 * (M_PI / 180.0);
1822

1823 1824 1825
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
}
1826 1827 1828


/*
1829
 *		drandom		- returns a random number
1830
 */
1831 1832
Datum
drandom(PG_FUNCTION_ARGS)
1833
{
1834
	float8		result;
1835

1836 1837
	/* result [0.0 - 1.0) */
	result = (double) random() / ((double) MAX_RANDOM_VALUE + 1);
1838

1839 1840
	PG_RETURN_FLOAT8(result);
}
1841 1842 1843 1844 1845


/*
 *		setseed		- set seed for the random number generator
 */
1846 1847
Datum
setseed(PG_FUNCTION_ARGS)
1848
{
1849
	float8		seed = PG_GETARG_FLOAT8(0);
1850
	int			iseed = (int) (seed * MAX_RANDOM_VALUE);
1851

1852
	srandom((unsigned int) iseed);
1853

1854 1855
	PG_RETURN_INT32(iseed);
}
1856 1857


1858

1859
/*
1860 1861 1862 1863 1864 1865 1866
 *		=========================
 *		FLOAT AGGREGATE OPERATORS
 *		=========================
 *
 *		float8_accum	- accumulate for AVG(), STDDEV(), etc
 *		float4_accum	- same, but input data is float4
 *		float8_avg		- produce final result for float AVG()
B
Bruce Momjian 已提交
1867
 *		float8_variance - produce final result for float VARIANCE()
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
 *		float8_stddev	- produce final result for float STDDEV()
 *
 * The transition datatype for all these aggregates is a 3-element array
 * of float8, holding the values N, sum(X), sum(X*X) in that order.
 *
 * Note that we represent N as a float to avoid having to build a special
 * datatype.  Given a reasonable floating-point implementation, there should
 * be no accuracy loss unless N exceeds 2 ^ 52 or so (by which time the
 * user will have doubtless lost interest anyway...)
 */

static float8 *
check_float8_array(ArrayType *transarray, const char *caller)
{
	/*
B
Bruce Momjian 已提交
1883 1884 1885
	 * We expect the input to be a 3-element float array; verify that. We
	 * don't need to use deconstruct_array() since the array data is just
	 * going to look like a C array of 3 float8 values.
1886
	 */
1887 1888
	if (ARR_NDIM(transarray) != 1 ||
		ARR_DIMS(transarray)[0] != 3 ||
1889
		ARR_HASNULL(transarray) ||
1890
		ARR_ELEMTYPE(transarray) != FLOAT8OID)
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
		elog(ERROR, "%s: expected 3-element float8 array", caller);
	return (float8 *) ARR_DATA_PTR(transarray);
}

Datum
float8_accum(PG_FUNCTION_ARGS)
{
	ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
	float8		newval = PG_GETARG_FLOAT8(1);
	float8	   *transvalues;
	float8		N,
				sumX,
				sumX2;

	transvalues = check_float8_array(transarray, "float8_accum");
	N = transvalues[0];
	sumX = transvalues[1];
	sumX2 = transvalues[2];

	N += 1.0;
	sumX += newval;
	sumX2 += newval * newval;

1914 1915
	/*
	 * If we're invoked by nodeAgg, we can cheat and modify our first
B
Bruce Momjian 已提交
1916 1917
	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
	 * new array with the updated transition data and return it.
1918 1919 1920 1921 1922 1923
	 */
	if (fcinfo->context && IsA(fcinfo->context, AggState))
	{
		transvalues[0] = N;
		transvalues[1] = sumX;
		transvalues[2] = sumX2;
1924

1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
		PG_RETURN_ARRAYTYPE_P(transarray);
	}
	else
	{
		Datum		transdatums[3];
		ArrayType  *result;

		transdatums[0] = Float8GetDatumFast(N);
		transdatums[1] = Float8GetDatumFast(sumX);
		transdatums[2] = Float8GetDatumFast(sumX2);
1935

1936 1937
		result = construct_array(transdatums, 3,
								 FLOAT8OID,
B
Bruce Momjian 已提交
1938
							 sizeof(float8), false /* float8 byval */ , 'd');
1939 1940 1941

		PG_RETURN_ARRAYTYPE_P(result);
	}
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
}

Datum
float4_accum(PG_FUNCTION_ARGS)
{
	ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
	float4		newval4 = PG_GETARG_FLOAT4(1);
	float8	   *transvalues;
	float8		N,
				sumX,
				sumX2,
				newval;

	transvalues = check_float8_array(transarray, "float4_accum");
	N = transvalues[0];
	sumX = transvalues[1];
	sumX2 = transvalues[2];

	/* Do arithmetic in float8 for best accuracy */
	newval = newval4;

	N += 1.0;
	sumX += newval;
	sumX2 += newval * newval;

1967 1968
	/*
	 * If we're invoked by nodeAgg, we can cheat and modify our first
B
Bruce Momjian 已提交
1969 1970
	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
	 * new array with the updated transition data and return it.
1971 1972 1973 1974 1975 1976
	 */
	if (fcinfo->context && IsA(fcinfo->context, AggState))
	{
		transvalues[0] = N;
		transvalues[1] = sumX;
		transvalues[2] = sumX2;
1977

1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
		PG_RETURN_ARRAYTYPE_P(transarray);
	}
	else
	{
		Datum		transdatums[3];
		ArrayType  *result;

		transdatums[0] = Float8GetDatumFast(N);
		transdatums[1] = Float8GetDatumFast(sumX);
		transdatums[2] = Float8GetDatumFast(sumX2);
1988

1989 1990
		result = construct_array(transdatums, 3,
								 FLOAT8OID,
B
Bruce Momjian 已提交
1991
							 sizeof(float8), false /* float8 byval */ , 'd');
1992 1993 1994

		PG_RETURN_ARRAYTYPE_P(result);
	}
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
}

Datum
float8_avg(PG_FUNCTION_ARGS)
{
	ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
	float8	   *transvalues;
	float8		N,
				sumX;

	transvalues = check_float8_array(transarray, "float8_avg");
	N = transvalues[0];
	sumX = transvalues[1];
	/* ignore sumX2 */

	/* SQL92 defines AVG of no values to be NULL */
	if (N == 0.0)
		PG_RETURN_NULL();

	PG_RETURN_FLOAT8(sumX / N);
}

Datum
float8_variance(PG_FUNCTION_ARGS)
{
	ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
	float8	   *transvalues;
	float8		N,
				sumX,
2024 2025
				sumX2,
				numerator;
2026 2027 2028 2029 2030 2031

	transvalues = check_float8_array(transarray, "float8_variance");
	N = transvalues[0];
	sumX = transvalues[1];
	sumX2 = transvalues[2];

2032
	/* Sample variance is undefined when N is 0 or 1, so return NULL */
2033
	if (N <= 1.0)
2034
		PG_RETURN_NULL();
2035

2036 2037 2038 2039 2040 2041 2042
	numerator = N * sumX2 - sumX * sumX;

	/* Watch out for roundoff error producing a negative numerator */
	if (numerator <= 0.0)
		PG_RETURN_FLOAT8(0.0);

	PG_RETURN_FLOAT8(numerator / (N * (N - 1.0)));
2043 2044 2045 2046 2047 2048 2049 2050 2051
}

Datum
float8_stddev(PG_FUNCTION_ARGS)
{
	ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
	float8	   *transvalues;
	float8		N,
				sumX,
2052 2053
				sumX2,
				numerator;
2054 2055 2056 2057 2058 2059

	transvalues = check_float8_array(transarray, "float8_stddev");
	N = transvalues[0];
	sumX = transvalues[1];
	sumX2 = transvalues[2];

2060
	/* Sample stddev is undefined when N is 0 or 1, so return NULL */
2061
	if (N <= 1.0)
2062
		PG_RETURN_NULL();
2063

2064 2065 2066 2067 2068 2069 2070
	numerator = N * sumX2 - sumX * sumX;

	/* Watch out for roundoff error producing a negative numerator */
	if (numerator <= 0.0)
		PG_RETURN_FLOAT8(0.0);

	PG_RETURN_FLOAT8(sqrt(numerator / (N * (N - 1.0))));
2071 2072 2073 2074 2075 2076 2077
}


/*
 *		====================================
 *		MIXED-PRECISION ARITHMETIC OPERATORS
 *		====================================
2078 2079 2080
 */

/*
2081 2082 2083 2084
 *		float48pl		- returns arg1 + arg2
 *		float48mi		- returns arg1 - arg2
 *		float48mul		- returns arg1 * arg2
 *		float48div		- returns arg1 / arg2
2085
 */
2086 2087
Datum
float48pl(PG_FUNCTION_ARGS)
2088
{
2089 2090 2091
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
2092

2093 2094 2095
	result = arg1 + arg2;
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2096 2097
}

2098 2099
Datum
float48mi(PG_FUNCTION_ARGS)
2100
{
2101 2102 2103
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
2104

2105 2106 2107
	result = arg1 - arg2;
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2108 2109
}

2110 2111
Datum
float48mul(PG_FUNCTION_ARGS)
2112
{
2113 2114 2115
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
2116

2117 2118 2119
	result = arg1 * arg2;
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2120 2121
}

2122 2123
Datum
float48div(PG_FUNCTION_ARGS)
2124
{
2125 2126 2127
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
	float8		result;
2128

2129
	if (arg2 == 0.0)
2130 2131 2132
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
2133

2134 2135 2136
	result = arg1 / arg2;
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2137 2138 2139
}

/*
2140 2141 2142 2143
 *		float84pl		- returns arg1 + arg2
 *		float84mi		- returns arg1 - arg2
 *		float84mul		- returns arg1 * arg2
 *		float84div		- returns arg1 / arg2
2144
 */
2145 2146
Datum
float84pl(PG_FUNCTION_ARGS)
2147
{
2148 2149 2150
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	float8		result;
2151

2152
	result = arg1 + arg2;
2153

2154 2155
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2156 2157
}

2158 2159
Datum
float84mi(PG_FUNCTION_ARGS)
2160
{
2161 2162 2163
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	float8		result;
2164

2165
	result = arg1 - arg2;
2166

2167 2168
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2169 2170
}

2171 2172
Datum
float84mul(PG_FUNCTION_ARGS)
2173
{
2174 2175 2176
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	float8		result;
2177

2178
	result = arg1 * arg2;
2179

2180 2181
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2182 2183
}

2184 2185
Datum
float84div(PG_FUNCTION_ARGS)
2186
{
2187 2188 2189
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
	float8		result;
2190

2191
	if (arg2 == 0.0)
2192 2193 2194
		ereport(ERROR,
				(errcode(ERRCODE_DIVISION_BY_ZERO),
				 errmsg("division by zero")));
2195

2196
	result = arg1 / arg2;
2197

2198 2199
	CheckFloat8Val(result);
	PG_RETURN_FLOAT8(result);
2200 2201 2202
}

/*
2203 2204 2205
 *		====================
 *		COMPARISON OPERATORS
 *		====================
2206 2207 2208
 */

/*
2209
 *		float48{eq,ne,lt,le,gt,ge}		- float4/float8 comparison operations
2210
 */
2211 2212
Datum
float48eq(PG_FUNCTION_ARGS)
2213
{
2214 2215
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
2216

2217
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);
2218 2219
}

2220 2221
Datum
float48ne(PG_FUNCTION_ARGS)
2222
{
2223 2224
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
2225

2226
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);
2227 2228
}

2229 2230
Datum
float48lt(PG_FUNCTION_ARGS)
2231
{
2232 2233
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
2234

2235
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);
2236 2237
}

2238 2239
Datum
float48le(PG_FUNCTION_ARGS)
2240
{
2241 2242
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
2243

2244
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);
2245 2246
}

2247 2248
Datum
float48gt(PG_FUNCTION_ARGS)
2249
{
2250 2251
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
2252

2253
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);
2254 2255
}

2256 2257
Datum
float48ge(PG_FUNCTION_ARGS)
2258
{
2259 2260
	float4		arg1 = PG_GETARG_FLOAT4(0);
	float8		arg2 = PG_GETARG_FLOAT8(1);
2261

2262
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);
2263 2264 2265
}

/*
2266
 *		float84{eq,ne,lt,le,gt,ge}		- float8/float4 comparison operations
2267
 */
2268 2269
Datum
float84eq(PG_FUNCTION_ARGS)
2270
{
2271 2272
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
2273

2274
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);
2275 2276
}

2277 2278
Datum
float84ne(PG_FUNCTION_ARGS)
2279
{
2280 2281
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
2282

2283
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);
2284 2285
}

2286 2287
Datum
float84lt(PG_FUNCTION_ARGS)
2288
{
2289 2290
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
2291

2292
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);
2293 2294
}

2295 2296
Datum
float84le(PG_FUNCTION_ARGS)
2297
{
2298 2299
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
2300

2301
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);
2302 2303
}

2304 2305
Datum
float84gt(PG_FUNCTION_ARGS)
2306
{
2307 2308
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
2309

2310
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);
2311 2312
}

2313 2314
Datum
float84ge(PG_FUNCTION_ARGS)
2315
{
2316 2317
	float8		arg1 = PG_GETARG_FLOAT8(0);
	float4		arg2 = PG_GETARG_FLOAT4(1);
2318

2319
	PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);
2320 2321 2322 2323
}

/* ========== PRIVATE ROUTINES ========== */

2324
#ifndef HAVE_CBRT
2325 2326
static double
cbrt(double x)
2327
{
2328 2329
	int			isneg = (x < 0.0);
	double		tmpres = pow(fabs(x), (double) 1.0 / (double) 3.0);
2330

2331
	return isneg ? -tmpres : tmpres;
2332
}
2333

2334
#endif   /* !HAVE_CBRT */