Convert value to given numeric type T.
(value, T)
| 1621 | |
| 1622 | |
| 1623 | def _convert(value, T): |
| 1624 | """Convert value to given numeric type T.""" |
| 1625 | if type(value) is T: |
| 1626 | # This covers the cases where T is Fraction, or where value is |
| 1627 | # a NAN or INF (Decimal or float). |
| 1628 | return value |
| 1629 | |
| 1630 | if issubclass(T, int) and value.denominator != 1: |
| 1631 | T = float |
| 1632 | |
| 1633 | try: |
| 1634 | # FIXME: what do we do if this overflows? |
| 1635 | return T(value) |
| 1636 | except TypeError: |
| 1637 | if issubclass(T, Decimal): |
| 1638 | return T(value.numerator) / T(value.denominator) |
| 1639 | else: |
| 1640 | raise |
| 1641 | |
| 1642 | |
| 1643 | def _fail_neg(values, errmsg='negative value'): |
no test coverage detected
searching dependent graphs…