golang/go

math: document that Min()/Max() don't agree with builtin min()/max()

crisman opened this issue · 6 comments

What version of Go are you using (go version)?

https://go.dev/play/?v=gotip
devel go1.21-05293d6b49 Mon Jun 5 14:01:09 2023 +0000

What did you do?

https://go.dev/play/p/Sf_G8Lt9dHy?v=gotip

	x := math.NaN()
	y := math.Inf(-1)

	fmt.Println(min(x, y))
	fmt.Println(math.Min(x, y))

Reports different output.

What did you expect to see?

I expected all output to be NaN.

What did you see instead?

min()/max() are returning NaN and math.Min()/math.Max() are returning -Inf/+Inf.

I think the output of the math versions is wrong as it should check for NaN before the infinities, but I don't have a copy of 754 right now and what min/max is under 754 is special (e.g. see minNum in IEEE 754-2008 vs. minimum & minimumNumber in IEEE 754-2019.

This was explicitly addressed in the min/max proposal: #59488 (comment)

Ah, good point. Well given that comment and #59488 (comment) I think we should still fix math.Min/Max.

Unfortunately I don't think we can change math.Min at this point. We would need a very good reason to break compatibility, and "not quite the same as min" isn't a good enough reason.

I'm going to close, at minimum this would need to be a proposal and though I am sympathetic, I can imagine someone writing code that depends on the current behavior. It would be an interesting experiment to instrument math.Min/Max and see how often the problematic case occurred, if at all.

Change https://go.dev/cl/501355 mentions this issue: math: document that Min/Max differ from min/max

Retitling this to be about documenting this difference, which CL 501355 will resolve.