-
Notifications
You must be signed in to change notification settings - Fork 61
/
rounding.go
50 lines (45 loc) · 961 Bytes
/
rounding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package decimal
import (
"math"
"math/big"
"github.com/ericlagergren/decimal/internal/arith"
)
func (r RoundingMode) needsInc(c int, pos, odd bool) bool {
switch r {
case Unneeded:
panic("decimal: rounding is necessary")
case AwayFromZero:
return true
case ToZero:
return false
case ToPositiveInf:
return pos
case ToNegativeInf:
return !pos
case ToNearestEven, ToNearestAway:
if c < 0 {
return false
}
if c > 0 {
return true
}
if r == ToNearestEven {
return odd
}
return true
default:
panic("decimal: unknown RoundingMode")
}
}
func (z *Big) needsInc(x, r int64, pos, odd bool) bool {
m := 1
if r > math.MinInt64/2 || r <= math.MaxInt64/2 {
m = arith.AbsCmp(r<<1, x)
}
return z.Context.RoundingMode.needsInc(m, pos, odd)
}
func (z *Big) needsIncBig(x, r *big.Int, pos, odd bool) bool {
var x0 big.Int
m := arith.BigAbsCmp(*x0.Mul(r, twoInt), *x)
return z.Context.RoundingMode.needsInc(m, pos, odd)
}