MCPcopy Create free account
hub / github.com/apache/arrow / check_cast_float_to_decimal

Function check_cast_float_to_decimal

python/pyarrow/tests/test_compute.py:2392–2412  ·  view source on GitHub ↗
(float_ty, float_val, decimal_ty, decimal_ctx,
                                max_precision)

Source from the content-addressed store, hash-verified

2390
2391
2392def check_cast_float_to_decimal(float_ty, float_val, decimal_ty, decimal_ctx,
2393 max_precision):
2394 # Use the Python decimal module to build the expected result
2395 # using the right precision
2396 decimal_ctx.prec = decimal_ty.precision
2397 decimal_ctx.rounding = decimal.ROUND_HALF_EVEN
2398 expected = decimal_ctx.create_decimal_from_float(float_val)
2399 # Round `expected` to `scale` digits after the decimal point
2400 expected = expected.quantize(decimal.Decimal(1).scaleb(-decimal_ty.scale))
2401 s = pa.scalar(float_val, type=float_ty)
2402 actual = pc.cast(s, decimal_ty).as_py()
2403 if actual != expected:
2404 # Allow the last digit to vary. The tolerance is higher for
2405 # very high precisions as rounding errors can accumulate in
2406 # the iterative algorithm (GH-35576).
2407 diff_digits = abs(actual - expected) * 10**decimal_ty.scale
2408 limit = 2 if decimal_ty.precision < max_precision - 2 else 4
2409 assert diff_digits <= limit, (
2410 f"float_val = {float_val!r}, precision={decimal_ty.precision}, "
2411 f"expected = {expected!r}, actual = {actual!r}, "
2412 f"diff_digits = {diff_digits!r}")
2413
2414
2415# Cannot test float32 as case generators above assume float64

Callers 1

Calls 4

as_pyMethod · 0.80
DecimalMethod · 0.45
scalarMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected