Skip to content

Commit

Permalink
Merge branch 'master' into add-contributing-section
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlagergren authored Sep 12, 2019
2 parents edb2081 + c9bb3d8 commit 6f46dc7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
15 changes: 9 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,13 @@ type RoundingMode uint8

// The following rounding modes are supported.
const (
ToNearestEven RoundingMode = iota // == IEEE 754-2008 roundTiesToEven
ToNearestAway // == IEEE 754-2008 roundTiesToAway
ToZero // == IEEE 754-2008 roundTowardZero
AwayFromZero // no IEEE 754-2008 equivalent
ToNegativeInf // == IEEE 754-2008 roundTowardNegative
ToPositiveInf // == IEEE 754-2008 roundTowardPositive
ToNearestEven RoundingMode = iota // == IEEE 754-2008 roundTiesToEven
ToNearestAway // == IEEE 754-2008 roundTiesToAway
ToZero // == IEEE 754-2008 roundTowardZero
AwayFromZero // no IEEE 754-2008 equivalent
ToNegativeInf // == IEEE 754-2008 roundTowardNegative
ToPositiveInf // == IEEE 754-2008 roundTowardPositive
ToNearestTowardZero // no IEEE 754-2008 equivalent

unnecessary // placeholder for x / y with UnlimitedPrecision.
)
Expand Down Expand Up @@ -185,6 +186,8 @@ func (m RoundingMode) needsInc(odd bool, r int, pos bool) bool {
return odd
case ToNearestAway:
return r >= 0
case ToNearestTowardZero:
return r > 0
default:
return false
}
Expand Down
39 changes: 39 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,42 @@ func TestCondition_String(t *testing.T) {
}
}
}

func TestNonStandardRoundingModes(t *testing.T) {
for i, test := range [...]struct {
value int64
mode RoundingMode
expected int64
}{
{55, ToNearestTowardZero, 5},
{25, ToNearestTowardZero, 2},
{16, ToNearestTowardZero, 2},
{11, ToNearestTowardZero, 1},
{10, ToNearestTowardZero, 1},
{-10, ToNearestTowardZero, -1},
{-11, ToNearestTowardZero, -1},
{-16, ToNearestTowardZero, -2},
{-25, ToNearestTowardZero, -2},
{-55, ToNearestTowardZero, -5},
{55, AwayFromZero, 6},
{25, AwayFromZero, 3},
{16, AwayFromZero, 2},
{11, AwayFromZero, 2},
{10, AwayFromZero, 1},
{-10, AwayFromZero, -1},
{-11, AwayFromZero, -2},
{-16, AwayFromZero, -2},
{-25, AwayFromZero, -3},
{-55, AwayFromZero, -6},
} {
v := New(test.value, 1)
v.Context.RoundingMode = test.mode
r, ok := v.RoundToInt().Int64()
if !ok {
t.Fatalf("#%d: failed to convert result to int64", i)
}
if test.expected != r {
t.Fatalf("#%d: wanted %d, got %d", i, test.expected, r)
}
}
}
1 change: 1 addition & 0 deletions dectest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var decRoundingModes = map[RoundingMode]decimal.RoundingMode{
Floor: decimal.ToNegativeInf,
HalfEven: decimal.ToNearestEven,
HalfUp: decimal.ToNearestAway,
HalfDown: decimal.ToNearestTowardZero,
}

var decConditions = map[Condition]decimal.Condition{
Expand Down
4 changes: 4 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func roundString(b []byte, mode RoundingMode, pos bool, prec int) []byte {
if b[i+1] >= '5' {
b[i]++
}
case ToNearestTowardZero:
if b[i+1] > '5' {
b[i]++
}
}

if b[i] != '9'+1 {
Expand Down
7 changes: 4 additions & 3 deletions roundingmode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c Context) fix(z *Big) *Big {
}

switch m := c.RoundingMode; m {
case ToNearestAway, ToNearestEven:
case ToNearestAway, ToNearestEven, ToNearestTowardZero:
z.SetInf(z.Signbit())
case AwayFromZero:
// OK
Expand Down

0 comments on commit 6f46dc7

Please sign in to comment.