Skip to content

Commit

Permalink
Merge pull request #115 from nvanbenschoten/nvanbenschoten/stripZeros
Browse files Browse the repository at this point in the history
apd: don't strip trailing zeros in Quo and Sqrt
  • Loading branch information
nvanbenschoten authored Jan 31, 2022
2 parents 94925d2 + 9f01ff4 commit 5684ba3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 0 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ func (c *Context) Quo(d, x, y *Decimal) (Condition, error) {
}

res |= d.setExponent(c, nd, res, shift, -adjCoeffs, -adjExp10)
d.Reduce(d) // remove trailing zeros
return c.goError(res)
}

Expand Down Expand Up @@ -552,7 +551,6 @@ func (c *Context) Sqrt(d, x *Decimal) (Condition, error) {
d.Exponent += int32(e / 2)
nc.Precision = c.Precision
nc.Rounding = RoundHalfEven
d.Reduce(d) // remove trailing zeros
res := nc.round(d, d)
return nc.goError(res)
}
Expand Down
19 changes: 12 additions & 7 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func ExampleContext_overflow() {
return
}
}
// Output: d: 998, overflow: false, err: <nil>
// Output:
// d: 998, overflow: false, err: <nil>
// d: 999, overflow: false, err: <nil>
// d: Infinity, overflow: true, err: overflow
}
Expand All @@ -56,9 +57,10 @@ func ExampleContext_inexact() {
return
}
}
// Output: d: 9, inexact: false, err: <nil>
// d: 3, inexact: false, err: <nil>
// d: 1, inexact: false, err: <nil>
// Output:
// d: 9.0000, inexact: false, err: <nil>
// d: 3.0000, inexact: false, err: <nil>
// d: 1.0000, inexact: false, err: <nil>
// d: 0.33333, inexact: true, err: <nil>
}

Expand All @@ -74,7 +76,8 @@ func ExampleContext_Quantize() {
}
fmt.Println()
}
// Output: -3: 123.450
// Output:
// -3: 123.450
// -2: 123.45
// -1: 123.5 (inexact, rounded)
// 0: 123 (inexact, rounded)
Expand All @@ -95,7 +98,8 @@ func ExampleErrDecimal() {
ed.Sub(d, d, apd.New(1, 0)) // attempt to subtract 1
// The subtraction doesn't occur and doesn't change the error.
fmt.Printf("%s, err: %v\n", d, ed.Err())
// Output: 10, err: <nil>
// Output:
// 10, err: <nil>
// 30, err: <nil>
// Infinity, err: division by zero
// Infinity, err: division by zero
Expand Down Expand Up @@ -125,7 +129,8 @@ func ExampleContext_RoundToIntegralExact() {
}
fmt.Println()
}
// Output: input: 123.4, output: 123, integer: false, strict: false, res: inexact, rounded
// Output:
// input: 123.4, output: 123, integer: false, strict: false, res: inexact, rounded
// input: 123.0, output: 123, integer: true, strict: false, res: rounded
// input: 123, output: 123, integer: true, strict: true, res:
// input: 12E1, output: 120, integer: true, strict: true, res:
Expand Down

0 comments on commit 5684ba3

Please sign in to comment.