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

math: math.E returns incorrect result #66198

Closed
EvilRedHorse opened this issue Mar 8, 2024 · 5 comments
Closed

math: math.E returns incorrect result #66198

EvilRedHorse opened this issue Mar 8, 2024 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@EvilRedHorse
Copy link

Go version

go version go1.20.11 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.11"
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 -fdebug-prefix-map=/tmp/go-build4055892220=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Attempted to print e with 64 significant digits.

command:
fmt.Printf(("%"+"."+"64"+"f"), math.E)

What did you see happen?

output:
2.7182818284590450907955982984276488423347473144531250000000000000

precision:
inaccurate past 15 digits of precision

What did you expect to see?

command:
echo "scale=64;e(1)" | bc -l

output:
2.7182818284590452353602874713526624977572470936999595749669676277

precision:
accurate to requested 64 significant digits.

relates to:
#9545 #9546 #25270

@mknyszek
Copy link
Contributor

mknyszek commented Mar 8, 2024

I'm pretty sure when you go to print it, the constant is converted from an untyped float into a float64, and a float64 does not have anywhere near 64 decimal digits of precision.

https://pkg.go.dev/math/big#Float on the other hand may be able to do what you want. For example, https://go.dev/play/p/XysLL10W6GL. (I copied the constant from the math package, which only has 62 digits after the decimal point, but you could add and represent more.)

I'm not sure there's anything actionable here. This seems working as intended.

CC @griesemer but closing for now.

@mknyszek mknyszek closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 2024
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 8, 2024
@mknyszek mknyszek added this to the Backlog milestone Mar 8, 2024
@EvilRedHorse
Copy link
Author

e is inaccurate to the requested 64 significant digits.
The example you give does not use math.E instead you have directly input/output a float.
Why would math/big package be needed?

@ianlancetaylor
Copy link
Contributor

The Go float64 type, which is what your example is using, is not accurate to 64 decimal digits.

See https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html for more background.

@EvilRedHorse
Copy link
Author

EvilRedHorse commented Mar 9, 2024

  1. So a float64 can represent 64 digits but only be precise to 15 digits.

  2. math/big is required to be more precise.

  3. flag does not have a math/big float.

relates to:
#45751

"Based on the discussion this proposal will clearly not be accepted, so closing. Please comment if you disagree. Thanks."

Therefore using "out of the box" golang math/flag you are limited to 15 significant digits.

@ianlancetaylor
Copy link
Contributor

Fortunately the flag package is extensible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants