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

Function test_cast_float_to_decimal_random

python/pyarrow/tests/test_compute.py:2438–2497  ·  view source on GitHub ↗

Test float-to-decimal conversion against exactly generated values.

(float_ty, decimal_traits)

Source from the content-addressed store, hash-verified

2436@pytest.mark.parametrize('decimal_traits', decimal_type_traits,
2437 ids=lambda v: v.name)
2438def test_cast_float_to_decimal_random(float_ty, decimal_traits):
2439 """
2440 Test float-to-decimal conversion against exactly generated values.
2441 """
2442 r = random.Random(43)
2443 np_float_ty = {
2444 pa.float32(): np.float32,
2445 pa.float64(): np.float64,
2446 }[float_ty]
2447 mantissa_bits = {
2448 pa.float32(): 24,
2449 pa.float64(): 53,
2450 }[float_ty]
2451 float_exp_min, float_exp_max = {
2452 pa.float32(): (-126, 127),
2453 pa.float64(): (-1022, 1023),
2454 }[float_ty]
2455 mantissa_digits = math.floor(math.log10(2**mantissa_bits))
2456 max_precision = decimal_traits.max_precision
2457
2458 # For example, decimal32 <-> float64
2459 if max_precision < mantissa_digits:
2460 mantissa_bits = math.floor(math.log2(10**max_precision))
2461 mantissa_digits = math.floor(math.log10(2**mantissa_bits))
2462
2463 with decimal.localcontext() as ctx:
2464 precision = mantissa_digits
2465 ctx.prec = precision
2466 # The scale must be chosen so as
2467 # 1) it's within bounds for the decimal type
2468 # 2) the floating point exponent is within bounds
2469 min_scale = max(-max_precision,
2470 precision + math.ceil(math.log10(2**float_exp_min)))
2471 max_scale = min(max_precision,
2472 math.floor(math.log10(2**float_exp_max)))
2473 for scale in range(min_scale, max_scale):
2474 decimal_ty = decimal_traits.factory(precision, scale)
2475 # We want to random-generate a float from its mantissa bits
2476 # and exponent, and compute the expected value in the
2477 # decimal domain. The float exponent has to ensure the
2478 # expected value doesn't overflow and doesn't lose precision.
2479 float_exp = (-mantissa_bits +
2480 math.floor(math.log2(10**(precision - scale))))
2481 assert float_exp_min <= float_exp <= float_exp_max
2482 for i in range(5):
2483 mantissa = r.randrange(0, 2**mantissa_bits)
2484 float_val = np.ldexp(np_float_ty(mantissa), float_exp)
2485 assert isinstance(float_val, np_float_ty)
2486 # Make sure we compute the exact expected value and
2487 # round by half-to-even when converting to the expected precision.
2488 if float_exp >= 0:
2489 expected = decimal.Decimal(mantissa) * 2**float_exp
2490 else:
2491 expected = decimal.Decimal(mantissa) / 2**-float_exp
2492 expected_as_int = round(expected.scaleb(scale))
2493 actual = pc.cast(
2494 pa.scalar(float_val, type=float_ty), decimal_ty).as_py()
2495 actual_as_int = round(actual.scaleb(scale))

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected