Skip to content

Commit

Permalink
patch 8.1.1111: it is not easy to check for infinity
Browse files Browse the repository at this point in the history
Problem:    It is not easy to check for infinity.
Solution:   Add isinf(). (Ozaki Kiichi, closes #3787)
  • Loading branch information
brammool committed Apr 4, 2019
1 parent e5e4e22 commit fda1bff
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
12 changes: 12 additions & 0 deletions runtime/doc/eval.txt
Expand Up @@ -2411,6 +2411,8 @@ inputsecret({prompt} [, {text}]) String like input() but hiding the text
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}
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 14 additions & 3 deletions src/evalfunc.c
Expand Up @@ -237,6 +237,7 @@ static void f_invert(typval_T *argvars, typval_T *rettv);
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);
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -7442,6 +7443,16 @@ f_islocked(typval_T *argvars, typval_T *rettv)
}

#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
*/
Expand Down
23 changes: 17 additions & 6 deletions src/testdir/test_float_func.vim
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -771,6 +771,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1111,
/**/
1110,
/**/
Expand Down

0 comments on commit fda1bff

Please sign in to comment.