Skip to content

Alternative Eq/Neq implementation? #2

@dmitshur

Description

@dmitshur

Right now the implementation of Eq and Neq methods looks like this:

// Eq is "==" operation.
func (c Complex64) Eq(x Complex64) bool {
	return c.r == x.r && c.i == x.i
}

// Neq is "!=" operation.
func (c Complex64) Neq(x Complex64) bool {
	return c.r != x.r || c.i != x.i
}

I'm just wondering, have you considered the following alternative implementation?

// Eq is "==" operation.
func (c Complex64) Eq(x Complex64) bool {
	return c == x
}

// Neq is "!=" operation.
func (c Complex64) Neq(x Complex64) bool {
	return c != x
}

Since the struct contains just two float32 fields, the behavior would be identical. But it's shorter, and perhaps simpler/more readable? Of course, that's debatable.

I would expect it to generate identical assembly, but case it's not, would it be faster or slower?

It's just a thought that came to mind when seeing that code, so I wanted to share it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions