float.c 27.2 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * float.c--
4
 *	  Functions for the built-in floating-point types.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.36 1999/01/10 17:13:06 thomas Exp $
11 12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
/*
 * OLD COMMENTS
16 17 18 19 20 21 22 23 24 25 26 27
 *		Basic float4 ops:
 *		 float4in, float4out, float4abs, float4um
 *		Basic float8 ops:
 *		 float8in, float8inAd, float8out, float8outAd, float8abs, float8um
 *		Arithmetic operators:
 *		 float4pl, float4mi, float4mul, float4div
 *		 float8pl, float8mi, float8mul, float8div
 *		Comparison operators:
 *		 float4eq, float4ne, float4lt, float4le, float4gt, float4ge
 *		 float8eq, float8ne, float8lt, float8le, float8gt, float8ge
 *		Conversion routines:
 *		 ftod, dtof, i4tod, dtoi4, i2tod, dtoi2, itof, ftoi, i2tof, ftoi2
28
 *
29 30 31 32 33 34 35 36
 *		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
37
 *
38 39
 *		(You can do the arithmetic and comparison stuff using conversion
 *		 routines, but then you pay the overhead of converting...)
40 41
 *
 * XXX GLUESOME STUFF. FIX IT! -AY '94
42
 *
43 44 45 46
 *		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
47
 */
48
#include <stdio.h>				/* for sprintf() */
49 50 51 52 53
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>

54
#include <float.h>				/* faked on sunos4 */
55 56 57 58

#include <math.h>

#include "postgres.h"
59
#ifdef HAVE_LIMITS_H
60
#include <limits.h>
61
#endif
62
#include "fmgr.h"
63
#include "utils/builtins.h"		/* for ftod() prototype */
64 65 66
#include "utils/palloc.h"


67 68 69 70 71 72 73
#ifndef SHRT_MAX
#define SHRT_MAX 32767
#endif
#ifndef SHRT_MIN
#define SHRT_MIN (-32768)
#endif

74 75
#define FORMAT			'g'		/* use "g" output format as standard
								 * format */
76
/* not sure what the following should be, but better to make it over-sufficient */
77
#define MAXFLOATWIDTH	64
78 79 80
#define MAXDOUBLEWIDTH	128

#if !(NeXT && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_2)
81
 /* NS3.3 has conflicting declarations of these in <math.h> */
82 83

#ifndef atof
84
extern double atof(const char *p);
85

86 87
#endif

88
#ifndef HAVE_CBRT
89
#define cbrt my_cbrt
90
static double cbrt(double x);
91 92 93

#else
#if !defined(nextstep)
94
extern double cbrt(double x);
95 96 97

#endif
#endif
98 99

#ifndef HAVE_RINT
100
#define rint my_rint
101
static double rint(double x);
102 103

#else
104
extern double rint(double x);
105 106

#endif
107

108
extern int	isinf(double x);
109

110
#endif
111
/* ========== USER I/O ROUTINES ========== */
112 113 114 115 116 117 118 119


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

/*
120 121 122
 * if FLOAT8_MIN and FLOAT8_MAX are the limits of the range a
 * double can store, then how are we ever going to wind up
 * with something stored in a double that is outside those
123
 * limits?	(and similarly for FLOAT4_{MIN,MAX}/float.)
124 125 126 127 128
 * doesn't make sense to me, and it causes a
 * floating point exception on linuxalpha, so UNSAFE_FLOATS
 * it is.
 * (maybe someone wanted to allow for values other than DBL_MIN/
 * DBL_MAX for FLOAT8_MIN/FLOAT8_MAX?)
129 130 131
 *								--djm 12/12/96
 * according to Richard Henderson this is a known bug in gcc on
 * the Alpha.  might as well leave the workaround in
132
 * until the distributions are updated.
133
 *								--djm 12/16/96
134
 */
135
#if ( defined(linux) && defined(__alpha) ) && !defined(UNSAFE_FLOATS)
136 137 138
#define UNSAFE_FLOATS
#endif

139 140 141
/*
   check to see if a float4 val is outside of
   the FLOAT4_MIN, FLOAT4_MAX bounds.
142

143 144
   raise an elog warning if it is
*/
145 146
static void
CheckFloat4Val(double val)
147
{
148 149 150 151 152

	/*
	 * defining unsafe floats's will make float4 and float8 ops faster at
	 * the cost of safety, of course!
	 */
153
#ifdef UNSAFE_FLOATS
154
	return;
155
#else
156
	if (fabs(val) > FLOAT4_MAX)
157
		elog(ERROR, "Bad float4 input format -- overflow");
158
	if (val != 0.0 && fabs(val) < FLOAT4_MIN)
159
		elog(ERROR, "Bad float4 input format -- underflow");
160
	return;
161
#endif	 /* UNSAFE_FLOATS */
162 163 164 165 166
}

/*
   check to see if a float8 val is outside of
   the FLOAT8_MIN, FLOAT8_MAX bounds.
167

168 169
   raise an elog warning if it is
*/
170 171
void
CheckFloat8Val(double val)
172
{
173 174 175 176 177

	/*
	 * defining unsafe floats's will make float4 and float8 ops faster at
	 * the cost of safety, of course!
	 */
178
#ifdef UNSAFE_FLOATS
179
	return;
180
#else
181
	if (fabs(val) > FLOAT8_MAX)
182
		elog(ERROR, "Bad float8 input format -- overflow");
183
	if (val != 0.0 && fabs(val) < FLOAT8_MIN)
184
		elog(ERROR, "Bad float8 input format -- underflow");
185
	return;
186
#endif	 /* UNSAFE_FLOATS */
187 188 189
}

/*
190 191 192 193 194
 *		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.
195
 */
196 197 198
float32
float4in(char *num)
{
199 200 201
	float32		result = (float32) palloc(sizeof(float32data));
	double		val;
	char	   *endptr;
202 203 204 205

	errno = 0;
	val = strtod(num, &endptr);
	if (*endptr != '\0' || errno == ERANGE)
206
		elog(ERROR, "Bad float4 input format '%s'", num);
207 208 209 210 211 212 213 214 215 216

	/*
	 * if we get here, we have a legal double, still need to check to see
	 * if it's a legal float
	 */

	CheckFloat4Val(val);

	*result = val;
	return result;
217 218 219
}

/*
220 221
 *		float4out		- converts a float4 number to a string
 *						  using a standard output format
222
 */
223
char *
224
float4out(float32 num)
225
{
226
	char	   *ascii = (char *) palloc(MAXFLOATWIDTH + 1);
227 228 229 230 231

	if (!num)
		return strcpy(ascii, "(null)");

	sprintf(ascii, "%.*g", FLT_DIG, *num);
232
	return ascii;
233 234 235 236
}


/*
237 238 239 240 241
 *		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.
242
 */
243 244
float64
float8in(char *num)
245
{
246 247 248
	float64		result = (float64) palloc(sizeof(float64data));
	double		val;
	char	   *endptr;
249 250 251

	errno = 0;
	val = strtod(num, &endptr);
252 253 254 255 256 257 258 259 260 261 262
	if (*endptr != '\0')
	{
		if (strcasecmp(num, "NaN") == 0)
			val = NAN;
		else if (strcasecmp(num, "Infinity") == 0)
			val = HUGE_VAL;
		else if (errno == ERANGE)
			elog(ERROR, "Input '%s' is out of range for float8", num);
		else
			elog(ERROR, "Bad float8 input format '%s'", num);
	}
263 264 265

	CheckFloat8Val(val);
	*result = val;
266
	return result;
267 268 269 270
}


/*
271 272
 *		float8out		- converts float8 number to a string
 *						  using a standard output format
273
 */
274
char *
275
float8out(float64 num)
276
{
277
	char	   *ascii = (char *) palloc(MAXDOUBLEWIDTH + 1);
278 279 280

	if (!num)
		return strcpy(ascii, "(null)");
281

282 283 284 285
	if (isnan(*num))
		return strcpy(ascii, "NaN");
	if (isinf(*num))
		return strcpy(ascii, "Infinity");
286

287
	sprintf(ascii, "%.*g", DBL_DIG, *num);
288
	return ascii;
289 290 291 292 293 294
}

/* ========== PUBLIC ROUTINES ========== */


/*
295 296 297
 *		======================
 *		FLOAT4 BASE OPERATIONS
 *		======================
298 299 300
 */

/*
301
 *		float4abs		- returns a pointer to |arg1| (absolute value)
302
 */
303 304
float32
float4abs(float32 arg1)
305
{
306 307
	float32		result;
	double		val;
308

309 310
	if (!arg1)
		return (float32) NULL;
311

312 313 314 315 316 317
	val = fabs(*arg1);

	CheckFloat4Val(val);

	result = (float32) palloc(sizeof(float32data));
	*result = val;
318
	return result;
319 320 321
}

/*
322
 *		float4um		- returns a pointer to -arg1 (unary minus)
323
 */
324 325
float32
float4um(float32 arg1)
326
{
327 328
	float32		result;
	double		val;
329 330 331 332 333 334

	if (!arg1)
		return (float32) NULL;

	val = ((*arg1 != 0) ? -(*arg1) : *arg1);
	CheckFloat4Val(val);
335

336 337
	result = (float32) palloc(sizeof(float32data));
	*result = val;
338
	return result;
339 340
}

341 342
float32
float4larger(float32 arg1, float32 arg2)
343
{
344
	float32		result;
345 346 347 348 349 350 351 352

	if (!arg1 || !arg2)
		return (float32) NULL;

	result = (float32) palloc(sizeof(float32data));

	*result = ((*arg1 > *arg2) ? *arg1 : *arg2);
	return result;
353 354
}

355 356
float32
float4smaller(float32 arg1, float32 arg2)
357
{
358
	float32		result;
359 360 361 362 363 364 365 366

	if (!arg1 || !arg2)
		return (float32) NULL;

	result = (float32) palloc(sizeof(float32data));

	*result = ((*arg1 > *arg2) ? *arg2 : *arg1);
	return result;
367 368 369
}

/*
370 371 372
 *		======================
 *		FLOAT8 BASE OPERATIONS
 *		======================
373 374 375
 */

/*
376
 *		float8abs		- returns a pointer to |arg1| (absolute value)
377
 */
378 379
float64
float8abs(float64 arg1)
380
{
381 382
	float64		result;
	double		val;
383 384 385 386 387 388 389 390 391

	if (!arg1)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	val = fabs(*arg1);
	CheckFloat8Val(val);
	*result = val;
392
	return result;
393 394 395 396
}


/*
397
 *		float8um		- returns a pointer to -arg1 (unary minus)
398
 */
399 400
float64
float8um(float64 arg1)
401
{
402 403
	float64		result;
	double		val;
404 405 406 407 408

	if (!arg1)
		return (float64) NULL;

	val = ((*arg1 != 0) ? -(*arg1) : *arg1);
409

410 411 412
	CheckFloat8Val(val);
	result = (float64) palloc(sizeof(float64data));
	*result = val;
413
	return result;
414 415
}

416 417
float64
float8larger(float64 arg1, float64 arg2)
418
{
419
	float64		result;
420 421 422 423 424 425 426 427

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = ((*arg1 > *arg2) ? *arg1 : *arg2);
	return result;
428 429
}

430 431
float64
float8smaller(float64 arg1, float64 arg2)
432
{
433
	float64		result;
434 435 436 437 438 439 440 441

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = ((*arg1 > *arg2) ? *arg2 : *arg1);
	return result;
442 443 444 445
}


/*
446 447 448
 *		====================
 *		ARITHMETIC OPERATORS
 *		====================
449 450 451
 */

/*
452 453 454 455 456
 *		float4pl		- returns a pointer to arg1 + arg2
 *		float4mi		- returns a pointer to arg1 - arg2
 *		float4mul		- returns a pointer to arg1 * arg2
 *		float4div		- returns a pointer to arg1 / arg2
 *		float4inc		- returns a poniter to arg1 + 1.0
457
 */
458 459
float32
float4pl(float32 arg1, float32 arg2)
460
{
461 462
	float32		result;
	double		val;
463 464 465

	if (!arg1 || !arg2)
		return (float32) NULL;
466

467 468
	val = *arg1 + *arg2;
	CheckFloat4Val(val);
469

470 471 472
	result = (float32) palloc(sizeof(float32data));
	*result = val;

473
	return result;
474 475
}

476 477
float32
float4mi(float32 arg1, float32 arg2)
478
{
479 480
	float32		result;
	double		val;
481 482 483 484 485

	if (!arg1 || !arg2)
		return (float32) NULL;

	val = *arg1 - *arg2;
486

487 488 489
	CheckFloat4Val(val);
	result = (float32) palloc(sizeof(float32data));
	*result = val;
490
	return result;
491 492
}

493 494
float32
float4mul(float32 arg1, float32 arg2)
495
{
496 497
	float32		result;
	double		val;
498

499 500 501 502 503 504 505 506
	if (!arg1 || !arg2)
		return (float32) NULL;

	val = *arg1 * *arg2;

	CheckFloat4Val(val);
	result = (float32) palloc(sizeof(float32data));
	*result = val;
507
	return result;
508 509
}

510 511
float32
float4div(float32 arg1, float32 arg2)
512
{
513 514
	float32		result;
	double		val;
515 516 517 518 519

	if (!arg1 || !arg2)
		return (float32) NULL;

	if (*arg2 == 0.0)
520
		elog(ERROR, "float4div: divide by zero error");
521

522 523 524 525 526
	val = *arg1 / *arg2;

	CheckFloat4Val(val);
	result = (float32) palloc(sizeof(float32data));
	*result = *arg1 / *arg2;
527
	return result;
528 529
}

530 531
float32
float4inc(float32 arg1)
532
{
533
	double		val;
534 535 536

	if (!arg1)
		return (float32) NULL;
537

538 539 540 541
	val = *arg1 + (float32data) 1.0;
	CheckFloat4Val(val);
	*arg1 = val;
	return arg1;
542 543 544
}

/*
545 546 547 548 549
 *		float8pl		- returns a pointer to arg1 + arg2
 *		float8mi		- returns a pointer to arg1 - arg2
 *		float8mul		- returns a pointer to arg1 * arg2
 *		float8div		- returns a pointer to arg1 / arg2
 *		float8inc		- returns a pointer to arg1 + 1.0
550
 */
551 552 553
float64
float8pl(float64 arg1, float64 arg2)
{
554 555
	float64		result;
	double		val;
556 557 558 559 560 561 562 563 564

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	val = *arg1 + *arg2;
	CheckFloat8Val(val);
	*result = val;
565
	return result;
566 567 568 569 570
}

float64
float8mi(float64 arg1, float64 arg2)
{
571 572
	float64		result;
	double		val;
573 574 575 576 577 578 579 580 581

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	val = *arg1 - *arg2;
	CheckFloat8Val(val);
	*result = val;
582
	return result;
583 584 585 586 587
}

float64
float8mul(float64 arg1, float64 arg2)
{
588 589
	float64		result;
	double		val;
590 591 592 593 594 595 596 597 598

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	val = *arg1 * *arg2;
	CheckFloat8Val(val);
	*result = val;
599
	return result;
600 601 602 603 604
}

float64
float8div(float64 arg1, float64 arg2)
{
605 606
	float64		result;
	double		val;
607 608 609 610 611 612 613

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	if (*arg2 == 0.0)
614
		elog(ERROR, "float8div: divide by zero error");
615 616 617 618

	val = *arg1 / *arg2;
	CheckFloat8Val(val);
	*result = val;
619
	return result;
620 621 622 623 624
}

float64
float8inc(float64 arg1)
{
625
	double		val;
626 627 628 629 630 631 632

	if (!arg1)
		return (float64) NULL;

	val = *arg1 + (float64data) 1.0;
	CheckFloat8Val(val);
	*arg1 = val;
633
	return arg1;
634 635 636 637
}


/*
638 639 640
 *		====================
 *		COMPARISON OPERATORS
 *		====================
641 642 643
 */

/*
644
 *		float4{eq,ne,lt,le,gt,ge}		- float4/float4 comparison operations
645
 */
646 647
bool
float4eq(float32 arg1, float32 arg2)
648
{
649 650
	if (!arg1 || !arg2)
		return 0;
651

652
	return *arg1 == *arg2;
653 654
}

655 656
bool
float4ne(float32 arg1, float32 arg2)
657
{
658 659 660
	if (!arg1 || !arg2)
		return 0;

661
	return *arg1 != *arg2;
662 663
}

664 665
bool
float4lt(float32 arg1, float32 arg2)
666
{
667 668 669
	if (!arg1 || !arg2)
		return 0;

670
	return *arg1 < *arg2;
671 672
}

673 674
bool
float4le(float32 arg1, float32 arg2)
675
{
676 677 678
	if (!arg1 || !arg2)
		return 0;

679
	return *arg1 <= *arg2;
680 681
}

682 683
bool
float4gt(float32 arg1, float32 arg2)
684
{
685 686 687
	if (!arg1 || !arg2)
		return 0;

688
	return *arg1 > *arg2;
689 690
}

691 692
bool
float4ge(float32 arg1, float32 arg2)
693
{
694 695 696
	if (!arg1 || !arg2)
		return 0;

697
	return *arg1 >= *arg2;
698 699 700
}

/*
701
 *		float8{eq,ne,lt,le,gt,ge}		- float8/float8 comparison operations
702
 */
703 704
bool
float8eq(float64 arg1, float64 arg2)
705
{
706 707 708
	if (!arg1 || !arg2)
		return 0;

709
	return *arg1 == *arg2;
710 711
}

712 713
bool
float8ne(float64 arg1, float64 arg2)
714
{
715 716 717
	if (!arg1 || !arg2)
		return 0;

718
	return *arg1 != *arg2;
719 720
}

721 722
bool
float8lt(float64 arg1, float64 arg2)
723
{
724 725 726
	if (!arg1 || !arg2)
		return 0;

727
	return *arg1 < *arg2;
728 729
}

730 731
bool
float8le(float64 arg1, float64 arg2)
732
{
733 734 735
	if (!arg1 || !arg2)
		return 0;

736
	return *arg1 <= *arg2;
737 738
}

739 740
bool
float8gt(float64 arg1, float64 arg2)
741
{
742 743 744
	if (!arg1 || !arg2)
		return 0;

745
	return *arg1 > *arg2;
746 747
}

748 749
bool
float8ge(float64 arg1, float64 arg2)
750
{
751 752 753
	if (!arg1 || !arg2)
		return 0;

754
	return *arg1 >= *arg2;
755 756 757 758
}


/*
759 760 761
 *		===================
 *		CONVERSION ROUTINES
 *		===================
762 763 764
 */

/*
765
 *		ftod			- converts a float4 number to a float8 number
766
 */
767 768
float64
ftod(float32 num)
769
{
770
	float64		result;
771 772 773 774 775

	if (!num)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));
776

777
	*result = *num;
778
	return result;
779 780 781 782
}


/*
783
 *		dtof			- converts a float8 number to a float4 number
784
 */
785 786
float32
dtof(float64 num)
787
{
788
	float32		result;
789

790 791
	if (!num)
		return (float32) NULL;
792

793
	CheckFloat4Val(*num);
794

795 796 797
	result = (float32) palloc(sizeof(float32data));

	*result = *num;
798
	return result;
799 800 801 802
}


/*
803
 *		dtoi4			- converts a float8 number to an int4 number
804
 */
805 806
int32
dtoi4(float64 num)
807
{
808
	int32		result;
809

810
	if (!PointerIsValid(num))
811
		elog(ERROR, "dtoi4: unable to convert null", NULL);
812

813
	if ((*num < INT_MIN) || (*num > INT_MAX))
814
		elog(ERROR, "dtoi4: integer out of range", NULL);
815

816
	result = rint(*num);
817
	return result;
818 819 820 821
}


/*
822
 *		dtoi2			- converts a float8 number to an int2 number
823
 */
824 825
int16
dtoi2(float64 num)
826
{
827
	int16		result;
828

829
	if (!PointerIsValid(num))
830
		elog(ERROR, "dtoi2: unable to convert null", NULL);
831

832
	if ((*num < SHRT_MIN) || (*num > SHRT_MAX))
833
		elog(ERROR, "dtoi2: integer out of range", NULL);
834

835
	result = rint(*num);
836
	return result;
837 838 839 840
}


/*
841
 *		i4tod			- converts an int4 number to a float8 number
842
 */
843 844
float64
i4tod(int32 num)
845
{
846
	float64		result;
847 848 849 850

	result = (float64) palloc(sizeof(float64data));

	*result = num;
851
	return result;
852 853 854 855
}


/*
856
 *		i2tod			- converts an int2 number to a float8 number
857
 */
858 859
float64
i2tod(int16 num)
860
{
861
	float64		result;
862 863 864 865

	result = (float64) palloc(sizeof(float64data));

	*result = num;
866
	return result;
867 868 869 870
}


/*
871
 *		ftoi4			- converts a float8 number to an int4 number
872
 */
873 874
int32
ftoi4(float32 num)
875
{
876
	int32		result;
877

878
	if (!PointerIsValid(num))
879
		elog(ERROR, "ftoi4: unable to convert null", NULL);
880

881
	if ((*num < INT_MIN) || (*num > INT_MAX))
882
		elog(ERROR, "ftoi4: integer out of range", NULL);
883 884

	result = rint(*num);
885
	return result;
886 887 888 889
}


/*
890
 *		ftoi2			- converts a float8 number to an int2 number
891
 */
892 893
int16
ftoi2(float32 num)
894
{
895
	int16		result;
896

897
	if (!PointerIsValid(num))
898
		elog(ERROR, "ftoi2: unable to convert null", NULL);
899

900
	if ((*num < SHRT_MIN) || (*num > SHRT_MAX))
901
		elog(ERROR, "ftoi2: integer out of range", NULL);
902

903
	result = rint(*num);
904
	return result;
905 906 907 908
}


/*
909
 *		i4tof			- converts an int4 number to a float8 number
910
 */
911 912
float32
i4tof(int32 num)
913
{
914
	float32		result;
915 916 917 918

	result = (float32) palloc(sizeof(float32data));

	*result = num;
919
	return result;
920 921 922 923
}


/*
924
 *		i2tof			- converts an int2 number to a float8 number
925
 */
926 927
float32
i2tof(int16 num)
928
{
929
	float32		result;
930 931 932 933

	result = (float32) palloc(sizeof(float32data));

	*result = num;
934
	return result;
935 936 937
}


938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
/*
 *		float8_text		- converts a float8 number to a text string
 */
text *
float8_text(float64 num)
{
	text   *result;
	int		len;
	char   *str;

	str = float8out(num);
	len = (strlen(str)+VARHDRSZ);

	result = palloc(len);

	VARSIZE(result) = len;
	memmove(VARDATA(result), str, (len - VARHDRSZ));

	pfree(str);
	return result;
}	/* float8_text() */


/*
 *		text_float8		- converts a text string to a float8 number
 */
float64
text_float8(text *string)
{
	float64	result;
	int		len;
	char   *str;

	len = (VARSIZE(string) - VARHDRSZ);
	str = palloc(len + 1);
	memmove(str, VARDATA(string), len);
	*(str + len) = '\0';

	result = float8in(str);
	pfree(str);

	return result;
}	/* text_float8() */


/*
 *		float4_text		- converts a float4 number to a text string
 */
text *
float4_text(float32 num)
{
	text   *result;
	int		len;
	char   *str;

	str = float4out(num);
	len = (strlen(str)+VARHDRSZ);

	result = palloc(len);

	VARSIZE(result) = len;
	memmove(VARDATA(result), str, (len - VARHDRSZ));

	pfree(str);
	return result;
}	/* float4_text() */


/*
 *		text_float4		- converts a text string to a float4 number
 */
float32
text_float4(text *string)
{
	float32	result;
	int		len;
	char   *str;

	len = (VARSIZE(string) - VARHDRSZ);
	str = palloc(len + 1);
	memmove(str, VARDATA(string), len);
	*(str + len) = '\0';

	result = float4in(str);
	pfree(str);

	return result;
}	/* text_float4() */


