Quantize adjusts and rounds x as necessary so it is represented with exponent exp and stores the result in d.
(d, x *Decimal, exp int32)
| 1147 | // Quantize adjusts and rounds x as necessary so it is represented with |
| 1148 | // exponent exp and stores the result in d. |
| 1149 | func (c *Context) Quantize(d, x *Decimal, exp int32) (Condition, error) { |
| 1150 | if c.shouldSetAsNaN(x, nil) { |
| 1151 | return c.setAsNaN(d, x, nil) |
| 1152 | } |
| 1153 | if x.Form == Infinite || exp < c.etiny() { |
| 1154 | d.Set(decimalNaN) |
| 1155 | return c.goError(InvalidOperation) |
| 1156 | } |
| 1157 | res := c.quantize(d, x, exp) |
| 1158 | if nd := d.NumDigits(); nd > int64(c.Precision) || exp > c.MaxExponent { |
| 1159 | res = InvalidOperation |
| 1160 | d.Set(decimalNaN) |
| 1161 | } else { |
| 1162 | res |= c.round(d, d) |
| 1163 | if res.Overflow() || res.Underflow() { |
| 1164 | res = InvalidOperation |
| 1165 | d.Set(decimalNaN) |
| 1166 | } |
| 1167 | } |
| 1168 | return c.goError(res) |
| 1169 | } |
| 1170 | |
| 1171 | func (c *Context) quantize(d, v *Decimal, exp int32) Condition { |
| 1172 | diff := exp - v.Exponent |