📄 float.c
字号:
/* * dtoi4 - converts a float8 number to an int4 number */int32dtoi4(float64 num){ int32 result; if (!PointerIsValid(num)) elog(ERROR, "dtoi4: unable to convert null", NULL); if ((*num < INT_MIN) || (*num > INT_MAX)) elog(ERROR, "dtoi4: integer out of range", NULL); result = rint(*num); return result;}/* * dtoi2 - converts a float8 number to an int2 number */int16dtoi2(float64 num){ int16 result; if (!PointerIsValid(num)) elog(ERROR, "dtoi2: unable to convert null", NULL); if ((*num < SHRT_MIN) || (*num > SHRT_MAX)) elog(ERROR, "dtoi2: integer out of range", NULL); result = rint(*num); return result;}/* * i4tod - converts an int4 number to a float8 number */float64i4tod(int32 num){ float64 result; result = (float64) palloc(sizeof(float64data)); *result = num; return result;}/* * i2tod - converts an int2 number to a float8 number */float64i2tod(int16 num){ float64 result; result = (float64) palloc(sizeof(float64data)); *result = num; return result;}/* * ftoi4 - converts a float8 number to an int4 number */int32ftoi4(float32 num){ int32 result; if (!PointerIsValid(num)) elog(ERROR, "ftoi4: unable to convert null", NULL); if ((*num < INT_MIN) || (*num > INT_MAX)) elog(ERROR, "ftoi4: integer out of range", NULL); result = rint(*num); return result;}/* * ftoi2 - converts a float8 number to an int2 number */int16ftoi2(float32 num){ int16 result; if (!PointerIsValid(num)) elog(ERROR, "ftoi2: unable to convert null", NULL); if ((*num < SHRT_MIN) || (*num > SHRT_MAX)) elog(ERROR, "ftoi2: integer out of range", NULL); result = rint(*num); return result;}/* * i4tof - converts an int4 number to a float8 number */float32i4tof(int32 num){ float32 result; result = (float32) palloc(sizeof(float32data)); *result = num; return result;}/* * i2tof - converts an int2 number to a float8 number */float32i2tof(int16 num){ float32 result; result = (float32) palloc(sizeof(float32data)); *result = num; return result;}/* * 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 */float64text_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 */float32text_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() *//* * ======================= * RANDOM FLOAT8 OPERATORS * ======================= *//* * dround - returns a pointer to ROUND(arg1) */float64dround(float64 arg1){ float64 result; double tmp; if (!arg1) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp = *arg1; *result = (float64data) rint(tmp); return result;}/* * 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 */float64dtrunc(float64 arg1){ float64 result; double tmp; if (!arg1) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp = *arg1; if (*arg1 >= 0) *result = (float64data) floor(tmp); else *result = (float64data) -(floor(-tmp)); return result;}/* * dsqrt - returns a pointer to square root of arg1 */float64dsqrt(float64 arg1){ float64 result; double tmp; if (!arg1) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp = *arg1; *result = (float64data) sqrt(tmp); return result;}/* * dcbrt - returns a pointer to cube root of arg1 */float64dcbrt(float64 arg1){ float64 result; double tmp; if (!arg1) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp = *arg1; *result = (float64data) cbrt(tmp); return result;}/* * dpow - returns a pointer to pow(arg1,arg2) */float64dpow(float64 arg1, float64 arg2){ float64 result; double tmp1, tmp2; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp1 = *arg1; tmp2 = *arg2;#ifndef finite errno = 0;#endif *result = (float64data) pow(tmp1, tmp2);#ifndef finite if (errno != 0) /* on some machines both EDOM & ERANGE can * occur */#else if (!finite(*result))#endif elog(ERROR, "pow() result is out of range"); CheckFloat8Val(*result); return result;}/* * dexp - returns a pointer to the exponential function of arg1 */float64dexp(float64 arg1){ float64 result; double tmp; if (!arg1) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp = *arg1;#ifndef finite errno = 0;#endif *result = (float64data) exp(tmp);#ifndef finite if (errno == ERANGE)#else /* infinity implies overflow, zero implies underflow */ if (!finite(*result) || *result == 0.0)#endif elog(ERROR, "exp() result is out of range"); CheckFloat8Val(*result); return result;}/* * dlog1 - returns a pointer to the natural logarithm of arg1 * ("dlog" is already a logging routine...) */float64dlog1(float64 arg1){ float64 result; double tmp; if (!arg1) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); tmp = *arg1; if (tmp == 0.0) elog(ERROR, "can't take log of zero"); if (tmp < 0) elog(ERROR, "can't take log of a negative number"); *result = (float64data) log(tmp); CheckFloat8Val(*result); return result;}/* * ==================== * ARITHMETIC OPERATORS * ==================== *//* * 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 */float64float48pl(float32 arg1, float64 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); *result = *arg1 + *arg2; CheckFloat8Val(*result); return result;}float64float48mi(float32 arg1, float64 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); *result = *arg1 - *arg2; CheckFloat8Val(*result); return result;}float64float48mul(float32 arg1, float64 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); *result = *arg1 * *arg2; CheckFloat8Val(*result); return result;}float64float48div(float32 arg1, float64 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); if (*arg2 == 0.0) elog(ERROR, "float48div: divide by zero"); *result = *arg1 / *arg2; CheckFloat8Val(*result); return result;}/* * 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 */float64float84pl(float64 arg1, float32 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); *result = *arg1 + *arg2; CheckFloat8Val(*result); return result;}float64float84mi(float64 arg1, float32 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); *result = *arg1 - *arg2; CheckFloat8Val(*result); return result;}float64float84mul(float64 arg1, float32 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); *result = *arg1 * *arg2; CheckFloat8Val(*result); return result;}float64float84div(float64 arg1, float32 arg2){ float64 result; if (!arg1 || !arg2) return (float64) NULL; result = (float64) palloc(sizeof(float64data)); if (*arg2 == 0.0) elog(ERROR, "float48div: divide by zero"); *result = *arg1 / *arg2; CheckFloat8Val(*result); return result;}/* * ==================== * COMPARISON OPERATORS * ==================== *//* * float48{eq,ne,lt,le,gt,ge} - float4/float8 comparison operations */boolfloat48eq(float32 arg1, float64 arg2){ if (!arg1 || !arg2) return 0; return *arg1 == (float) *arg2;}boolfloat48ne(float32 arg1, float64 arg2){ if (!arg1 || !arg2) return 0; return *arg1 != (float) *arg2;}boolfloat48lt(float32 arg1, float64 arg2){ if (!arg1 || !arg2) return 0; return *arg1 < (float) *arg2;}boolfloat48le(float32 arg1, float64 arg2){ if (!arg1 || !arg2) return 0; return *arg1 <= (float) *arg2;}boolfloat48gt(float32 arg1, float64 arg2){ if (!arg1 || !arg2) return 0; return *arg1 > (float) *arg2;}boolfloat48ge(float32 arg1, float64 arg2){ if (!arg1 || !arg2) return 0; return *arg1 >= (float) *arg2;}/* * float84{eq,ne,lt,le,gt,ge} - float4/float8 comparison operations */boolfloat84eq(float64 arg1, float32 arg2){ if (!arg1 || !arg2) return 0; return (float) *arg1 == *arg2;}boolfloat84ne(float64 arg1, float32 arg2){ if (!arg1 || !arg2) return 0; return (float) *arg1 != *arg2;}boolfloat84lt(float64 arg1, float32 arg2){ if (!arg1 || !arg2) return 0; return (float) *arg1 < *arg2;}boolfloat84le(float64 arg1, float32 arg2){ if (!arg1 || !arg2) return 0; return (float) *arg1 <= *arg2;}boolfloat84gt(float64 arg1, float32 arg2){ if (!arg1 || !arg2) return 0; return (float) *arg1 > *arg2;}boolfloat84ge(float64 arg1, float32 arg2){ if (!arg1 || !arg2) return 0; return (float) *arg1 >= *arg2;}/* ========== PRIVATE ROUTINES ========== *//* From "fdlibm" @ netlib.att.com */#ifndef HAVE_RINT/* @(#)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 * software is freely granted, provided that this notice * is preserved. * ==================================================== *//* * rint(x) * Return x rounded to integral value according to the prevailing * rounding mode. * Method: * Using floating addition. * Exception: * Inexact flag raised if x not equal to rint(x). */#ifdef __STDC__static const double#elsestatic double#endif one = 1.0, TWO52[2] = { 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ -4.50359962737049600000e+15,/* 0xC3300000, 0x00000000 */};#ifdef __STDC__static doublerint(double x)#elsestatic doublerint(x)double x;#endif{ int i0, n0, j0, sx; unsigned i, i1; double w, t; 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)); } *(n0 + (int *) &x) = i0; *(1 - n0 + (int *) &x) = i1; w = TWO52[sx] + x; return w - TWO52[sx];}#endif /* !HAVE_RINT */#ifndef HAVE_CBRTstaticdoublecbrt(x)double x;{ int isneg = (x < 0.0); double tmpres = pow(fabs(x), (double) 1.0 / (double) 3.0); return isneg ? -tmpres : tmpres;}#endif /* !HAVE_CBRT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -