| 438 | } |
| 439 | |
| 440 | func (c *Context) rootSpecials(d, x *Decimal, factor int32) (bool, Condition, error) { |
| 441 | if c.shouldSetAsNaN(x, nil) { |
| 442 | res, err := c.setAsNaN(d, x, nil) |
| 443 | return true, res, err |
| 444 | } |
| 445 | if x.Form == Infinite { |
| 446 | if x.Negative { |
| 447 | d.Set(decimalNaN) |
| 448 | res, err := c.goError(InvalidOperation) |
| 449 | return true, res, err |
| 450 | } |
| 451 | d.Set(decimalInfinity) |
| 452 | return true, 0, nil |
| 453 | } |
| 454 | |
| 455 | switch x.Sign() { |
| 456 | case -1: |
| 457 | if factor%2 == 0 { |
| 458 | d.Set(decimalNaN) |
| 459 | res, err := c.goError(InvalidOperation) |
| 460 | return true, res, err |
| 461 | } |
| 462 | case 0: |
| 463 | d.Set(x) |
| 464 | d.Exponent /= factor |
| 465 | return true, 0, nil |
| 466 | } |
| 467 | return false, 0, nil |
| 468 | } |
| 469 | |
| 470 | // Sqrt sets d to the square root of x. Sqrt uses the Babylonian method |
| 471 | // for computing the square root, which uses O(log p) steps for p digits |