Syntax allowed by strconv.ParseFloat?

166 views
Skip to first unread message

ben...@gmail.com

unread,
Jun 3, 2022, 1:26:44 AM6/3/22
to golang-dev
Hi again,

(I'm sending this to golang-dev and not golang-nuts because I think a documentation improvement is probably needed.)

The documentation for strconv.ParseFloat mentions that it "accepts decimal and hexadecimal floating-point number syntax", but it doesn't specify what those formats entail. The doc for FormatFloat mentions that the 'x' format gives "-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent", but that's a bit vague, and ParseFloat doesn't refer to that in its doc in any case.

So it's unclear exactly what syntax ParseFloat supports. I think it's important to specify a syntax, particularly for hexadecimal floating point, which isn't particularly common. For example, I was surprised to find "0x1.8" wasn't allowed (an exponent of "p0" is required, as in "0x1.8p0").

It wouldn't be hard to update the docs to have a simple representation of the grammar it supports, for example [±]ddd[.dddd][e[±]ddd] for decimal and [±]0xd.ddddp[±]ddd for hex (or whatever they are exactly). Or we could go full BNF-style notation, but that's probably too much for these docs. Or maybe it's as simple as saying "ParseFloat accepts decimal and hexadecimal floating-point numbers using the same syntax as floating-point literals in Go source code [link to spec section]" ... assuming that's correct.

Yes, in fact, now that I look at https://go.dev/ref/spec#Floating-point_literals, that's presumably what ParseFloat does support. Is that correct? If so, I can submit a doc update CL.

-Ben

Ian Lance Taylor

unread,
Jun 4, 2022, 12:02:58 AM6/4/22
to ben...@gmail.com, golang-dev
Yes, the intent is that ParseFloat accepts exactly the syntax that the
language accepts.

Ian

ben...@gmail.com

unread,
Jun 4, 2022, 2:34:01 AM6/4/22
to golang-dev

I'm pretty sure I've got the new [link] syntax right, but I haven't tested it -- godoc doesn't seem to support the new syntax.

-Ben
Reply all
Reply to author
Forward
0 new messages