1028
/*
1029 1030 1031
 *		=======================
 *		RANDOM FLOAT8 OPERATORS
 *		=======================
1032 1033 1034
 */

/*
1035
 *		dround			- returns a pointer to	ROUND(arg1)
1036
 */
1037 1038
float64
dround(float64 arg1)
1039
{
1040 1041
	float64		result;
	double		tmp;
1042 1043 1044 1045 1046 1047 1048 1049

	if (!arg1)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	tmp = *arg1;
	*result = (float64data) rint(tmp);
1050
	return result;
1051 1052 1053 1054
}


/*
1055 1056 1057 1058 1059
 *		dtrunc			- returns a pointer to	truncation of arg1,
 *						  arg1 >= 0 ... the greatest integer as float8 less
 *										than or equal to arg1
 *						  arg1 < 0	... the greatest integer as float8 greater
 *										than or equal to arg1
1060
 */
1061 1062 1063
float64
dtrunc(float64 arg1)
{
1064 1065
	float64		result;
	double		tmp;
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

	if (!arg1)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	tmp = *arg1;
	if (*arg1 >= 0)
		*result = (float64data) floor(tmp);
	else
1076
		*result = (float64data) -(floor(-tmp));
1077
	return result;
1078 1079 1080 1081 1082
}


/*
 *		dsqrt			- returns a pointer to square root of arg1
1083
 */
1084 1085
float64
dsqrt(float64 arg1)
1086
{
1087 1088
	float64		result;
	double		tmp;
1089 1090 1091 1092 1093 1094 1095 1096

	if (!arg1)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	tmp = *arg1;
	*result = (float64data) sqrt(tmp);
1097
	return result;
1098 1099 1100 1101
}


/*
1102
 *		dcbrt			- returns a pointer to cube root of arg1
1103
 */
1104 1105
float64
dcbrt(float64 arg1)
1106
{
1107 1108
	float64		result;
	double		tmp;
1109 1110 1111 1112 1113 1114 1115 1116

	if (!arg1)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	tmp = *arg1;
	*result = (float64data) cbrt(tmp);
1117
	return result;
1118 1119 1120 1121
}


/*
1122
 *		dpow			- returns a pointer to pow(arg1,arg2)
1123
 */
1124 1125 1126
float64
dpow(float64 arg1, float64 arg2)
{
1127 1128 1129
	float64		result;
	double		tmp1,
				tmp2;
1130 1131 1132 1133 1134 1135 1136 1137

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	tmp1 = *arg1;
	tmp2 = *arg2;
1138
#ifndef finite
1139
	errno = 0;
1140
#endif
1141
	*result = (float64data) pow(tmp1, tmp2);
1142
#ifndef finite
1143
	if (errno != 0)			/* on some machines both EDOM & ERANGE can occur */
1144
#else
1145
	if (!finite(*result))
1146
#endif
1147
		elog(ERROR, "pow() result is out of range");
1148

1149
	CheckFloat8Val(*result);
1150
	return result;
1151 1152 1153 1154
}


/*
1155
 *		dexp			- returns a pointer to the exponential function of arg1
1156
 */
1157 1158 1159
float64
dexp(float64 arg1)
{
1160 1161
	float64		result;
	double		tmp;
1162 1163 1164 1165 1166 1167 1168

	if (!arg1)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	tmp = *arg1;
1169
#ifndef finite
1170
	errno = 0;
1171
#endif
1172
	*result = (float64data) exp(tmp);
1173
#ifndef finite
1174
	if (errno == ERANGE)
1175
#else
1176 1177
	/* infinity implies overflow, zero implies underflow */
	if (!finite(*result) || *result == 0.0)
1178
#endif
1179
		elog(ERROR, "exp() result is out of range");
1180

1181
	CheckFloat8Val(*result);
1182
	return result;
1183 1184 1185 1186
}


/*
1187 1188
 *		dlog1			- returns a pointer to the natural logarithm of arg1
 *						  ("dlog" is already a logging routine...)
1189
 */
1190 1191
float64
dlog1(float64 arg1)
1192
{
1193 1194
	float64		result;
	double		tmp;
1195 1196 1197

	if (!arg1)
		return (float64) NULL;
1198

1199 1200 1201 1202
	result = (float64) palloc(sizeof(float64data));

	tmp = *arg1;
	if (tmp == 0.0)
1203
		elog(ERROR, "can't take log of zero");
1204
	if (tmp < 0)
1205
		elog(ERROR, "can't take log of a negative number");
1206 1207 1208
	*result = (float64data) log(tmp);

	CheckFloat8Val(*result);
1209
	return result;
1210 1211 1212 1213
}


/*
1214 1215 1216
 *		====================
 *		ARITHMETIC OPERATORS
 *		====================
1217 1218 1219
 */

/*
1220 1221 1222 1223
 *		float48pl		- returns a pointer to arg1 + arg2
 *		float48mi		- returns a pointer to arg1 - arg2
 *		float48mul		- returns a pointer to arg1 * arg2
 *		float48div		- returns a pointer to arg1 / arg2
1224
 */
1225 1226 1227
float64
float48pl(float32 arg1, float64 arg2)
{
1228
	float64		result;
1229 1230 1231 1232 1233 1234 1235 1236

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = *arg1 + *arg2;
	CheckFloat8Val(*result);
1237
	return result;
1238 1239 1240 1241 1242
}

float64
float48mi(float32 arg1, float64 arg2)
{
1243
	float64		result;
1244 1245 1246 1247 1248 1249 1250 1251

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = *arg1 - *arg2;
	CheckFloat8Val(*result);
1252
	return result;
1253 1254 1255 1256 1257
}

float64
float48mul(float32 arg1, float64 arg2)
{
1258
	float64		result;
1259 1260 1261 1262 1263 1264 1265 1266

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = *arg1 * *arg2;
	CheckFloat8Val(*result);
1267
	return result;
1268 1269 1270 1271 1272
}

float64
float48div(float32 arg1, float64 arg2)
{
1273
	float64		result;
1274 1275 1276 1277 1278 1279 1280

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	if (*arg2 == 0.0)
1281
		elog(ERROR, "float48div: divide by zero");
1282 1283 1284

	*result = *arg1 / *arg2;
	CheckFloat8Val(*result);
1285
	return result;
1286 1287 1288
}

/*
1289 1290 1291 1292
 *		float84pl		- returns a pointer to arg1 + arg2
 *		float84mi		- returns a pointer to arg1 - arg2
 *		float84mul		- returns a pointer to arg1 * arg2
 *		float84div		- returns a pointer to arg1 / arg2
1293
 */
1294 1295 1296
float64
float84pl(float64 arg1, float32 arg2)
{
1297
	float64		result;
1298 1299 1300 1301 1302 1303 1304 1305

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = *arg1 + *arg2;
	CheckFloat8Val(*result);
1306
	return result;
1307 1308 1309 1310 1311
}

float64
float84mi(float64 arg1, float32 arg2)
{
1312
	float64		result;
1313 1314 1315 1316 1317 1318 1319 1320

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = *arg1 - *arg2;
	CheckFloat8Val(*result);
1321
	return result;
1322 1323 1324 1325 1326 1327
}

float64
float84mul(float64 arg1, float32 arg2)
{

1328
	float64		result;
1329 1330 1331 1332 1333 1334 1335 1336

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	*result = *arg1 * *arg2;
	CheckFloat8Val(*result);
1337
	return result;
1338 1339 1340 1341 1342
}

float64
float84div(float64 arg1, float32 arg2)
{
1343
	float64		result;
1344 1345 1346 1347 1348 1349 1350

	if (!arg1 || !arg2)
		return (float64) NULL;

	result = (float64) palloc(sizeof(float64data));

	if (*arg2 == 0.0)
1351
		elog(ERROR, "float48div: divide by zero");
1352 1353 1354

	*result = *arg1 / *arg2;
	CheckFloat8Val(*result);
1355
	return result;
1356 1357 1358
}

/*
1359 1360 1361
 *		====================
 *		COMPARISON OPERATORS
 *		====================
1362 1363 1364
 */

/*
1365
 *		float48{eq,ne,lt,le,gt,ge}		- float4/float8 comparison operations
1366
 */
1367 1368
bool
float48eq(float32 arg1, float64 arg2)
1369
{
1370 1371 1372
	if (!arg1 || !arg2)
		return 0;

1373
	return *arg1 == (float) *arg2;
1374 1375
}

1376 1377
bool
float48ne(float32 arg1, float64 arg2)
1378
{
1379 1380 1381
	if (!arg1 || !arg2)
		return 0;

1382
	return *arg1 != (float) *arg2;
1383 1384
}

1385 1386
bool
float48lt(float32 arg1, float64 arg2)
1387
{
1388 1389 1390
	if (!arg1 || !arg2)
		return 0;

1391
	return *arg1 < (float) *arg2;
1392 1393
}

1394 1395
bool
float48le(float32 arg1, float64 arg2)
1396
{
1397 1398 1399
	if (!arg1 || !arg2)
		return 0;

1400
	return *arg1 <= (float) *arg2;
1401 1402
}

1403 1404
bool
float48gt(float32 arg1, float64 arg2)
1405
{
1406 1407 1408
	if (!arg1 || !arg2)
		return 0;

1409
	return *arg1 > (float) *arg2;
1410 1411
}

1412 1413
bool
float48ge(float32 arg1, float64 arg2)
1414
{
1415 1416 1417
	if (!arg1 || !arg2)
		return 0;

1418
	return *arg1 >= (float) *arg2;
1419 1420 1421
}

/*
1422
 *		float84{eq,ne,lt,le,gt,ge}		- float4/float8 comparison operations
1423
 */
1424 1425
bool
float84eq(float64 arg1, float32 arg2)
1426
{
1427 1428 1429
	if (!arg1 || !arg2)
		return 0;

1430
	return (float) *arg1 == *arg2;
1431 1432
}

1433 1434
bool
float84ne(float64 arg1, float32 arg2)
1435
{
1436 1437 1438
	if (!arg1 || !arg2)
		return 0;

1439
	return (float) *arg1 != *arg2;
1440 1441
}

1442 1443
bool
float84lt(float64 arg1, float32 arg2)
1444
{
1445 1446 1447
	if (!arg1 || !arg2)
		return 0;

1448
	return (float) *arg1 < *arg2;
1449 1450
}

1451 1452
bool
float84le(float64 arg1, float32 arg2)
1453
{
1454 1455 1456
	if (!arg1 || !arg2)
		return 0;

1457
	return (float) *arg1 <= *arg2;
1458 1459
}

1460 1461
bool
float84gt(float64 arg1, float32 arg2)
1462
{
1463 1464 1465
	if (!arg1 || !arg2)
		return 0;

1466
	return (float) *arg1 > *arg2;
1467 1468
}

1469 1470
bool
float84ge(float64 arg1, float32 arg2)
1471
{
1472 1473 1474
	if (!arg1 || !arg2)
		return 0;

1475
	return (float) *arg1 >= *arg2;
1476 1477 1478 1479 1480 1481
}

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

/* From "fdlibm" @ netlib.att.com */

1482
#ifndef HAVE_RINT
1483 1484 1485 1486 1487 1488 1489 1490

/* @(#)s_rint.c 5.1 93/09/24 */
/*
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
1491
 * software is freely granted, provided that this notice
1492 1493 1494 1495 1496 1497 1498 1499 1500
 * is preserved.
 * ====================================================
 */

/*
 * rint(x)
 * Return x rounded to integral value according to the prevailing
 * rounding mode.
 * Method:
1501
 *		Using floating addition.
1502
 * Exception:
1503
 *		Inexact flag raised if x not equal to rint(x).
1504 1505 1506 1507 1508
 */

#ifdef __STDC__
static const double
#else
1509
static double
1510
#endif
1511 1512
			one = 1.0,
			TWO52[2] = {
1513
	4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
1514 1515
	-4.50359962737049600000e+15,/* 0xC3300000, 0x00000000 */
};
1516 1517

#ifdef __STDC__
1518 1519
static double
rint(double x)
1520
#else
1521 1522
static double
rint(x)
1523
double		x;
1524

1525 1526
#endif
{
1527 1528 1529 1530 1531 1532 1533 1534
	int			i0,
				n0,
				j0,
				sx;
	unsigned	i,
				i1;
	double		w,
				t;
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

	n0 = (*((int *) &one) >> 29) ^ 1;
	i0 = *(n0 + (int *) &x);
	sx = (i0 >> 31) & 1;
	i1 = *(1 - n0 + (int *) &x);
	j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
	if (j0 < 20)
	{
		if (j0 < 0)
		{
			if (((i0 & 0x7fffffff) | i1) == 0)
				return x;
			i1 |= (i0 & 0x0fffff);
			i0 &= 0xfffe0000;
			i0 |= ((i1 | -i1) >> 12) & 0x80000;
			*(n0 + (int *) &x) = i0;
			w = TWO52[sx] + x;
			t = w - TWO52[sx];
			i0 = *(n0 + (int *) &t);
			*(n0 + (int *) &t) = (i0 & 0x7fffffff) | (sx << 31);
			return t;
		}
		else
		{
			i = (0x000fffff) >> j0;
			if (((i0 & i) | i1) == 0)
				return x;		/* x is integral */
			i >>= 1;
			if (((i0 & i) | i1) != 0)
			{
				if (j0 == 19)
					i1 = 0x40000000;
				else
					i0 = (i0 & (~i)) | ((0x20000) >> j0);
			}
		}
	}
	else if (j0 > 51)
	{
		if (j0 == 0x400)
			return x + x;		/* inf or NaN */
		else
			return x;			/* x is integral */
	}
	else
	{
		i = ((unsigned) (0xffffffff)) >> (j0 - 20);
		if ((i1 & i) == 0)
			return x;			/* x is integral */
		i >>= 1;
		if ((i1 & i) != 0)
			i1 = (i1 & (~i)) | ((0x40000000) >> (j0 - 20));
1587
	}
1588 1589 1590 1591 1592 1593
	*(n0 + (int *) &x) = i0;
	*(1 - n0 + (int *) &x) = i1;
	w = TWO52[sx] + x;
	return w - TWO52[sx];
}

1594
#endif	 /* !HAVE_RINT */
1595

1596
#ifndef HAVE_CBRT
1597 1598

static
1599 1600
double
cbrt(x)
1601
double		x;
1602
{
1603 1604
	int			isneg = (x < 0.0);
	double		tmpres = pow(fabs(x), (double) 1.0 / (double) 3.0);
1605

1606
	return isneg ? -tmpres : tmpres;
1607 1608
}

1609
#endif	 /* !HAVE_CBRT */