James McCoy wrote:

> Recently, there was some work done on defining how to handle converting
> numeric literals in vim script to C integers -- basically, clamp to the
> extremes of the datatype.  If it would exceed the max/min value, then
> just treat it as the max/min value.
> 
> However, there's nothing defining how arithmetic behaves when it would
> exceed the range of the datatype.  The only information in the help is:
> 
>   Number              A 32 or 64 bit signed number.  |expr-number| *Number*
>               64-bit Numbers are available only when compiled with the
>               |+num64| feature.
> 
> Now, in C signed over/underflow is undefined behavior.  If you're "lucky",
> the implementation will wrap but it could just as well optimize the code
> under the assumption that the code can't over/underflow.

Yeah, I consider this a bug in the C standard.  Undefined behavior is
useless, the only reason I suspect this is done is so that compiler
writers can makey the code < 1% faster.  And at the same time require
programmers to handle all the edge cases, which gets really complicated
and everybody forgets about.

> Currently, Vim runs afoul of undefined behavior as can easily be seen
> with a -fsanitize=undefined build by running
> 
>   if has('num64')
>     echo float2nr(pow(2, 62)) * 2
>   else
>     echo float2nr(pow(2, 30)) * 2
>   endif
> 
> It would be nice for there to be defined behavior here for the user,
> instead of exposing them to the whims of the compiler implementation.

Unfortunately, that is quite difficult.  I propose the compiler standard
gets fixed. This means only the few compilers that exist need to take
care of this, instead of the millions of C programs.

-- 
Every person is responsible for the choices he makes.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui