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

Go is bad at floating point numbers #41923

Closed
vj0R opened this issue Oct 11, 2020 · 1 comment
Closed

Go is bad at floating point numbers #41923

vj0R opened this issue Oct 11, 2020 · 1 comment

Comments

@vj0R
Copy link

@vj0R vj0R commented Oct 11, 2020

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

$ go version
go version go1.14.7 linux/amd64

Does this issue reproduce with the latest release? Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/vj0r/GOLANG/bin"
GOCACHE="/home/vj0r/.cache/go-build"
GOENV="/home/vj0r/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/vj0r/GOLANG"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.14/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build355855424=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Provide code that calculates the average of the floating point numbers read from a file
If the file with numbers of size 50M the count is performed flawlessly. And the values are the same from other programming languages.
If the file is 245M in size, then Go does not count correctly. Most likely, there is an accumulation of errors.

File from go

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {

	file, err := os.Open("./file")
	panicm(err)
	read := bufio.NewScanner(file)
	var sum float32 = 0
	var i int = 0
	for read.Scan() {

		res := strings.TrimSpace(read.Text())
		r1, err := strconv.ParseFloat(res, 32)
		panicm(err)
		sum += float32(r1)
		i++
	}

	err = file.Close()
	panicm(err)

	fmt.Printf("%F\n", sum/float32(i))
}

func panicm(err error) {
	if err != nil {
		log.Panic(err)
	}
}

file with numbers like:

$ cat file
56.2
89.5
71.8
56.2
......
$ du -sh file
245M	file
go build loadfile.go 
$ ./loadfile 
101.770912
$ perl -e 'open(fd,"file"); $sum=0; $i=0; while(){$sum+=$_; $i++} print $sum/$i'
150.811212282527
$awk 'BEGIN {sum=0; count=0} {sum+=$1; count++} END{print sum/count}' file
150.811
$ cat loadfile.py
#!/usr/bin/python3
f = open("./file")
sum=0
i=0
for ch in f.readlines():
    sum+=float(ch)
    i+=1
print(sum/i)
$ ./loadfile.py
150.8112122825271

What did you expect to see?

150.811212

What did you see instead?

101.770912

@vj0R
Copy link
Author

@vj0R vj0R commented Oct 11, 2020

should be used float64

@vj0R vj0R closed this Oct 11, 2020
@ALTree ALTree changed the title Golang is bad at floating point numbers Go is bad at floating point numbers Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.