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

FormatFloat() overflow bug on 32bit systems #46

Closed
TerraTech opened this issue Jan 9, 2017 · 3 comments
Closed

FormatFloat() overflow bug on 32bit systems #46

TerraTech opened this issue Jan 9, 2017 · 3 comments

Comments

@TerraTech
Copy link

I ran into a bug tonight compiling a 32bit version and found FormatFloat() was overflowing an int somewhere. My program is dealing with nanosecond time calculations and I'm humanizing a scaling factor.

Interestingly enough, Commaf() doesn't have the same problem.

package main
import (
	"fmt"
	"math"
	"github.com/dustin/go-humanize"
)

func main() {
	var a float64 = 52746220055.92342
	fmt.Println(math.MaxFloat64)
	fmt.Println(humanize.Commaf(a))
	fmt.Println(">>> " + humanize.FormatFloat("#,###.##", a))
}
#64bit
$ go run main.go
1.7976931348623157e+308
52,746,220,055.92342
>>> 52,746,220,055.92

#32bit
$ GOARCH=386 go run main.go
1.7976931348623157e+308
52,746,220,055.92342
>>> -2,147,483,648.92
@dmitshur
Copy link
Collaborator

dmitshur commented Jan 9, 2017

There are 3 places or so inside humanize.FormatFloat that use int. I suspect that's where the issue likely lies. A fix could be to always use int64.

@dustin
Copy link
Owner

dustin commented Jan 9, 2017 via email

@dmitshur
Copy link
Collaborator

dmitshur commented Jan 9, 2017

Anyone happen to know how to force 32-bit for tests?

Perhaps something like:

GOARCH=386 go test

travis-ci/travis-ci#6126 appears to be related.

@dustin dustin closed this as completed in 7a41df0 Jan 10, 2017
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

3 participants