Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.linkpendium.com!news.linkpendium.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Martin Ward Newsgroups: comp.compilers Subject: Re: Optimization techniques and undefined behavior Date: Wed, 8 May 2019 13:16:40 +0100 Organization: Compilers Central Lines: 35 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-05-065@comp.compilers> References: <19-05-014@comp.compilers> <19-04-021@comp.compilers> <19-04-023@comp.compilers> <19-04-037@comp.compilers> <19-04-039@comp.compilers> <19-04-042@comp.compilers> <19-04-044@comp.compilers> <19-04-047@comp.compilers> <19-05-004@comp.compilers> <19-05-008@comp.compilers> <19-05-014@comp.compilers> <19-05-021@comp.compilers> <19-05-037@comp.compilers> <19-05-049@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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 X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com In-Reply-To: Xref: csiph.com comp.compilers:2300 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