int64(math.Inf(+1)) != math.MaxInt64

103 views
Skip to first unread message

Jonathan Pearson

unread,
Oct 3, 2022, 10:19:05 PM10/3/22
to golang-nuts
I expected int64(math.Inf(+1)) == math.MaxInt64, but it does not.

In fact, these three values are equal:
int64(math.Inf(+1))
int64(math.Inf(-1))
math.MinInt64

This surprised me, and the language spec didn't clarify this specific behavior, so I wondered whether it was intentional. If so, why?

~Jonathan

Rob Pike

unread,
Oct 3, 2022, 10:37:42 PM10/3/22
to Jonathan Pearson, golang-nuts
Looks like a bug to me, so I filed https://github.com/golang/go/issues/56023

Thanks for reporting it.

-rob


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3970aef1-8416-4102-bbd7-72033993e9b0n%40googlegroups.com.

Jan Mercl

unread,
Oct 4, 2022, 1:29:52 AM10/4/22
to Jonathan Pearson, golang-nuts
On Tue, Oct 4, 2022 at 4:18 AM Jonathan Pearson <cell...@gmail.com> wrote:

> I expected int64(math.Inf(+1)) == math.MaxInt64, but it does not.

Quoting from https://go.dev/ref/spec#Conversions, section
"Conversions between numeric types"

""""
In all non-constant conversions involving floating-point or complex
values, if the result type cannot represent the value the conversion
succeeds but the result value is implementation-dependent.
""""

AFAICT, this is working as intended. FTR, also in my experience,
different targets produced different results for the same values the last
time I ran into this issue some years ago.
Reply all
Reply to author
Forward
0 new messages