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

Bug multiplying a float32(variable(int)) with a float32 variable #35674

Closed
gonzalochief opened this issue Nov 18, 2019 · 4 comments
Closed

Bug multiplying a float32(variable(int)) with a float32 variable #35674

gonzalochief opened this issue Nov 18, 2019 · 4 comments

Comments

@gonzalochief
Copy link

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

go version go1.13.4 darwin/amd64

Does this issue reproduce with the latest release?

I have not tested it

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

Mac OS Catalina

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/macbook/Library/Caches/go-build"
GOENV="/Users/macbook/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/macbook/golib:/Users/macbook/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
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/c3/nyj9fvvd1vn375qfvw78t3mc0000gn/T/go-build544665324=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have a int slice with a value 155250 and multiply it by another float32 slice variable (519.0) using the float32(int) function. The result was 80574752, instead of 80574750.
The error does not show when converting to float64 variable. The issue must be in the float32() function

What did you expect to see?

80574750

What did you see instead?

80574752

@gonzalochief
Copy link
Author

gonzalochief commented Nov 18, 2019

I also replicated the issue in the Golang playground:

package main

import "fmt"

func main() {
	var x int32 = 155250
	var y float32 = 519
	fmt.Printf("%0.8f\n", float32(x)*(y))//80,574,752
	fmt.Printf("%0.8f\n", float32(155250)*(519.00))//80,574,752
	fmt.Printf("%0.8f\n", float64(155250)*float64(519.00))//80,574,750
}

@randall77
Copy link
Contributor

This is how float32 works.
The number 80574750 is not exactly representable in float32. It rounds to 80574752.

	fmt.Printf("%f\n", float32(80574750))

This also prints 80574752.

@dmitshur

This comment has been minimized.

@gonzalochief
Copy link
Author

Thanks for your answers and suggestions. In fact it was as you said. a precision issue.
I have moved all variables to float 64 and it worked

@golang golang locked and limited conversation to collaborators Nov 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants