Skip to content

Commit

Permalink
Exact() works like String() with <= 16 digits in mantissa.
Browse files Browse the repository at this point in the history
  • Loading branch information
keep94 committed Sep 12, 2024
1 parent 2ad3f29 commit 2ef645a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion v3/sqroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (n *FiniteNumber) Format(state fmt.State, verb rune) {
// the exact representation of n.
func (n *FiniteNumber) Exact() string {
var builder strings.Builder
fs := formatSpecForG(endOf(n), n.exponent, false)
fs := formatSpecForG(max(endOf(n), gPrecision), n.exponent, false)
fs.PrintNumber(&builder, n)
return builder.String()
}
Expand Down
11 changes: 9 additions & 2 deletions v3/sqroot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ func TestExactShort(t *testing.T) {
n, _ := NewNumberForTesting([]int{5, 0, 0, 1}, nil, 3)
assert.Equal(t, "500.1", n.WithSignificant(20).Exact())
assert.Equal(t, "500", n.WithSignificant(3).Exact())
assert.Equal(t, "0.50e+03", n.WithSignificant(2).Exact())
assert.Equal(t, "0.5e+03", n.WithSignificant(1).Exact())
assert.Equal(t, "500", n.WithSignificant(2).Exact())
assert.Equal(t, "500", n.WithSignificant(1).Exact())
assert.Equal(t, "0", n.WithSignificant(0).Exact())
smallN := n.withExponent(-3)
assert.Equal(t, "0.0005001", smallN.WithSignificant(4).Exact())
Expand All @@ -228,6 +228,13 @@ func TestNewFiniteNumber(t *testing.T) {
assert.Equal(t, "20.5", n.Exact())
}

func TestNewBigFiniteNumber(t *testing.T) {
n, err := NewFiniteNumber([]int{2, 0, 5}, 4)
assert.NoError(t, err)
assert.Equal(t, "2050", n.String())
assert.Equal(t, "2050", n.Exact())
}

func TestNewFiniteNumberZero(t *testing.T) {
n, err := NewFiniteNumber(nil, 2)
assert.NoError(t, err)
Expand Down

0 comments on commit 2ef645a

Please sign in to comment.