Skip to content

Carer's Allowance earnings-limit property test counts pence in float64, not the model's float32 #2532

Closed

Description

@MaxGhenis

policyengine_uk/tests/test_carers_allowance_earnings_limit_properties.py::test_bounds_and_differential can fail at random. Its reg. 5 penny-rounding check counts pence in a different precision from the formula it tests.

What goes wrong

_person() turns each result into a Python float (float64) with float(np.asarray(...)[0]). The test then recomputes the reg. 5 rounding, np.floor(earnings / WEEKS * 100 + 1e-6) / 100 > LIMIT, in float64. The model stores carers_allowance_earnings as float32, and is_gainfully_employed_for_carers_allowance floors the weekly pence in float32. Where the formula's float32 arithmetic rounds a value across a penny boundary, the two disagree.

Case

Pay of £10,608.52 in 2026, with no profit and no pension contributions (measured on main at c6715d4):

value
stored carers_allowance_earnings (float32) 10,608.52, which is 10,608.51953125 as float64
formula's weekly pence (float32) 20,401, so over the £204 limit: gainfully employed
test's weekly pence (float64) 20,400, so the test expects not gainfully employed

The assertion bool(result["is_gainfully_employed_for_carers_allowance"]) == over then fails. The test isn't derandomized, so a draw in that narrow band would turn CI red with no code change.

The test should mirror the formula's float32 arithmetic. The float64 recompute applied reg. 5 exactly to the stored value, which is £204.00 a week (20,400.9991 pence floored); the formula's float32 rounding gives £204.01, which matches the £10,608.52 entered. Whether the formula should count pence in float32 at all is #2541. #2518 found and fixed the same pattern in test_carers_allowance_pension_contribution_deduction_rate_properties.py (calculate(), over_limit() and the PENNY_OVER example).

Fix

Test only: no model source changes.

  • Count the pence on the stored float32 value, as the formula does. Convert with float() only for the bounds and tolerance checks.
  • Add an explicit Hypothesis @example at pay 10,608.52, profit 0, pension 0.
  • Derandomize the property tests in this file, so CI draws the same examples every run.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions