On Wed, Jan 23, 2013 at 2:19 AM, Rémy Oudompheng
<
remyoud...@gmail.com> wrote:
> The conversion of float64 to uint8 has undefined behaviour for values
> outside (0, 255) and is a frequent source of annoyance.
In standardese "undefined behaviour" is a term of art. It explicitly
means that if the undefined part of the program is executed, anything
at all may happen. What that means in practice is that the compiler
may transform a program that uses undefined behaviour into something
completely unexpected and in fact unpredictable. It's difficult to
overemphasize how strange this can get. For example, this function in
C
int f(int x) { return 0x7ffffff0 < x && x + 32 < 0x7fffffff; }
while seemingly meaningful, relies on the specific behaviour of signed
overflow, which is undefined in C/C++. GCC will compile this into a
function that always returns 0. For a more exotic example, while I
don't have a complete function on hand, I've seen cases where "if (x)
A; else B;" were compiled to execute neither A nor B when x was a C++
bool variable that held a value other than true or false. That kind
of thing seems impossible to non-compiler writers.
Anyhow, all that is simply to say that the conversion of float64 to
uint8 in Go is not undefined behaviour. It is implementation defined
behaviour, which is very different and much more controlled.
Implementation defined behaviour means that the program always behaves
normally, but the specific value that you get may differ in different
implementations. That is not the same as undefined behaviour where
quite literally anything can happen.
Go has much less undefined behaviour than C/C++. That is a deliberate
choice. Pretty much all cases that are undefined in C/C++ are fully
specified or implementation defined in Go. However, Go does have
undefined behaviour: if your program has a race condition, the
behaviour is undefined. If you have a race condition you can see
inconsistent and seemingly impossible states in your program.
Ian
> 2013/1/23, Dave Cheney <
da...@cheney.net>:
>> In reading about C/C++ and its compilers, a common complaint is the
>> liberal use of the words `undefined behavior ` in the relevant
>> specifications. The concept of undefined behavior is simultaneously
>> reviled by those debugging C/C++ applications, and very important to
>> optimizing compiler writers. Some references even claim that this term
>> is overused in the current specifications, implying that the authors
>> were somewhat lax in their job.
>>
>> The term `undefined behavior` or even `undefined` appears nowhere in
>> the Go spec (either 1.0 or tip), which I interpret to be a good thing.
>>
>> My questions are:
>>
>> * does the lack of documented undefined behavior improve the safety of
>> Go as a language (assuming a conforming compiler and runtime) ?
>>
>> * what trade offs, possibly in performance, or compiler speed or
>> design, were necessary to avoid undefined behavior in Go programs ?
>>
>> Cheers
>>
>> Dave
>>
>> --
>>
>>
>>
>
> --
>
>