Re: Optimization techniques and undefined behavior

Martin Ward <martin@gkc.org.uk>
Wed, 8 May 2019 13:16:40 +0100

          From comp.compilers

Related articles
[31 earlier articles]
Re: Optimization techniques and undefined behavior robin51@dodo.com.au (Robin Vowels) (2019-05-07)
Re: Optimization techniques and undefined behavior derek@_NOSPAM_knosof.co.uk (Derek M. Jones) (2019-05-06)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-07)
Re: Optimization techniques and undefined behavior martin@gkc.org.uk (Martin Ward) (2019-05-08)
Re: Optimization techniques and undefined behavior gneuner2@comcast.net (George Neuner) (2019-05-08)
Re: Optimization techniques and undefined behavior genew@telus.net (Gene Wirchenko) (2019-05-11)
| List of all articles for this month |
From: Martin Ward <martin@gkc.org.uk>
Newsgroups: comp.compilers
Date: Wed, 8 May 2019 13:16:40 +0100
Organization: Compilers Central
References: 19-05-014 19-04-021 19-04-023 19-04-037 19-04-039 19-04-042 19-04-044 19-04-047 19-05-004 19-05-008 19-05-014 19-05-021 19-05-037 19-05-049
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="56584"; mail-complaints-to="abuse@iecc.com"
Keywords: design
Posted-Date: 08 May 2019 12:45:07 EDT
In-Reply-To: <cf50b1a5-804b-a217-c68d-9e233e3d429b@gkc.org.uk>

On 07/05/19 15:03, David Brown wrote:
> If I write a function "square_root" in Go that takes a non-negative
> input and returns its square root, then calling that function with a
> negative input is undefined behaviour.


https://golang.org/pkg/math/#Sqrt


Sqrt returns the square root of x.


Special cases are:


Sqrt(+Inf) = +Inf
Sqrt(±0) = ±0
Sqrt(x < 0) = NaN
Sqrt(NaN) = NaN


Your "square_root" function might have "undefined behaviour" in the
informal sense that the documentation you provide for your function is
incomplete and does not specify what the function returns for a
negative value. But that is not the kind of "undefined behaviour" we
are discussing in this thread: where the *language* says that
"anything could happen" under certain circumstances. Your
implementation of "square_root" will not have "undefined behaviour" in
this technical sense (unless you have perversely implemented a data
race condition!)


There is a reason why the IEEE floating point standard
includes NaN as a result instead of "undefined behaviour".


--
Martin


Dr Martin Ward | Email: martin@gkc.org.uk | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.