📄 numeric.c
字号:
set_var_from_num(num2, &arg2); sub_var(&arg1, &arg2, &result); res = make_result(&result); free_var(&arg1); free_var(&arg2); free_var(&result); PG_RETURN_NUMERIC(res);}/* * numeric_mul() - * * Calculate the product of two numerics */Datumnumeric_mul(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); NumericVar arg1; NumericVar arg2; NumericVar result; Numeric res; /* * Handle NaN */ if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Unpack the values, let mul_var() compute the result and return it. * Unlike add_var() and sub_var(), mul_var() will round its result. In the * case of numeric_mul(), which is invoked for the * operator on numerics, * we request exact representation for the product (rscale = sum(dscale of * arg1, dscale of arg2)). */ init_var(&arg1); init_var(&arg2); init_var(&result); set_var_from_num(num1, &arg1); set_var_from_num(num2, &arg2); mul_var(&arg1, &arg2, &result, arg1.dscale + arg2.dscale); res = make_result(&result); free_var(&arg1); free_var(&arg2); free_var(&result); PG_RETURN_NUMERIC(res);}/* * numeric_div() - * * Divide one numeric into another */Datumnumeric_div(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); NumericVar arg1; NumericVar arg2; NumericVar result; Numeric res; int rscale; /* * Handle NaN */ if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Unpack the arguments */ init_var(&arg1); init_var(&arg2); init_var(&result); set_var_from_num(num1, &arg1); set_var_from_num(num2, &arg2); /* * Select scale for division result */ rscale = select_div_scale(&arg1, &arg2); /* * Do the divide and return the result */ div_var(&arg1, &arg2, &result, rscale, true); res = make_result(&result); free_var(&arg1); free_var(&arg2); free_var(&result); PG_RETURN_NUMERIC(res);}/* * numeric_mod() - * * Calculate the modulo of two numerics */Datumnumeric_mod(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); Numeric res; NumericVar arg1; NumericVar arg2; NumericVar result; if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) PG_RETURN_NUMERIC(make_result(&const_nan)); init_var(&arg1); init_var(&arg2); init_var(&result); set_var_from_num(num1, &arg1); set_var_from_num(num2, &arg2); mod_var(&arg1, &arg2, &result); res = make_result(&result); free_var(&result); free_var(&arg2); free_var(&arg1); PG_RETURN_NUMERIC(res);}/* * numeric_inc() - * * Increment a number by one */Datumnumeric_inc(PG_FUNCTION_ARGS){ Numeric num = PG_GETARG_NUMERIC(0); NumericVar arg; Numeric res; /* * Handle NaN */ if (NUMERIC_IS_NAN(num)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Compute the result and return it */ init_var(&arg); set_var_from_num(num, &arg); add_var(&arg, &const_one, &arg); res = make_result(&arg); free_var(&arg); PG_RETURN_NUMERIC(res);}/* * numeric_smaller() - * * Return the smaller of two numbers */Datumnumeric_smaller(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); /* * Use cmp_numerics so that this will agree with the comparison operators, * particularly as regards comparisons involving NaN. */ if (cmp_numerics(num1, num2) < 0) PG_RETURN_NUMERIC(num1); else PG_RETURN_NUMERIC(num2);}/* * numeric_larger() - * * Return the larger of two numbers */Datumnumeric_larger(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); /* * Use cmp_numerics so that this will agree with the comparison operators, * particularly as regards comparisons involving NaN. */ if (cmp_numerics(num1, num2) > 0) PG_RETURN_NUMERIC(num1); else PG_RETURN_NUMERIC(num2);}/* ---------------------------------------------------------------------- * * Advanced math functions * * ---------------------------------------------------------------------- *//* * numeric_fac() * * Compute factorial */Datumnumeric_fac(PG_FUNCTION_ARGS){ int64 num = PG_GETARG_INT64(0); Numeric res; NumericVar fact; NumericVar result; if (num <= 1) { res = make_result(&const_one); PG_RETURN_NUMERIC(res); } init_var(&fact); init_var(&result); int8_to_numericvar(num, &result); for (num = num - 1; num > 1; num--) { int8_to_numericvar(num, &fact); mul_var(&result, &fact, &result, 0); } res = make_result(&result); free_var(&fact); free_var(&result); PG_RETURN_NUMERIC(res);}/* * numeric_sqrt() - * * Compute the square root of a numeric. */Datumnumeric_sqrt(PG_FUNCTION_ARGS){ Numeric num = PG_GETARG_NUMERIC(0); Numeric res; NumericVar arg; NumericVar result; int sweight; int rscale; /* * Handle NaN */ if (NUMERIC_IS_NAN(num)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Unpack the argument and determine the result scale. We choose a scale * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any * case not less than the input's dscale. */ init_var(&arg); init_var(&result); set_var_from_num(num, &arg); /* Assume the input was normalized, so arg.weight is accurate */ sweight = (arg.weight + 1) * DEC_DIGITS / 2 - 1; rscale = NUMERIC_MIN_SIG_DIGITS - sweight; rscale = Max(rscale, arg.dscale); rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE); rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE); /* * Let sqrt_var() do the calculation and return the result. */ sqrt_var(&arg, &result, rscale); res = make_result(&result); free_var(&result); free_var(&arg); PG_RETURN_NUMERIC(res);}/* * numeric_exp() - * * Raise e to the power of x */Datumnumeric_exp(PG_FUNCTION_ARGS){ Numeric num = PG_GETARG_NUMERIC(0); Numeric res; NumericVar arg; NumericVar result; int rscale; double val; /* * Handle NaN */ if (NUMERIC_IS_NAN(num)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Unpack the argument and determine the result scale. We choose a scale * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any * case not less than the input's dscale. */ init_var(&arg); init_var(&result); set_var_from_num(num, &arg); /* convert input to float8, ignoring overflow */ val = numericvar_to_double_no_overflow(&arg); /* * log10(result) = num * log10(e), so this is approximately the decimal * weight of the result: */ val *= 0.434294481903252; /* limit to something that won't cause integer overflow */ val = Max(val, -NUMERIC_MAX_RESULT_SCALE); val = Min(val, NUMERIC_MAX_RESULT_SCALE); rscale = NUMERIC_MIN_SIG_DIGITS - (int) val; rscale = Max(rscale, arg.dscale); rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE); rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE); /* * Let exp_var() do the calculation and return the result. */ exp_var(&arg, &result, rscale); res = make_result(&result); free_var(&result); free_var(&arg); PG_RETURN_NUMERIC(res);}/* * numeric_ln() - * * Compute the natural logarithm of x */Datumnumeric_ln(PG_FUNCTION_ARGS){ Numeric num = PG_GETARG_NUMERIC(0); Numeric res; NumericVar arg; NumericVar result; int dec_digits; int rscale; /* * Handle NaN */ if (NUMERIC_IS_NAN(num)) PG_RETURN_NUMERIC(make_result(&const_nan)); init_var(&arg); init_var(&result); set_var_from_num(num, &arg); /* Approx decimal digits before decimal point */ dec_digits = (arg.weight + 1) * DEC_DIGITS; if (dec_digits > 1) rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(dec_digits - 1); else if (dec_digits < 1) rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(1 - dec_digits); else rscale = NUMERIC_MIN_SIG_DIGITS; rscale = Max(rscale, arg.dscale); rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE); rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE); ln_var(&arg, &result, rscale); res = make_result(&result); free_var(&result); free_var(&arg); PG_RETURN_NUMERIC(res);}/* * numeric_log() - * * Compute the logarithm of x in a given base */Datumnumeric_log(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); Numeric res; NumericVar arg1; NumericVar arg2; NumericVar result; /* * Handle NaN */ if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Initialize things */ init_var(&arg1); init_var(&arg2); init_var(&result); set_var_from_num(num1, &arg1); set_var_from_num(num2, &arg2); /* * Call log_var() to compute and return the result; note it handles scale * selection itself. */ log_var(&arg1, &arg2, &result); res = make_result(&result); free_var(&result); free_var(&arg2); free_var(&arg1); PG_RETURN_NUMERIC(res);}/* * numeric_power() - * * Raise b to the power of x */Datumnumeric_power(PG_FUNCTION_ARGS){ Numeric num1 = PG_GETARG_NUMERIC(0); Numeric num2 = PG_GETARG_NUMERIC(1); Numeric res; NumericVar arg1; NumericVar arg2; NumericVar arg2_trunc; NumericVar result; /* * Handle NaN */ if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2)) PG_RETURN_NUMERIC(make_result(&const_nan)); /* * Initialize things */ init_var(&arg1); init_var(&arg2); init_var(&arg2_trunc); init_var(&result); set_var_from_num(num1, &arg1); set_var_from_num(num2, &arg2); set_var_from_var(&arg2, &arg2_trunc); trunc_var(&arg2_trunc, 0); /* * Return special SQLSTATE error codes for a few conditions mandated by * the standard. */ if ((cmp_var(&arg1, &const_zero) == 0 && cmp_var(&arg2, &const_zero) < 0) || (cmp_var(&arg1, &const_zero) < 0 && cmp_var(&arg2, &arg2_trunc) != 0)) ereport(ERROR, (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION), errmsg("invalid argument for power function"))); /* * Call power_var() to compute and return the result; note it handles * scale selection itself. */ power_var(&arg1, &arg2, &result); res = make_result(&result); free_var(&result); free_var(&arg2); free_var(&arg2_trunc); free_var(&arg1); PG_RETURN_NUMERIC(res);}/* ---------------------------------------------------------------------- * * Type conversion functions * * ---------------------------------------------------------------------- */Datumint4_numeric(PG_FUNCTION_ARGS){ int32 val = PG_GETARG_INT32(0); Numeric res; NumericVar result; init_var(&result); int8_to_numericvar((int64) val, &result); res = make_result(&result); free_var(&result); PG_RETURN_NUMERIC(res);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -