📄 float.c
字号:
PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) == 0);}Datumfloat4ne(PG_FUNCTION_ARGS){ float4 arg1 = PG_GETARG_FLOAT4(0); float4 arg2 = PG_GETARG_FLOAT4(1); PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) != 0);}Datumfloat4lt(PG_FUNCTION_ARGS){ float4 arg1 = PG_GETARG_FLOAT4(0); float4 arg2 = PG_GETARG_FLOAT4(1); PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) < 0);}Datumfloat4le(PG_FUNCTION_ARGS){ float4 arg1 = PG_GETARG_FLOAT4(0); float4 arg2 = PG_GETARG_FLOAT4(1); PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) <= 0);}Datumfloat4gt(PG_FUNCTION_ARGS){ float4 arg1 = PG_GETARG_FLOAT4(0); float4 arg2 = PG_GETARG_FLOAT4(1); PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) > 0);}Datumfloat4ge(PG_FUNCTION_ARGS){ float4 arg1 = PG_GETARG_FLOAT4(0); float4 arg2 = PG_GETARG_FLOAT4(1); PG_RETURN_BOOL(float4_cmp_internal(arg1, arg2) >= 0);}Datumbtfloat4cmp(PG_FUNCTION_ARGS){ float4 arg1 = PG_GETARG_FLOAT4(0); float4 arg2 = PG_GETARG_FLOAT4(1); PG_RETURN_INT32(float4_cmp_internal(arg1, arg2));}/* * float8{eq,ne,lt,le,gt,ge} - float8/float8 comparison operations */static intfloat8_cmp_internal(float8 a, float8 b){ /* * 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. */ 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; else if (a < b) return -1; else return 0; }}Datumfloat8eq(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) == 0);}Datumfloat8ne(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) != 0);}Datumfloat8lt(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) < 0);}Datumfloat8le(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) <= 0);}Datumfloat8gt(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) > 0);}Datumfloat8ge(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_BOOL(float8_cmp_internal(arg1, arg2) >= 0);}Datumbtfloat8cmp(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); PG_RETURN_INT32(float8_cmp_internal(arg1, arg2));}/* * =================== * CONVERSION ROUTINES * =================== *//* * ftod - converts a float4 number to a float8 number */Datumftod(PG_FUNCTION_ARGS){ float4 num = PG_GETARG_FLOAT4(0); PG_RETURN_FLOAT8((float8) num);}/* * dtof - converts a float8 number to a float4 number */Datumdtof(PG_FUNCTION_ARGS){ float8 num = PG_GETARG_FLOAT8(0); CheckFloat4Val(num); PG_RETURN_FLOAT4((float4) num);}/* * dtoi4 - converts a float8 number to an int4 number */Datumdtoi4(PG_FUNCTION_ARGS){ float8 num = PG_GETARG_FLOAT8(0); int32 result; if ((num < INT_MIN) || (num > INT_MAX)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); result = (int32) rint(num); PG_RETURN_INT32(result);}/* * dtoi2 - converts a float8 number to an int2 number */Datumdtoi2(PG_FUNCTION_ARGS){ float8 num = PG_GETARG_FLOAT8(0); int16 result; if ((num < SHRT_MIN) || (num > SHRT_MAX)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); result = (int16) rint(num); PG_RETURN_INT16(result);}/* * i4tod - converts an int4 number to a float8 number */Datumi4tod(PG_FUNCTION_ARGS){ int32 num = PG_GETARG_INT32(0); float8 result; result = num; PG_RETURN_FLOAT8(result);}/* * i2tod - converts an int2 number to a float8 number */Datumi2tod(PG_FUNCTION_ARGS){ int16 num = PG_GETARG_INT16(0); float8 result; result = num; PG_RETURN_FLOAT8(result);}/* * ftoi4 - converts a float4 number to an int4 number */Datumftoi4(PG_FUNCTION_ARGS){ float4 num = PG_GETARG_FLOAT4(0); int32 result; if ((num < INT_MIN) || (num > INT_MAX)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); result = (int32) rint(num); PG_RETURN_INT32(result);}/* * ftoi2 - converts a float4 number to an int2 number */Datumftoi2(PG_FUNCTION_ARGS){ float4 num = PG_GETARG_FLOAT4(0); int16 result; if ((num < SHRT_MIN) || (num > SHRT_MAX)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); result = (int16) rint(num); PG_RETURN_INT16(result);}/* * i4tof - converts an int4 number to a float8 number */Datumi4tof(PG_FUNCTION_ARGS){ int32 num = PG_GETARG_INT32(0); float4 result; result = num; PG_RETURN_FLOAT4(result);}/* * i2tof - converts an int2 number to a float4 number */Datumi2tof(PG_FUNCTION_ARGS){ int16 num = PG_GETARG_INT16(0); float4 result; result = num; PG_RETURN_FLOAT4(result);}/* * float8_text - converts a float8 number to a text string */Datumfloat8_text(PG_FUNCTION_ARGS){ float8 num = PG_GETARG_FLOAT8(0); text *result; int len; char *str; str = DatumGetCString(DirectFunctionCall1(float8out, Float8GetDatum(num))); len = strlen(str) + VARHDRSZ; result = (text *) palloc(len); VARATT_SIZEP(result) = len; memcpy(VARDATA(result), str, (len - VARHDRSZ)); pfree(str); PG_RETURN_TEXT_P(result);}/* * text_float8 - converts a text string to a float8 number */Datumtext_float8(PG_FUNCTION_ARGS){ text *string = PG_GETARG_TEXT_P(0); Datum result; int len; char *str; len = (VARSIZE(string) - VARHDRSZ); str = palloc(len + 1); memcpy(str, VARDATA(string), len); *(str + len) = '\0'; result = DirectFunctionCall1(float8in, CStringGetDatum(str)); pfree(str); PG_RETURN_DATUM(result);}/* * float4_text - converts a float4 number to a text string */Datumfloat4_text(PG_FUNCTION_ARGS){ float4 num = PG_GETARG_FLOAT4(0); text *result; int len; char *str; str = DatumGetCString(DirectFunctionCall1(float4out, Float4GetDatum(num))); len = strlen(str) + VARHDRSZ; result = (text *) palloc(len); VARATT_SIZEP(result) = len; memcpy(VARDATA(result), str, (len - VARHDRSZ)); pfree(str); PG_RETURN_TEXT_P(result);}/* * text_float4 - converts a text string to a float4 number */Datumtext_float4(PG_FUNCTION_ARGS){ text *string = PG_GETARG_TEXT_P(0); Datum result; int len; char *str; len = (VARSIZE(string) - VARHDRSZ); str = palloc(len + 1); memcpy(str, VARDATA(string), len); *(str + len) = '\0'; result = DirectFunctionCall1(float4in, CStringGetDatum(str)); pfree(str); PG_RETURN_DATUM(result);}/* * ======================= * RANDOM FLOAT8 OPERATORS * ======================= *//* * dround - returns ROUND(arg1) */Datumdround(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; result = rint(arg1); PG_RETURN_FLOAT8(result);}/* * dceil - returns the smallest integer greater than or * equal to the specified float */Datumdceil(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 */Datumdfloor(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. */Datumdsign(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);}/* * dtrunc - returns truncation-towards-zero of arg1, * arg1 >= 0 ... the greatest integer less * than or equal to arg1 * arg1 < 0 ... the least integer greater * than or equal to arg1 */Datumdtrunc(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; if (arg1 >= 0) result = floor(arg1); else result = -floor(-arg1); PG_RETURN_FLOAT8(result);}/* * dsqrt - returns square root of arg1 */Datumdsqrt(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; if (arg1 < 0) ereport(ERROR, (errcode(ERRCODE_FLOATING_POINT_EXCEPTION), errmsg("cannot take square root of a negative number"))); result = sqrt(arg1); CheckFloat8Val(result); PG_RETURN_FLOAT8(result);}/* * dcbrt - returns cube root of arg1 */Datumdcbrt(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; result = cbrt(arg1); PG_RETURN_FLOAT8(result);}/* * dpow - returns pow(arg1,arg2) */Datumdpow(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 arg2 = PG_GETARG_FLOAT8(1); float8 result; /* * We must check both for errno getting set and for a NaN result, in * order to deal with the vagaries of different platforms... */ errno = 0; result = pow(arg1, arg2); if (errno != 0#ifdef HAVE_FINITE || !finite(result)#endif ) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("result is out of range"))); CheckFloat8Val(result); PG_RETURN_FLOAT8(result);}/* * dexp - returns the exponential function of arg1 */Datumdexp(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; /* * 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. */ errno = 0; result = exp(arg1); if (errno != 0 || result == 0.0#ifdef HAVE_FINITE || !finite(result)#endif ) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("result is out of range"))); CheckFloat8Val(result); PG_RETURN_FLOAT8(result);}/* * dlog1 - returns the natural logarithm of arg1 * ("dlog" is already a logging routine...) */Datumdlog1(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; if (arg1 == 0.0) ereport(ERROR, (errcode(ERRCODE_FLOATING_POINT_EXCEPTION), errmsg("cannot take logarithm of zero"))); if (arg1 < 0) ereport(ERROR, (errcode(ERRCODE_FLOATING_POINT_EXCEPTION), errmsg("cannot take logarithm of a negative number"))); result = log(arg1); CheckFloat8Val(result); PG_RETURN_FLOAT8(result);}/* * dlog10 - returns the base 10 logarithm of arg1 */Datumdlog10(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; if (arg1 == 0.0) ereport(ERROR, (errcode(ERRCODE_FLOATING_POINT_EXCEPTION), errmsg("cannot take logarithm of zero"))); if (arg1 < 0) ereport(ERROR, (errcode(ERRCODE_FLOATING_POINT_EXCEPTION), errmsg("cannot take logarithm of a negative number"))); result = log10(arg1); CheckFloat8Val(result); PG_RETURN_FLOAT8(result);}/* * dacos - returns the arccos of arg1 (radians) */Datumdacos(PG_FUNCTION_ARGS){ float8 arg1 = PG_GETARG_FLOAT8(0); float8 result; errno = 0; result = acos(arg1); if (errno != 0#ifdef HAVE_FINITE || !finite(result)#endif ) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("input is out of range"))); CheckFloat8Val(result); PG_RETURN_FLOAT8(result);}/* * dasin - returns the arcsin of arg1 (radians) */Datumdasin(PG_FUNCTION_ARGS){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -