| 129 | } |
| 130 | |
| 131 | func (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. |
| 174 | func (c *Context) Add(d, x, y *Decimal) (Condition, error) { |