Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strconv: ftoa bug #2917

Closed
rsc opened this issue Feb 8, 2012 · 10 comments
Closed

strconv: ftoa bug #2917

rsc opened this issue Feb 8, 2012 · 10 comments
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Feb 8, 2012

not sure what happened or if it is true
http://build.golang.org/log/dd288a49040589c8e89101d9872e6fcbb3a2c0b9

--- FAIL: TestAtofRandom (0.01 seconds)
       atof_test.go:221: number 4.87402195346389e+27 badly parsed as 8865794286000692p+39 (expected 8865794286000691p+39)
       atof_test.go:226: tested 10000 random numbers
FAIL
@alberts
Copy link
Contributor

alberts commented Feb 8, 2012

Comment 1:

Could be related to issue #2894?

@remyoudompheng
Copy link
Contributor

Comment 2:

8865794286000691.5 * 1<<39 is 48740219534638900001133035520 so it is a real issue.

@remyoudompheng
Copy link
Contributor

Comment 3:

Weird. I can't actually reproduce.

@rsc
Copy link
Contributor Author

rsc commented Feb 8, 2012

Comment 4:

Reproduced.  The failure is in ftoa, which is being used to generate the input for atof.
diff -r 001b807ba435 src/pkg/strconv/ftoa_test.go
--- a/src/pkg/strconv/ftoa_test.go  Wed Feb 08 00:22:38 2012 -0500
+++ b/src/pkg/strconv/ftoa_test.go  Wed Feb 08 08:36:30 2012 -0500
@@ -128,6 +128,9 @@
    // issue #2625.
    {383260575764816448, 'f', 0, "383260575764816448"},
    {383260575764816448, 'g', -1, "3.8326057576481645e+17"},
+
+   // issue #2917.
+   {8865794286000691<<39, 'g', -1, "4.8740219534638897e+27"},
 }
 
 func TestFtoa(t *testing.T) {

@remyoudompheng
Copy link
Contributor

Comment 6:

If x = 8865794286000691<<39, the shortest print of x would be
D = 4.87402195346389e+27 because x < D < halfway < nextafter(x).
The halfway is 48740219534638900001... 
func main() {
      s := strconv.FormatFloat(8865794286000691<<39, 'g', -1, 64)
      fmt.Println(s)
      x, _ := strconv.ParseFloat("4.87402195346389e+27", 64)
      fmt.Println(int64(x / (1 << 39)))
}
prints 
4.87402195346389e+27
8865794286000691
as expected. Do you confirm? These atof/ftoa always make me go nuts.

@rsc
Copy link
Contributor Author

rsc commented Feb 8, 2012

Comment 7:

I think you are right that I confused myself.  Will take another look later.

@rsc
Copy link
Contributor Author

rsc commented Feb 11, 2012

Comment 8:

I agree: this is working fine.  The existence of the message
>  number 4.87402195346389e+27 badly parsed as 8865794286000692p+39 (expected
8865794286000691p+39)
suggests that the random number generator generated 8865794286000691p+39 and then
FormatFloat formatted it (correctly) as 4.87402195346389e+27, and then ParseFloat parsed
it (incorrectly) to 8865794286000692p+39.  But of course ParseFloat does not do that if
we just run the obvious code.  I will add this test case to the test suite, just in case
it turns up on some other machine again.  And we can chalk it up to a bad builder until
then.

Owner changed to @rsc.

Status changed to Started.

@rsc
Copy link
Contributor Author

rsc commented Feb 13, 2012

Comment 9:

This issue was closed by revision f7a3683.

Status changed to Fixed.

@rsc
Copy link
Contributor Author

rsc commented Feb 13, 2012

Comment 10:

Sure enough, this test case failed on the darwin/386 builder
but not anywhere else.  I wonder if the FPU has the wrong rounding
mode or (more likely) is using 80-bit registers by default.
We fix the control word on Linux; maybe we have to do it on Darwin too.
Will look into it using the actual builder machine.

Status changed to Accepted.

@rsc
Copy link
Contributor Author

rsc commented Feb 14, 2012

Comment 11:

This issue was closed by revision 1707a99.

Status changed to Fixed.

@rsc rsc added fixed labels Feb 14, 2012
@rsc rsc self-assigned this Feb 14, 2012
@rsc rsc added this to the Go1 milestone Apr 10, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants