MCPcopy Create free account
hub / github.com/apache/fory / _float_value

Function _float_value

python/pyfory/converter.py:234–257  ·  view source on GitHub ↗
(value, local_type_id: int)

Source from the content-addressed store, hash-verified

232
233
234def _float_value(value, local_type_id: int) -> float:
235 if isinstance(value, decimal.Decimal):
236 value = _canonical_decimal(value)
237 if value.is_zero():
238 return 0.0
239 result = _to_float_domain(float(value), local_type_id)
240 if _decimal_from_float_value(result) != value:
241 raise ValueError("decimal value is not exactly representable by target float")
242 return result
243 if type(value) is int:
244 result = _to_float_domain(float(value), local_type_id)
245 if not math.isfinite(result) or int(result) != value:
246 raise ValueError("integer value is not exactly representable by target float")
247 return result
248 if type(value) is float:
249 if math.isnan(value):
250 raise ValueError("NaN is not convertible across floating types")
251 result = _to_float_domain(value, local_type_id)
252 if not _same_float_value(_to_float_domain(result, local_type_id), result):
253 raise ValueError("target float round-trip failed")
254 if not _same_float_value(result, value):
255 raise ValueError("floating value is not exactly representable by target float")
256 return result
257 raise ValueError(f"expected numeric value, got {type(value)!r}")
258
259
260def _bool_value(value) -> bool:

Callers 1

_numeric_valueFunction · 0.85

Calls 5

_canonical_decimalFunction · 0.85
_to_float_domainFunction · 0.85
_same_float_valueFunction · 0.85
is_zeroMethod · 0.45

Tested by

no test coverage detected