diff src/float.c @ 24780:7bc92a651472 v8.2.2928

patch 8.2.2928: the evalfunc.c file is too big Commit: https://github.com/vim/vim/commit/01c798c31a94a50ad0c4a022fc21c1a31553be21 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Wed Jun 2 17:07:18 2021 +0200 patch 8.2.2928: the evalfunc.c file is too big Problem: The evalfunc.c file is too big. Solution: Move float related functionality to a separate file. (Yegappan Lakshmanan, closes #8287)
author Bram Moolenaar <Bram@vim.org>
date Wed, 02 Jun 2021 17:15:03 +0200
parents
children 5f8dd7b3ae41
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/src/float.c
@@ -0,0 +1,477 @@
+/* vi:set ts=8 sts=4 sw=4 noet:
+ *
+ * VIM - Vi IMproved	by Bram Moolenaar
+ *
+ * Do ":help uganda"  in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * float.c: Floating point functions
+ */
+#define USING_FLOAT_STUFF
+
+#include "vim.h"
+
+#if (defined(FEAT_EVAL) && defined(FEAT_FLOAT)) || defined(PROTO)
+
+#ifdef VMS
+# include <float.h>
+#endif
+
+/*
+ * Convert the string "text" to a floating point number.
+ * This uses strtod().  setlocale(LC_NUMERIC, "C") has been used to make sure
+ * this always uses a decimal point.
+ * Returns the length of the text that was consumed.
+ */
+    int
+string2float(
+    char_u	*text,
+    float_T	*value)	    // result stored here
+{
+    char	*s = (char *)text;
+    float_T	f;
+
+    // MS-Windows does not deal with "inf" and "nan" properly.
+    if (STRNICMP(text, "inf", 3) == 0)
+    {
+	*value = INFINITY;
+	return 3;
+    }
+    if (STRNICMP(text, "-inf", 3) == 0)
+    {
+	*value = -INFINITY;
+	return 4;
+    }
+    if (STRNICMP(text, "nan", 3) == 0)
+    {
+	*value = NAN;
+	return 3;
+    }
+    f = strtod(s, &s);
+    *value = f;
+    return (int)((char_u *)s - text);
+}
+
+/*
+ * Get the float value of "argvars[0]" into "f".
+ * Returns FAIL when the argument is not a Number or Float.
+ */
+    static int
+get_float_arg(typval_T *argvars, float_T *f)
+{
+    if (argvars[0].v_type == VAR_FLOAT)
+    {
+	*f = argvars[0].vval.v_float;
+	return OK;
+    }
+    if (argvars[0].v_type == VAR_NUMBER)
+    {
+	*f = (float_T)argvars[0].vval.v_number;
+	return OK;
+    }
+    emsg(_("E808: Number or Float required"));
+    return FAIL;
+}
+
+/*
+ * "abs(expr)" function
+ */
+    void
+f_abs(typval_T *argvars, typval_T *rettv)
+{
+    if (argvars[0].v_type == VAR_FLOAT)
+    {
+	rettv->v_type = VAR_FLOAT;
+	rettv->vval.v_float = fabs(argvars[0].vval.v_float);
+    }
+    else
+    {
+	varnumber_T	n;
+	int		error = FALSE;
+
+	n = tv_get_number_chk(&argvars[0], &error);
+	if (error)
+	    rettv->vval.v_number = -1;
+	else if (n > 0)
+	    rettv->vval.v_number = n;
+	else
+	    rettv->vval.v_number = -n;
+    }
+}
+
+/*
+ * "acos()" function
+ */
+    void
+f_acos(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = acos(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "asin()" function
+ */
+    void
+f_asin(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = asin(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "atan()" function
+ */
+    void
+f_atan(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = atan(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "atan2()" function
+ */
+    void
+f_atan2(typval_T *argvars, typval_T *rettv)
+{
+    float_T	fx = 0.0, fy = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &fx) == OK
+				     && get_float_arg(&argvars[1], &fy) == OK)
+	rettv->vval.v_float = atan2(fx, fy);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "ceil({float})" function
+ */
+    void
+f_ceil(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = ceil(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "cos()" function
+ */
+    void
+f_cos(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = cos(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "cosh()" function
+ */
+    void
+f_cosh(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = cosh(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "exp()" function
+ */
+    void
+f_exp(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = exp(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "float2nr({float})" function
+ */
+    void
+f_float2nr(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    if (get_float_arg(argvars, &f) == OK)
+    {
+	if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
+	    rettv->vval.v_number = -VARNUM_MAX;
+	else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
+	    rettv->vval.v_number = VARNUM_MAX;
+	else
+	    rettv->vval.v_number = (varnumber_T)f;
+    }
+}
+
+/*
+ * "floor({float})" function
+ */
+    void
+f_floor(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = floor(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "fmod()" function
+ */
+    void
+f_fmod(typval_T *argvars, typval_T *rettv)
+{
+    float_T	fx = 0.0, fy = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &fx) == OK
+				     && get_float_arg(&argvars[1], &fy) == OK)
+	rettv->vval.v_float = fmod(fx, fy);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+# if defined(HAVE_MATH_H) || defined(PROTO)
+/*
+ * "isinf()" function
+ */
+    void
+f_isinf(typval_T *argvars, typval_T *rettv)
+{
+    if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
+	rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
+}
+
+/*
+ * "isnan()" function
+ */
+    void
+f_isnan(typval_T *argvars, typval_T *rettv)
+{
+    rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
+					    && isnan(argvars[0].vval.v_float);
+}
+# endif
+
+/*
+ * "log()" function
+ */
+    void
+f_log(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = log(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "log10()" function
+ */
+    void
+f_log10(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = log10(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "pow()" function
+ */
+    void
+f_pow(typval_T *argvars, typval_T *rettv)
+{
+    float_T	fx = 0.0, fy = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &fx) == OK
+				     && get_float_arg(&argvars[1], &fy) == OK)
+	rettv->vval.v_float = pow(fx, fy);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+
+/*
+ * round() is not in C90, use ceil() or floor() instead.
+ */
+    float_T
+vim_round(float_T f)
+{
+    return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
+}
+
+/*
+ * "round({float})" function
+ */
+    void
+f_round(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = vim_round(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "sin()" function
+ */
+    void
+f_sin(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = sin(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "sinh()" function
+ */
+    void
+f_sinh(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = sinh(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "sqrt()" function
+ */
+    void
+f_sqrt(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = sqrt(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "str2float()" function
+ */
+    void
+f_str2float(typval_T *argvars, typval_T *rettv)
+{
+    char_u *p = skipwhite(tv_get_string(&argvars[0]));
+    int     isneg = (*p == '-');
+
+    if (*p == '+' || *p == '-')
+	p = skipwhite(p + 1);
+    (void)string2float(p, &rettv->vval.v_float);
+    if (isneg)
+	rettv->vval.v_float *= -1;
+    rettv->v_type = VAR_FLOAT;
+}
+
+/*
+ * "tan()" function
+ */
+    void
+f_tan(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = tan(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "tanh()" function
+ */
+    void
+f_tanh(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	rettv->vval.v_float = tanh(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+/*
+ * "trunc({float})" function
+ */
+    void
+f_trunc(typval_T *argvars, typval_T *rettv)
+{
+    float_T	f = 0.0;
+
+    rettv->v_type = VAR_FLOAT;
+    if (get_float_arg(argvars, &f) == OK)
+	// trunc() is not in C90, use floor() or ceil() instead.
+	rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
+    else
+	rettv->vval.v_float = 0.0;
+}
+
+#endif