Skip to content

Commit

Permalink
rename ToIntegral variants
Browse files Browse the repository at this point in the history
Fixes #48
  • Loading branch information
maddyblue committed Jun 30, 2017
1 parent a283b71 commit 83a2081
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
9 changes: 5 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,9 @@ func (c *Context) toIntegralSpecials(d, x *Decimal) (bool, Condition, error) {
return false, 0, nil
}

// ToIntegral sets d to integral value of x. Inexact and Rounded flags are ignored and removed.
func (c *Context) ToIntegral(d, x *Decimal) (Condition, error) {
// RoundToIntegralValue sets d to integral value of x. Inexact and Rounded flags
// are ignored and removed.
func (c *Context) RoundToIntegralValue(d, x *Decimal) (Condition, error) {
if set, res, err := c.toIntegralSpecials(d, x); set {
return res, err
}
Expand All @@ -1236,8 +1237,8 @@ func (c *Context) ToIntegral(d, x *Decimal) (Condition, error) {
return c.goError(res)
}

// ToIntegralX sets d to integral value of x.
func (c *Context) ToIntegralX(d, x *Decimal) (Condition, error) {
// RoundToIntegralExact sets d to integral value of x.
func (c *Context) RoundToIntegralExact(d, x *Decimal) (Condition, error) {
if set, res, err := c.toIntegralSpecials(d, x); set {
return res, err
}
Expand Down
12 changes: 6 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ func (e *ErrDecimal) Sub(d, x, y *Decimal) *Decimal {
return e.op3(d, x, y, e.Ctx.Sub)
}

// ToIntegral performs e.Ctx.ToIntegral(d, x) and returns d.
func (e *ErrDecimal) ToIntegral(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.ToIntegral)
// RoundToIntegralValue performs e.Ctx.RoundToIntegralValue(d, x) and returns d.
func (e *ErrDecimal) RoundToIntegralValue(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.RoundToIntegralValue)
}

// ToIntegralX performs e.Ctx.ToIntegralX(d, x) and returns d.
func (e *ErrDecimal) ToIntegralX(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.ToIntegralX)
// RoundToIntegralExact performs e.Ctx.RoundToIntegralExact(d, x) and returns d.
func (e *ErrDecimal) RoundToIntegralExact(d, x *Decimal) *Decimal {
return e.op2(d, x, e.Ctx.RoundToIntegralExact)
}
24 changes: 12 additions & 12 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func ExampleErrDecimal() {
// Infinity, err: division by zero
}

// ExampleToIntegralX demonstrates how to use ToIntegralX to check if a
// number is an integer or not. Note the variations between integer (which
// allows zeros after the decimal point) and strict (which does not). See
// ExampleRoundToIntegralExact demonstrates how to use RoundToIntegralExact to
// check if a number is an integer or not. Note the variations between integer
// (which allows zeros after the decimal point) and strict (which does not). See
// the documentation on Inexact and Rounded.
func ExampleContext_ToIntegralX() {
func ExampleContext_RoundToIntegralExact() {
inputs := []string{
"123.4",
"123.0",
Expand All @@ -116,19 +116,19 @@ func ExampleContext_ToIntegralX() {
}
for _, input := range inputs {
d, _, _ := apd.NewFromString(input)
res, _ := apd.BaseContext.ToIntegralX(d, d)
res, _ := apd.BaseContext.RoundToIntegralExact(d, d)
integer := !res.Inexact()
strict := !res.Rounded()
fmt.Printf("input: % 6s, ToIntegralX: % 3s, integer: % 5t, strict: % 5t, res:", input, d, integer, strict)
fmt.Printf("input: % 6s, output: % 3s, integer: % 5t, strict: % 5t, res:", input, d, integer, strict)
if res != 0 {
fmt.Printf(" %s", res)
}
fmt.Println()
}
// Output: input: 123.4, ToIntegralX: 123, integer: false, strict: false, res: inexact, rounded
// input: 123.0, ToIntegralX: 123, integer: true, strict: false, res: rounded
// input: 123, ToIntegralX: 123, integer: true, strict: true, res:
// input: 12E1, ToIntegralX: 120, integer: true, strict: true, res:
// input: 120E-1, ToIntegralX: 12, integer: true, strict: false, res: rounded
// input: 120E-2, ToIntegralX: 1, 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:
// input: 120E-1, output: 12, integer: true, strict: false, res: rounded
// input: 120E-2, output: 1, integer: false, strict: false, res: inexact, rounded
}
4 changes: 2 additions & 2 deletions gda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ func (tc TestCase) Run(c *Context, done chan error, d, x, y *Decimal) (res Condi
case "subtract":
res, err = c.Sub(d, x, y)
case "tointegral":
res, err = c.ToIntegral(d, x)
res, err = c.RoundToIntegralValue(d, x)
case "tointegralx":
res, err = c.ToIntegralX(d, x)
res, err = c.RoundToIntegralExact(d, x)

// Below used only in benchmarks. Tests call it themselves.
case "comparetotal":
Expand Down

0 comments on commit 83a2081

Please sign in to comment.