2

Can anyone elaborate on the subject of syscalls/js, why in line 57 there is a statement

if f != f { ... }

(f is of type float64).

How is it possible? When can a statement like i != i be true in go?

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • 1
    Also, edit the title. It isn't reference but value, `go != python => true` – Laevus Dexter Mar 03 '20 at 07:50
  • @LaevusDexter that's completely untrue. – hobbs Mar 03 '20 at 07:57
  • @hobbs don't hurt my feelings, explain what statement – Laevus Dexter Mar 03 '20 at 07:58
  • "how is it possible when can a statement like i != i can be true in go?" As in almost every other language. – Volker Mar 03 '20 at 08:02
  • @LaevusDexter equality on floats is always, and very simply, defined. The idea that it isn't is a common myth spread by those who don't know better. The fault isn't in equality, it's in assuming that two computations that "should" produce equal values *do* produce identical values. – hobbs Mar 03 '20 at 08:02
  • @hobbs I got it, thanx. I gotta learn english to read between the lines, feel free to yell at me being dumb. Just woke up and then I was already sleeping. But title is wrong, isn't it? – Laevus Dexter Mar 03 '20 at 14:05

1 Answers1

8

For example if f is of type float64, and its value is a specific value representing "not a number", which you can get from math.NaN(). NaN–by definition–does not equal to any other float64 value, including NaN itself. The float64 type uses the IEEE-754 standard which says only NaN satisfies the f != f unequality.

var f float64 = math.NaN()
fmt.Println(f != f)

This prints true, try it on the Go Playground.

For reasoning, see What is the rationale for all comparisons returning false for IEEE754 NaN values?

icza
  • 389,944
  • 63
  • 907
  • 827