Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: miscompilation of float64 comparison and bool comparison #34587

Open
johnfercher opened this issue Sep 28, 2019 · 3 comments

Comments

@johnfercher
Copy link

commented Sep 28, 2019

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

$ go version
go version go1.12.4 darwin/amd64

Does this issue reproduce with the latest release?

No.

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jfercher/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jfercher/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7p/3q7bnlnd3h9fk22hlpgngz0r0000gq/T/go-build807819520=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

totalOffsetY := self.offsetY+height+self.footerHeight
maxOffsetPage := pageHeight-bottom-top
greaterThan := totalOffsetY > maxOffsetPage

// At this point totalOffsetY is float64{7}
// maxOffsetPage is float64{80}
// and greaterThan is bool{false}.
if greaterThan == true {
	// ..
}

The greaterThan comparison is returning true.

What did you expect to see?

false must be false

What did you see instead?

false is true

@agnivade

This comment has been minimized.

Copy link
Member

commented Sep 28, 2019

Please provide a playground link which shows this.

@mdempsky mdempsky changed the title If, always being true after float64 comparison cmd/compile: miscompilation of float64 comparison and bool comparison Oct 3, 2019
@mdempsky

This comment has been minimized.

Copy link
Member

commented Oct 3, 2019

A playground link is ideal, but any way to easily reproduce this would be a good start.

@odeke-em

This comment has been minimized.

Copy link
Member

commented Oct 15, 2019

@johnfercher please use/modify this seed program that I derived from your report, to perhaps provide a repro for the problem otherwise this issue isn't actionable

package main

import "fmt"

type rect struct {
	offsetY      float64
	footerHeight float64
}

func main() {
	self := &rect{
		offsetY:      5,
		footerHeight: 1,
	}
	do(self)
}

func do(self *rect) {
	height := float64(1)
	pageHeight := float64(100)
	bottom := float64(10)
	top := float64(10)
	totalOffsetY := self.offsetY + height + self.footerHeight
	maxOffsetPage := pageHeight - bottom - top
	greaterThan := totalOffsetY > maxOffsetPage

	// At this point totalOffsetY is float64{7}
	// maxOffsetPage is float64{80}
	// and greaterThan is bool{false}.
	fmt.Printf("totalOffsetY: %.2f maxOffsetPage: %.2f\n", totalOffsetY, maxOffsetPage)
	if greaterThan == true {
		panic("greaterThan is supposed to be false")
	}
}

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.