From e482dcb0a438dfa1fcb2cb792730c00db337a834 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 13 Oct 2001 23:32:34 +0000 Subject: Make selectivity routines cope gracefully with NaNs, infinities, and NUMERIC values that are out of the range of 'double'. Per trouble report from Mike Quinn. --- src/backend/utils/adt/numeric.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/backend/utils/adt/numeric.c') diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 797586018d0..5160f690c1e 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -5,7 +5,7 @@ * * 1998 Jan Wieck * - * $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.44 2001/10/03 05:29:24 thomas Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.45 2001/10/13 23:32:33 tgl Exp $ * * ---------- */ @@ -1663,6 +1663,35 @@ numeric_float8(PG_FUNCTION_ARGS) } +/* Convert numeric to float8; if out of range, return +/- HUGE_VAL */ +Datum +numeric_float8_no_overflow(PG_FUNCTION_ARGS) +{ + Numeric num = PG_GETARG_NUMERIC(0); + char *tmp; + double val; + char *endptr; + + if (NUMERIC_IS_NAN(num)) + PG_RETURN_FLOAT8(NAN); + + tmp = DatumGetCString(DirectFunctionCall1(numeric_out, + NumericGetDatum(num))); + + /* unlike float8in, we ignore ERANGE from strtod */ + val = strtod(tmp, &endptr); + if (*endptr != '\0') + { + /* shouldn't happen ... */ + elog(ERROR, "Bad float8 input format '%s'", tmp); + } + + pfree(tmp); + + PG_RETURN_FLOAT8(val); +} + + Datum float4_numeric(PG_FUNCTION_ARGS) { -- cgit v1.2.3