Skip to content

Commit

Permalink
patch 8.0.0614: float2nr() is not exactly right
Browse files Browse the repository at this point in the history
Problem:    float2nr() is not exactly right.
Solution:   Make float2nr() more accurate.  Turn test64 into a new style test.
            (Hirohito Higashi, closes #1688)
  • Loading branch information
brammool committed Jun 4, 2017
1 parent 3e54569 commit 863e80b
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 173 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Expand Up @@ -2087,7 +2087,7 @@ test1 \
test30 test31 test32 test33 test34 test36 test37 test38 test39 \
test40 test41 test42 test43 test44 test45 test48 test49 \
test50 test51 test52 test53 test54 test55 test56 test57 test58 test59 \
test60 test64 test65 test66 test67 test68 test69 \
test60 test64 test66 test67 test68 test69 \
test70 test72 test73 test74 test75 test77 test78 test79 \
test80 test82 test83 test84 test85 test86 test87 test88 \
test90 test91 test94 test95 test97 test98 test99 \
Expand Down
4 changes: 2 additions & 2 deletions src/evalfunc.c
Expand Up @@ -3352,9 +3352,9 @@ f_float2nr(typval_T *argvars, typval_T *rettv)

if (get_float_arg(argvars, &f) == OK)
{
if (f < -VARNUM_MAX)
if (f <= -VARNUM_MAX + DBL_EPSILON)
rettv->vval.v_number = -VARNUM_MAX;
else if (f > VARNUM_MAX)
else if (f >= VARNUM_MAX - DBL_EPSILON)
rettv->vval.v_number = VARNUM_MAX;
else
rettv->vval.v_number = (varnumber_T)f;
Expand Down
3 changes: 3 additions & 0 deletions src/macros.h
Expand Up @@ -364,6 +364,9 @@
# if !defined(NAN)
# define NAN (INFINITY-INFINITY)
# endif
# if !defined(DBL_EPSILON)
# define DBL_EPSILON 2.2204460492503131e-16
# endif
# endif
#endif

Expand Down
1 change: 0 additions & 1 deletion src/testdir/Make_all.mak
Expand Up @@ -50,7 +50,6 @@ SCRIPTS_ALL = \
test57.out \
test60.out \
test64.out \
test65.out \
test66.out \
test67.out \
test68.out \
Expand Down
2 changes: 1 addition & 1 deletion src/testdir/Make_vms.mms
Expand Up @@ -87,7 +87,7 @@ SCRIPT = test1.out test3.out test4.out test5.out \
test43.out test44.out test45.out \
test48.out test49.out test51.out test53.out test54.out \
test55.out test56.out test57.out test60.out \
test64.out test65.out \
test64.out \
test66.out test67.out test68.out test69.out \
test72.out test75.out \
test77a.out test78.out test79.out test80.out \
Expand Down
95 changes: 0 additions & 95 deletions src/testdir/test65.in

This file was deleted.

73 changes: 0 additions & 73 deletions src/testdir/test65.ok

This file was deleted.

47 changes: 47 additions & 0 deletions src/testdir/test_float_func.vim
Expand Up @@ -224,6 +224,20 @@ func Test_str2float()
call assert_fails("call str2float(function('string'))", 'E729:')
endfunc

func Test_float2nr()
call assert_equal(1, float2nr(1.234))
call assert_equal(123, float2nr(1.234e2))
call assert_equal(12, float2nr(123.4e-1))
let max_number = 1/0
let min_number = -max_number
call assert_equal(max_number/2+1, float2nr(pow(2, 62)))
call assert_equal(max_number, float2nr(pow(2, 63)))
call assert_equal(max_number, float2nr(pow(2, 64)))
call assert_equal(min_number/2-1, float2nr(-pow(2, 62)))
call assert_equal(min_number, float2nr(-pow(2, 63)))
call assert_equal(min_number, float2nr(-pow(2, 64)))
endfunc

func Test_floor()
call assert_equal('2.0', string(floor(2.0)))
call assert_equal('2.0', string(floor(2.11)))
Expand Down Expand Up @@ -282,3 +296,36 @@ func Test_isnan()
call assert_equal(0, isnan([]))
call assert_equal(0, isnan({}))
endfunc

" This was converted from test65
func Test_float_misc()
call assert_equal('123.456000', printf('%f', 123.456))
call assert_equal('1.234560e+02', printf('%e', 123.456))
call assert_equal('123.456', printf('%g', 123.456))
" +=
let v = 1.234
let v += 6.543
call assert_equal('7.777', printf('%g', v))
let v = 1.234
let v += 5
call assert_equal('6.234', printf('%g', v))
let v = 5
let v += 3.333
call assert_equal('8.333', string(v))
" ==
let v = 1.234
call assert_true(v == 1.234)
call assert_false(v == 1.2341)
" add-subtract
call assert_equal('5.234', printf('%g', 4 + 1.234))
call assert_equal('-6.766', printf('%g', 1.234 - 8))
" mult-div
call assert_equal('4.936', printf('%g', 4 * 1.234))
call assert_equal('0.003241', printf('%g', 4.0 / 1234))
" dict
call assert_equal("{'x': 1.234, 'y': -2.0e20}", string({'x': 1.234, 'y': -2.0e20}))
" list
call assert_equal('[-123.4, 2.0e-20]', string([-123.4, 2.0e-20]))
endfunc

" vim: shiftwidth=2 sts=2 expandtab
43 changes: 43 additions & 0 deletions src/testdir/test_vimscript.vim
Expand Up @@ -1320,6 +1320,49 @@ func Test_script_emty_line_continuation()
\
endfunc

"-------------------------------------------------------------------------------
" Test 97: bitwise functions {{{1
"-------------------------------------------------------------------------------
func Test_bitwise_functions()
" and
call assert_equal(127, and(127, 127))
call assert_equal(16, and(127, 16))
call assert_equal(0, and(127, 128))
call assert_fails("call and(1.0, 1)", 'E805:')
call assert_fails("call and([], 1)", 'E745:')
call assert_fails("call and({}, 1)", 'E728:')
call assert_fails("call and(1, 1.0)", 'E805:')
call assert_fails("call and(1, [])", 'E745:')
call assert_fails("call and(1, {})", 'E728:')
" or
call assert_equal(23, or(16, 7))
call assert_equal(15, or(8, 7))
call assert_equal(123, or(0, 123))
call assert_fails("call or(1.0, 1)", 'E805:')
call assert_fails("call or([], 1)", 'E745:')
call assert_fails("call or({}, 1)", 'E728:')
call assert_fails("call or(1, 1.0)", 'E805:')
call assert_fails("call or(1, [])", 'E745:')
call assert_fails("call or(1, {})", 'E728:')
" xor
call assert_equal(0, xor(127, 127))
call assert_equal(111, xor(127, 16))
call assert_equal(255, xor(127, 128))
call assert_fails("call xor(1.0, 1)", 'E805:')
call assert_fails("call xor([], 1)", 'E745:')
call assert_fails("call xor({}, 1)", 'E728:')
call assert_fails("call xor(1, 1.0)", 'E805:')
call assert_fails("call xor(1, [])", 'E745:')
call assert_fails("call xor(1, {})", 'E728:')
" invert
call assert_equal(65408, and(invert(127), 65535))
call assert_equal(65519, and(invert(16), 65535))
call assert_equal(65407, and(invert(128), 65535))
call assert_fails("call invert(1.0)", 'E805:')
call assert_fails("call invert([])", 'E745:')
call assert_fails("call invert({})", 'E728:')
endfunc

"-------------------------------------------------------------------------------
" Modelines {{{1
" vim: ts=8 sw=4 tw=80 fdm=marker
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -764,6 +764,8 @@ static char *(features[]) =

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

0 comments on commit 863e80b

Please sign in to comment.