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

affected/package: standard implementation of division by 0 #64334

Closed
codelinx opened this issue Nov 22, 2023 · 2 comments
Closed

affected/package: standard implementation of division by 0 #64334

codelinx opened this issue Nov 22, 2023 · 2 comments

Comments

@codelinx
Copy link

codelinx commented Nov 22, 2023

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

$ go version 1.21.3

Does this issue reproduce with the latest release?

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

go env Output
$ go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/USERNAME/.cache/go-build'
GOENV='/home/USERNAME/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/USERNAME/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/USERNAME/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1569631650=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Division by 0 panic

What did you expect to see?

Expected: value at iteration + 1 to evaluate to 1 + iteration of range and not a panic as if 0 was the total value being divided by

n := len(s)/i + 1 // expected for division to start with numerator 1 + current count ie. at least 1

Working version:

n := len(s)/1 + i  // expected for division to start with numerator 1 + current count ie. at least 1

What did you see instead?

[Fatal panic:](panic: runtime error: integer divide by zero)

@mauri870
Copy link
Member

Check the Operator precedence section https://go.dev/ref/spec

That is expected behavior, the / operator has a higher precedence than +.

@mauri870 mauri870 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2023
@codelinx
Copy link
Author

codelinx commented Nov 22, 2023

thanks! wrapping the values in parenthesis fixes the issue and you are correct the operator precedence is the issue:
n := len(s)/ (1 + i) or flipping the values works n := len(s)/ 1+ i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants