Convert value to given numeric type T.
(value, T)
| 331 | |
| 332 | |
| 333 | def _convert(value, T): |
| 334 | """Convert value to given numeric type T.""" |
| 335 | if type(value) is T: |
| 336 | # This covers the cases where T is Fraction, or where value is |
| 337 | # a NAN or INF (Decimal or float). |
| 338 | return value |
| 339 | if issubclass(T, int) and value.denominator != 1: |
| 340 | T = float |
| 341 | try: |
| 342 | # FIXME: what do we do if this overflows? |
| 343 | return T(value) |
| 344 | except TypeError: |
| 345 | if issubclass(T, Decimal): |
| 346 | return T(value.numerator) / T(value.denominator) |
| 347 | else: |
| 348 | raise |
| 349 | |
| 350 | |
| 351 | def _fail_neg(values, errmsg='negative value'): |