Negative Zero Constant

ID

go.negative_zero_constant

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

Go

Tags

CWE:1164, code-style, suspicious

Description

Reports a floating-point constant written as -0.0. Unlike the IEEE-754 runtime value, a Go floating-point constant cannot represent negative zero: -0.0 is folded to the same value as 0.0.

Rationale

Writing -0.0 does not produce the negative zero the author probably expected — the constant is identical to 0.0. It is therefore either a misunderstanding or dead syntax. The check fires for a unary minus applied directly to a zero-valued floating-point literal; an integer -0, a real negative constant like -1.5, or a runtime negation such as -float64(0) is left alone.

func fn() {
    _ = -0.0          // FLAW — same as 0.0
    _ = float64(-0.0) // FLAW — the inner constant is still -0.0
    _ = -1.5          // OK — a real negative constant
}

Remediation

Use 0.0 if zero was intended, or compute negative zero at runtime (for example math.Copysign(0, -1)) if it is genuinely required.