Float32 -> string --> Float32 round trip not possible for strconv.FormatFloat 'b' ?

90 views
Skip to first unread message

Glen Newton

unread,
Dec 9, 2021, 7:35:36 PM12/9/21
to golang-nuts
Hello,

I am using strconv.FormatFloat[1] and strconv.ParseFloat[2]to convert float32s back-and-forth in a library I am working on. In my tests I decided to test all formats for FormatFloat ('b', 'e', 'E', 'f', 'g', 'G', 'x', 'X'). However, it seems like the 'b' format is not supported by ParseFloat (see [3]).

Is this correct? Is my code showing this correct?[3]
If Yes, is there some reason why the one format ('b') is not supported by ParseFloat?

My question is more one driven by curiosity rather than a need for this format. It was a surprise to me that one of the formats generated by the complement function in the same module was not supported.

Thanks,
Glen

[1]https://pkg.go.dev/strconv#FormatFloat

Kurtis Rader

unread,
Dec 9, 2021, 8:23:20 PM12/9/21
to Glen Newton, golang-nuts
Note that the documentation for ParseFloat states: "ParseFloat accepts decimal and hexadecimal floating-point number syntax." In other words, it explicitly does not support the binary format emitted by FormatFloat('b'). I don't know why it doesn't but I'd bet that a review of old issues and changes to the source would probably shed some light on the reason.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/cd22042a-cd35-484b-af86-9f3d6f918376n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Dan Kortschak

unread,
Dec 9, 2021, 11:20:11 PM12/9/21
to golan...@googlegroups.com
On Thu, 2021-12-09 at 17:22 -0800, Kurtis Rader wrote:
> Note that the documentation for ParseFloat states: "ParseFloat
> accepts decimal and hexadecimal floating-point number syntax." In
> other words, it explicitly does not support the binary format emitted
> by FormatFloat('b'). I don't know why it doesn't but I'd bet that a
> review of old issues and changes to the source would probably shed
> some light on the reason.
>

This gives some background: https://github.com/golang/go/issues/12518


Glen Newton

unread,
Dec 10, 2021, 2:12:20 AM12/10/21
to golang-nuts
Thanks to both of you for this! Appreciated.  :-)

Glen
Reply all
Reply to author
Forward
0 new messages