Beginners on fmt.SScanf (possible bug?)

I’m trying to parse “1.3p” as “%f%c” with fmt.Sscanf and always get 0.0 for the float. Is this a bug?

Check at

https://play.golang.org/p/2bEdp4PlaGe

Two things; you’ve confused the argument order for Sscanf - the first parameter is the data, the second is the format, and scanf requires white space separated fields.

Sscanf also returns an error that you can inspect to understand the root cause when it does not scan as you expect.

1 Like

Thanks for the initial reply

Another example:

https://play.golang.org/p/6qLU1u7da0D

Why does it work with ‘a’ but not with ‘p’ ???

Thanks, /PA

I had no idea, but it’s an interesting question.

https://play.golang.org/p/1g-FkWErwyY

Both “p” and “e” can occur in floating point constants, thus matching %f. But they need something following them. So “1.2p” looks like the start of a floating point constant and is consumed, but then not parsed successfully, by the %f verb.

Again, Scanf likes white space separated things.

Hmmm haven’t found the p in the definition of floats. Maybe the doc is not so explicit about it. Anyhow, thanks

You know that 1.2e0 means 1.2 × 10⁰, 1.2e1 means 1.2 × 10¹, 1.2e2 means 1.2 × 10², etc.

1.2p0 = 1.2 × 2⁰
1.2p1 = 1.2 × 2¹
1.2p2 = 1.2 × 2²
etc.

“p” stands for “power of 2”.

This is why it gives that weird error. It expects a number after the p and there’s none.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.