Run this code to see what I mean: Go Playground demo of issue
package main
import (
"fmt"
"strconv"
)
func main() {
// From https://golang.org/src/math/const.go
var SmallestNonzeroFloat64AsString string = "4.940656458412465441765687928682213723651e-324"
var SmallestNonzeroFloat64 float64
var err error
SmallestNonzeroFloat64, err = strconv.ParseFloat(SmallestNonzeroFloat64AsString, 64)
if err != nil {
panic(err)
}
fmt.Printf("SmallestNonzeroFloat64 = %g
", SmallestNonzeroFloat64)
fmt.Printf("SmallestNonzeroFloat64 = %s
", strconv.FormatFloat(SmallestNonzeroFloat64, 'f', -1, 64))
}
SmallestNonzeroFloat64
is defined in math/const.go and I assumed it can be represented by a float64 variable.
But when it is parsed into a float64 with strconv.ParseFloat()
and printed with strconv.FormatFloat()
the result is rounded.
Instead of 4.940656458412465441765687928682213723651e-324
I get 5e-324
(or its non-exponent equivalent, which you can see in the Go Playground results). The result is rounded.
Is there a way to get back the 4.940656458412465441765687928682213723651e-324
?
Or is it a bug?