Skip to content

Commit 863e80b

Browse files
committed
patch 8.0.0614: float2nr() is not exactly right
Problem: float2nr() is not exactly right. Solution: Make float2nr() more accurate. Turn test64 into a new style test. (Hirohito Higashi, closes #1688)
1 parent 3e54569 commit 863e80b

10 files changed

Lines changed: 99 additions & 173 deletions

File tree

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,7 @@ test1 \
20872087
test30 test31 test32 test33 test34 test36 test37 test38 test39 \
20882088
test40 test41 test42 test43 test44 test45 test48 test49 \
20892089
test50 test51 test52 test53 test54 test55 test56 test57 test58 test59 \
2090-
test60 test64 test65 test66 test67 test68 test69 \
2090+
test60 test64 test66 test67 test68 test69 \
20912091
test70 test72 test73 test74 test75 test77 test78 test79 \
20922092
test80 test82 test83 test84 test85 test86 test87 test88 \
20932093
test90 test91 test94 test95 test97 test98 test99 \

src/evalfunc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3352,9 +3352,9 @@ f_float2nr(typval_T *argvars, typval_T *rettv)
33523352

33533353
if (get_float_arg(argvars, &f) == OK)
33543354
{
3355-
if (f < -VARNUM_MAX)
3355+
if (f <= -VARNUM_MAX + DBL_EPSILON)
33563356
rettv->vval.v_number = -VARNUM_MAX;
3357-
else if (f > VARNUM_MAX)
3357+
else if (f >= VARNUM_MAX - DBL_EPSILON)
33583358
rettv->vval.v_number = VARNUM_MAX;
33593359
else
33603360
rettv->vval.v_number = (varnumber_T)f;

src/macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@
364364
# if !defined(NAN)
365365
# define NAN (INFINITY-INFINITY)
366366
# endif
367+
# if !defined(DBL_EPSILON)
368+
# define DBL_EPSILON 2.2204460492503131e-16
369+
# endif
367370
# endif
368371
#endif
369372

src/testdir/Make_all.mak

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ SCRIPTS_ALL = \
5050
test57.out \
5151
test60.out \
5252
test64.out \
53-
test65.out \
5453
test66.out \
5554
test67.out \
5655
test68.out \

src/testdir/Make_vms.mms

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ SCRIPT = test1.out test3.out test4.out test5.out \
8787
test43.out test44.out test45.out \
8888
test48.out test49.out test51.out test53.out test54.out \
8989
test55.out test56.out test57.out test60.out \
90-
test64.out test65.out \
90+
test64.out \
9191
test66.out test67.out test68.out test69.out \
9292
test72.out test75.out \
9393
test77a.out test78.out test79.out test80.out \

src/testdir/test65.in

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/testdir/test65.ok

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/testdir/test_float_func.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ func Test_str2float()
224224
call assert_fails("call str2float(function('string'))", 'E729:')
225225
endfunc
226226

227+
func Test_float2nr()
228+
call assert_equal(1, float2nr(1.234))
229+
call assert_equal(123, float2nr(1.234e2))
230+
call assert_equal(12, float2nr(123.4e-1))
231+
let max_number = 1/0
232+
let min_number = -max_number
233+
call assert_equal(max_number/2+1, float2nr(pow(2, 62)))
234+
call assert_equal(max_number, float2nr(pow(2, 63)))
235+
call assert_equal(max_number, float2nr(pow(2, 64)))
236+
call assert_equal(min_number/2-1, float2nr(-pow(2, 62)))
237+
call assert_equal(min_number, float2nr(-pow(2, 63)))
238+
call assert_equal(min_number, float2nr(-pow(2, 64)))
239+
endfunc
240+
227241
func Test_floor()
228242
call assert_equal('2.0', string(floor(2.0)))
229243
call assert_equal('2.0', string(floor(2.11)))
@@ -282,3 +296,36 @@ func Test_isnan()
282296
call assert_equal(0, isnan([]))
283297
call assert_equal(0, isnan({}))
284298
endfunc
299+
300+
" This was converted from test65
301+
func Test_float_misc()
302+
call assert_equal('123.456000', printf('%f', 123.456))
303+
call assert_equal('1.234560e+02', printf('%e', 123.456))
304+
call assert_equal('123.456', printf('%g', 123.456))
305+
" +=
306+
let v = 1.234
307+
let v += 6.543
308+
call assert_equal('7.777', printf('%g', v))
309+
let v = 1.234
310+
let v += 5
311+
call assert_equal('6.234', printf('%g', v))
312+
let v = 5
313+
let v += 3.333
314+
call assert_equal('8.333', string(v))
315+
" ==
316+
let v = 1.234
317+
call assert_true(v == 1.234)
318+
call assert_false(v == 1.2341)
319+
" add-subtract
320+
call assert_equal('5.234', printf('%g', 4 + 1.234))
321+
call assert_equal('-6.766', printf('%g', 1.234 - 8))
322+
" mult-div
323+
call assert_equal('4.936', printf('%g', 4 * 1.234))
324+
call assert_equal('0.003241', printf('%g', 4.0 / 1234))
325+
" dict
326+
call assert_equal("{'x': 1.234, 'y': -2.0e20}", string({'x': 1.234, 'y': -2.0e20}))
327+
" list
328+
call assert_equal('[-123.4, 2.0e-20]', string([-123.4, 2.0e-20]))
329+
endfunc
330+
331+
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_vimscript.vim

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,49 @@ func Test_script_emty_line_continuation()
13201320
\
13211321
endfunc
13221322

1323+
"-------------------------------------------------------------------------------
1324+
" Test 97: bitwise functions {{{1
1325+
"-------------------------------------------------------------------------------
1326+
func Test_bitwise_functions()
1327+
" and
1328+
call assert_equal(127, and(127, 127))
1329+
call assert_equal(16, and(127, 16))
1330+
call assert_equal(0, and(127, 128))
1331+
call assert_fails("call and(1.0, 1)", 'E805:')
1332+
call assert_fails("call and([], 1)", 'E745:')
1333+
call assert_fails("call and({}, 1)", 'E728:')
1334+
call assert_fails("call and(1, 1.0)", 'E805:')
1335+
call assert_fails("call and(1, [])", 'E745:')
1336+
call assert_fails("call and(1, {})", 'E728:')
1337+
" or
1338+
call assert_equal(23, or(16, 7))
1339+
call assert_equal(15, or(8, 7))
1340+
call assert_equal(123, or(0, 123))
1341+
call assert_fails("call or(1.0, 1)", 'E805:')
1342+
call assert_fails("call or([], 1)", 'E745:')
1343+
call assert_fails("call or({}, 1)", 'E728:')
1344+
call assert_fails("call or(1, 1.0)", 'E805:')
1345+
call assert_fails("call or(1, [])", 'E745:')
1346+
call assert_fails("call or(1, {})", 'E728:')
1347+
" xor
1348+
call assert_equal(0, xor(127, 127))
1349+
call assert_equal(111, xor(127, 16))
1350+
call assert_equal(255, xor(127, 128))
1351+
call assert_fails("call xor(1.0, 1)", 'E805:')
1352+
call assert_fails("call xor([], 1)", 'E745:')
1353+
call assert_fails("call xor({}, 1)", 'E728:')
1354+
call assert_fails("call xor(1, 1.0)", 'E805:')
1355+
call assert_fails("call xor(1, [])", 'E745:')
1356+
call assert_fails("call xor(1, {})", 'E728:')
1357+
" invert
1358+
call assert_equal(65408, and(invert(127), 65535))
1359+
call assert_equal(65519, and(invert(16), 65535))
1360+
call assert_equal(65407, and(invert(128), 65535))
1361+
call assert_fails("call invert(1.0)", 'E805:')
1362+
call assert_fails("call invert([])", 'E745:')
1363+
call assert_fails("call invert({})", 'E728:')
1364+
endfunc
1365+
13231366
"-------------------------------------------------------------------------------
13241367
" Modelines {{{1
13251368
" vim: ts=8 sw=4 tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
614,
767769
/**/
768770
613,
769771
/**/

0 commit comments

Comments
 (0)