fmt.Scan and NaN, +Inf, -Inf

148 views
Skip to first unread message

Remi Gillig

unread,
Nov 21, 2010, 4:52:54 PM11/21/10
to golang-nuts
Hello,

I did some tests recently and wondered how these cases were handled :

package main

import (
"fmt"
"math"
)

func main() {
// NaN
{
var f float = float(math.NaN())
s := fmt.Sprintf("%f", f)
fmt.Printf("[%s]\n", s)

var f2 float
_, err := fmt.Sscanf(s, "%f", &f2)

fmt.Printf("%#v %f\n", err, f2)
}
// +Inf
{
var f float = float(math.Inf(1))
s := fmt.Sprintf("%f", f)
fmt.Printf("[%s]\n", s)

var f2 float
_, err := fmt.Sscanf(s, "%f", &f2)

fmt.Printf("%#v %f\n", err, f2)
}
// -Inf
{
var f float = float(math.Inf(-1))
s := fmt.Sprintf("%f", f)
fmt.Printf("[%s]\n", s)

var f2 float
_, err := fmt.Sscanf(s, "%f", &f2)

fmt.Printf("%#v %f\n", err, f2)
}
}

It prints correctly but can't read back the strings. Is this intended?

[NaN]
&strconv.NumError{Num:"", Error:536870951} 0.000000
[+Inf]
&strconv.NumError{Num:"+", Error:536870951} 0.000000
[-Inf]
&strconv.NumError{Num:"-", Error:536870951} 0.000000

In my mind, I thought Print and Scan were symmetric on these cases.

Remi Gillig.

Andrew Gerrand

unread,
Nov 29, 2010, 5:19:45 PM11/29/10
to Remi Gillig, golang-nuts

Yes, they should be symmetric. Can you please file an issue on the bug tracker?

http://code.google.com/p/go/issues

Thanks.

Remi Gillig

unread,
Nov 29, 2010, 5:59:15 PM11/29/10
to Andrew Gerrand, golang-nuts
Filed as issue 1308 : http://code.google.com/p/go/issues/detail?id=1308

Remi Gillig.
Reply all
Reply to author
Forward
0 new messages