Skip to content

math: portable FMA implementation incorrectly returns -0 in some situations #61130

@mundaym

Description

@mundaym

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

8b5fe59

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

linux-amd64

What did you do?

https://go.dev/play/p/KGZMg3ATQtx

package main

import (
	"fmt"
	"math"
)

var portableFMA = math.FMA

func main() {
	fmt.Printf("%g\n", math.FMA(-1, 1, 1))
	fmt.Printf("%g\n", portableFMA(-1, 1, 1))
}

What did you expect to see?

0
0

What did you see instead?

0
-0

FMA calculates x*y+z. In the portable implementation of FMA if |x*y| is equal to |z| the sign bit of the result is set to the sign bit of x*y. If x*y is negative and z is positive this results in the incorrect result -0.

FMA is an intrinsic on some platforms. This issue only affects the portable implementation (hence the different results when calling FMA directly vs. indirectly).

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions