MCPcopy Create free account
hub / github.com/cockroachdb/apd / Round

Method Round

round.go:62–111  ·  view source on GitHub ↗

Round sets d to rounded x.

(c *Context, d, x *Decimal, disableIfPrecisionZero bool)

Source from the content-addressed store, hash-verified

60
61// Round sets d to rounded x.
62func (r Rounder) Round(c *Context, d, x *Decimal, disableIfPrecisionZero bool) Condition {
63 d.Set(x)
64 nd := x.NumDigits()
65 xs := x.Sign()
66 var res Condition
67
68 if disableIfPrecisionZero && c.Precision == 0 {
69 // Rounding has been disabled.
70 return d.setExponent(c, nd, res, int64(d.Exponent))
71 }
72
73 // adj is the adjusted exponent: exponent + clength - 1
74 if adj := int64(x.Exponent) + nd - 1; xs != 0 && adj < int64(c.MinExponent) {
75 // Subnormal is defined before rounding.
76 res |= Subnormal
77 // setExponent here to prevent double-rounded subnormals.
78 res |= d.setExponent(c, nd, res, int64(d.Exponent))
79 return res
80 }
81
82 diff := nd - int64(c.Precision)
83 if diff > 0 {
84 if diff > MaxExponent {
85 return SystemOverflow | Overflow
86 }
87 if diff < MinExponent {
88 return SystemUnderflow | Underflow
89 }
90 res |= Rounded
91 var y, m BigInt
92 e := tableExp10(diff, &y)
93 y.QuoRem(&d.Coeff, e, &m)
94 if m.Sign() != 0 {
95 res |= Inexact
96 var discard Decimal
97 discard.Coeff.Set(&m)
98 discard.Exponent = int32(-diff)
99 if r.ShouldAddOne(&y, x.Negative, discard.Cmp(decimalHalf)) {
100 roundAddOne(&y, &diff)
101 }
102 }
103 d.Coeff.Set(&y)
104 // The coefficient changed, so recompute num digits in setExponent.
105 nd = unknownNumDigits
106 } else {
107 diff = 0
108 }
109 res |= d.setExponent(c, nd, res, int64(d.Exponent), diff)
110 return res
111}
112
113// roundAddOne adds 1 to abs(b).
114func roundAddOne(b *BigInt, diff *int64) {

Callers

nothing calls this directly

Calls 9

SignMethod · 0.95
QuoRemMethod · 0.95
ShouldAddOneMethod · 0.95
CmpMethod · 0.95
tableExp10Function · 0.85
roundAddOneFunction · 0.85
NumDigitsMethod · 0.80
setExponentMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected