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

math/big: Float.Text does not correctly handle x format #34343

Closed
larhun opened this issue Sep 17, 2019 · 3 comments
Closed

math/big: Float.Text does not correctly handle x format #34343

larhun opened this issue Sep 17, 2019 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@larhun
Copy link

larhun commented Sep 17, 2019

What did you do?

see https://play.golang.org/p/IviJWtsbRoW

package main

import (
	"fmt"
	"math/big"
)

func main() {
	var a, _, _ = big.ParseFloat("0x.8p-2147483648", 0, 4, big.ToNearestEven)
	var b, _, _ = big.ParseFloat("0x.8p-2147483647", 0, 4, big.ToNearestEven)

	fmt.Println("a = (x format)", a.Text('x', -1), "(p format)", a.Text('p', -1))
	fmt.Println("b = (x format)", b.Text('x', -1), "(p format)", b.Text('p', -1))
}

What did you expect to see?

a = (x format) 0x1p-2147483649 (p format) 0x.8p-2147483648
b = (x format) 0x1p-2147483648 (p format) 0x.8p-2147483647

What did you see instead?

a = (x format) 0x1p+2147483647 (p format) 0x.8p-2147483648
b = (x format) 0x1p-2147483648 (p format) 0x.8p-2147483647

Please note the wrong +2147483647 exponent for a.Text('x', -1).

@toothrot toothrot added this to the Go1.14 milestone Sep 17, 2019
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 17, 2019
@toothrot
Copy link
Contributor

I agree that the sign looks incorrect here. I think that @griesemer would know more.

@griesemer
Copy link
Contributor

Yes, this is a bug. The problem is that the exponent underflows from -2147483648 to 2147483647 since big.Floats "only" support 32bit exponents.

@griesemer griesemer self-assigned this Sep 17, 2019
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/196057 mentions this issue: math/big: avoid MinExp exponent wrap-around in 'x' Text format

@golang golang locked and limited conversation to collaborators Sep 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants