Convert an floating point number.
(value, field)
| 778 | |
| 779 | |
| 780 | def _ConvertFloat(value, field): |
| 781 | """Convert an floating point number.""" |
| 782 | if isinstance(value, float): |
| 783 | if math.isnan(value): |
| 784 | raise ParseError('Couldn\'t parse NaN, use quoted "NaN" instead.') |
| 785 | if math.isinf(value): |
| 786 | if value > 0: |
| 787 | raise ParseError('Couldn\'t parse Infinity or value too large, ' |
| 788 | 'use quoted "Infinity" instead.') |
| 789 | else: |
| 790 | raise ParseError('Couldn\'t parse -Infinity or value too small, ' |
| 791 | 'use quoted "-Infinity" instead.') |
| 792 | if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_FLOAT: |
| 793 | # pylint: disable=protected-access |
| 794 | if value > type_checkers._FLOAT_MAX: |
| 795 | raise ParseError('Float value too large') |
| 796 | # pylint: disable=protected-access |
| 797 | if value < type_checkers._FLOAT_MIN: |
| 798 | raise ParseError('Float value too small') |
| 799 | if value == 'nan': |
| 800 | raise ParseError('Couldn\'t parse float "nan", use "NaN" instead.') |
| 801 | try: |
| 802 | # Assume Python compatible syntax. |
| 803 | return float(value) |
| 804 | except ValueError: |
| 805 | # Check alternative spellings. |
| 806 | if value == _NEG_INFINITY: |
| 807 | return float('-inf') |
| 808 | elif value == _INFINITY: |
| 809 | return float('inf') |
| 810 | elif value == _NAN: |
| 811 | return float('nan') |
| 812 | else: |
| 813 | raise ParseError('Couldn\'t parse float: {0}.'.format(value)) |
| 814 | |
| 815 | |
| 816 | def _ConvertBool(value, require_str): |
no test coverage detected