Mercurial > vim
changeset 16213:2c33b119ba92 v8.1.1111
patch 8.1.1111: it is not easy to check for infinity
commit https://github.com/vim/vim/commit/fda1bff39f89775b20a2d88ef3903656d52f66ad
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Apr 4 13:44:37 2019 +0200
patch 8.1.1111: it is not easy to check for infinity
Problem: It is not easy to check for infinity.
Solution: Add isinf(). (Ozaki Kiichi, closes https://github.com/vim/vim/issues/3787)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 04 Apr 2019 13:45:04 +0200 |
parents | 862f5ea5dcb0 |
children | 1922689a5149 |
files | runtime/doc/eval.txt src/evalfunc.c src/testdir/test_float_func.vim src/version.c |
diffstat | 4 files changed, 45 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -2411,6 +2411,8 @@ inputsecret({prompt} [, {text}]) String insert({object}, {item} [, {idx}]) List insert {item} in {object} [before {idx}] invert({expr}) Number bitwise invert isdirectory({directory}) Number |TRUE| if {directory} is a directory +isinf({expr}) Number determine if {expr} is infinity value + (positive or negative) islocked({expr}) Number |TRUE| if {expr} is locked isnan({expr}) Number |TRUE| if {expr} is NaN items({dict}) List key-value pairs in {dict} @@ -5772,6 +5774,16 @@ isdirectory({directory}) *isdirectory exist, or isn't a directory, the result is |FALSE|. {directory} is any expression, which is used as a String. +isinf({expr}) *isinf()* + Return 1 if {expr} is a positive infinity, or -1 a negative + infinity, otherwise 0. > + :echo isinf(1.0 / 0.0) +< 1 > + :echo isinf(-1.0 / 0.0) +< -1 + + {only available when compiled with the |+float| feature} + islocked({expr}) *islocked()* *E786* The result is a Number, which is |TRUE| when {expr} is the name of a locked variable.
--- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -237,6 +237,7 @@ static void f_invert(typval_T *argvars, static void f_isdirectory(typval_T *argvars, typval_T *rettv); static void f_islocked(typval_T *argvars, typval_T *rettv); #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) +static void f_isinf(typval_T *argvars, typval_T *rettv); static void f_isnan(typval_T *argvars, typval_T *rettv); #endif static void f_items(typval_T *argvars, typval_T *rettv); @@ -721,6 +722,9 @@ static struct fst {"insert", 2, 3, f_insert}, {"invert", 1, 1, f_invert}, {"isdirectory", 1, 1, f_isdirectory}, +#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) + {"isinf", 1, 1, f_isinf}, +#endif {"islocked", 1, 1, f_islocked}, #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) {"isnan", 1, 1, f_isnan}, @@ -6582,9 +6586,6 @@ f_has(typval_T *argvars, typval_T *rettv #ifdef FEAT_TAG_BINS "tag_binary", #endif -#ifdef FEAT_TAG_OLDSTATIC - "tag_old_static", -#endif #ifdef FEAT_TCL # ifndef DYNAMIC_TCL "tcl", @@ -7443,6 +7444,16 @@ f_islocked(typval_T *argvars, typval_T * #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) /* + * "isinf()" function + */ + static 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 */ static void
--- a/src/testdir/test_float_func.vim +++ b/src/testdir/test_float_func.vim @@ -288,13 +288,24 @@ func Test_trunc() call assert_fails("call trunc('')", 'E808:') endfunc +func Test_isinf() + call assert_equal(1, isinf(1.0/0.0)) + call assert_equal(-1, isinf(-1.0/0.0)) + call assert_false(isinf(1.0)) + call assert_false(isinf(0.0/0.0)) + call assert_false(isinf('a')) + call assert_false(isinf([])) + call assert_false(isinf({})) +endfunc + func Test_isnan() - call assert_equal(0, isnan(1.0)) - call assert_equal(1, isnan(0.0/0.0)) - call assert_equal(0, isnan(1.0/0.0)) - call assert_equal(0, isnan('a')) - call assert_equal(0, isnan([])) - call assert_equal(0, isnan({})) + call assert_true(isnan(0.0/0.0)) + call assert_false(isnan(1.0)) + call assert_false(isnan(1.0/0.0)) + call assert_false(isnan(-1.0/0.0)) + call assert_false(isnan('a')) + call assert_false(isnan([])) + call assert_false(isnan({})) endfunc " This was converted from test65