Rem sets d to the remainder part of the quotient x/y. If the integer part cannot fit in d.Precision digits, an error is returned.
(d, x, y *Decimal)
| 395 | // Rem sets d to the remainder part of the quotient x/y. If |
| 396 | // the integer part cannot fit in d.Precision digits, an error is returned. |
| 397 | func (c *Context) Rem(d, x, y *Decimal) (Condition, error) { |
| 398 | if c.shouldSetAsNaN(x, y) { |
| 399 | return c.setAsNaN(d, x, y) |
| 400 | } |
| 401 | |
| 402 | if x.Form != Finite { |
| 403 | d.Set(decimalNaN) |
| 404 | return c.goError(InvalidOperation) |
| 405 | } |
| 406 | if y.Form == Infinite { |
| 407 | d.Set(x) |
| 408 | return 0, nil |
| 409 | } |
| 410 | |
| 411 | var res Condition |
| 412 | if y.IsZero() { |
| 413 | if x.IsZero() { |
| 414 | res |= DivisionUndefined |
| 415 | } else { |
| 416 | res |= InvalidOperation |
| 417 | } |
| 418 | d.Set(decimalNaN) |
| 419 | return c.goError(res) |
| 420 | } |
| 421 | var tmp1 BigInt |
| 422 | a, b, s, err := upscale(x, y, &tmp1) |
| 423 | if err != nil { |
| 424 | return 0, fmt.Errorf("Rem: %w", err) |
| 425 | } |
| 426 | var tmp2 BigInt |
| 427 | tmp2.QuoRem(a, b, &d.Coeff) |
| 428 | if NumDigits(&tmp2) > int64(c.Precision) { |
| 429 | d.Set(decimalNaN) |
| 430 | return c.goError(DivisionImpossible) |
| 431 | } |
| 432 | d.Form = Finite |
| 433 | d.Exponent = s |
| 434 | // The sign of the result is sign if the dividend. |
| 435 | d.Negative = x.Negative |
| 436 | res |= c.round(d, d) |
| 437 | return c.goError(res) |
| 438 | } |
| 439 | |
| 440 | func (c *Context) rootSpecials(d, x *Decimal, factor int32) (bool, Condition, error) { |
| 441 | if c.shouldSetAsNaN(x, nil) { |