MCPcopy Create free account
hub / github.com/apple/coremltools / _ConvertFloat

Function _ConvertFloat

deps/protobuf/python/google/protobuf/json_format.py:780–813  ·  view source on GitHub ↗

Convert an floating point number.

(value, field)

Source from the content-addressed store, hash-verified

778
779
780def _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.&#x27;)
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}.&#x27;.format(value))
814
815
816def _ConvertBool(value, require_str):

Callers 1

_ConvertScalarFieldValueFunction · 0.85

Calls 5

isinstanceFunction · 0.85
isnanMethod · 0.80
isinfMethod · 0.80
ParseErrorClass · 0.70
formatMethod · 0.45

Tested by

no test coverage detected