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

Method add

context.go:131–171  ·  view source on GitHub ↗
(d, x, y *Decimal, subtract bool)

Source from the content-addressed store, hash-verified

129}
130
131func (c *Context) add(d, x, y *Decimal, subtract bool) (Condition, error) {
132 if c.shouldSetAsNaN(x, y) {
133 return c.setAsNaN(d, x, y)
134 }
135 xn := x.Negative
136 yn := y.Negative != subtract
137 if xi, yi := x.Form == Infinite, y.Form == Infinite; xi || yi {
138 if xi && yi && xn != yn {
139 d.Set(decimalNaN)
140 return c.goError(InvalidOperation)
141 } else if xi {
142 d.Set(x)
143 } else {
144 d.Set(decimalInfinity)
145 d.Negative = yn
146 }
147 return 0, nil
148 }
149 var tmp BigInt
150 a, b, s, err := upscale(x, y, &tmp)
151 if err != nil {
152 return 0, fmt.Errorf("add: %w", err)
153 }
154 d.Negative = xn
155 if xn == yn {
156 d.Coeff.Add(a, b)
157 } else {
158 d.Coeff.Sub(a, b)
159 switch d.Coeff.Sign() {
160 case -1:
161 d.Negative = !d.Negative
162 d.Coeff.Neg(&d.Coeff)
163 case 0:
164 d.Negative = c.Rounding == RoundFloor
165 }
166 }
167 d.Exponent = s
168 d.Form = Finite
169 res := c.round(d, d)
170 return c.goError(res)
171}
172
173// Add sets d to the sum x+y.
174func (c *Context) Add(d, x, y *Decimal) (Condition, error) {

Callers 2

AddMethod · 0.95
SubMethod · 0.95

Calls 10

shouldSetAsNaNMethod · 0.95
setAsNaNMethod · 0.95
goErrorMethod · 0.95
roundMethod · 0.95
upscaleFunction · 0.85
SetMethod · 0.45
AddMethod · 0.45
SubMethod · 0.45
SignMethod · 0.45
NegMethod · 0.45

Tested by

no test coverage detected