PC04-L02 · guided challenge

Safe ratio

Prevent division by zero and return the same type from both branches.

Practical CEL4 test casesDocumented
What will I practise?

You can place a risky operation only in the branch where it can run safely.

How is it checked?

Your one expression must produce the expected result for all four JSON inputs.

When am I done?

When all tests pass and you can explain every condition in your expression.

Word problem

Calculate numerator divided by denominator. When the denominator is zero, return the double value 0.0 instead of evaluating an invalid division.

Exact requirements

  1. Do not evaluate division when the denominator is zero.
  2. Normal and negative denominators still use division.
  3. Both branches return double.

Required result

Write one CEL expression. It must return the expected value and type for all four prepared JSON inputs. Do not change the inputs to make an incomplete expression pass.

Recommended procedure

  1. Translate each numbered requirement into one small boolean condition.
  2. Run the starter expression and inspect which cases fail.
  3. Add one condition at a time and run the complete suite again.
  4. Mark the challenge complete only when you can explain every operator and every boundary.

Solve the challenge · browser test suite

Write one expression that passes every case

Edit the CEL expression, then run it against all prepared JSON inputs. A solution is complete only when every case passes.

4 test cases

Hint: Test the denominator first. Division belongs only in the true branch.

Tip: press Ctrl/⌘ + Enter to run all tests.
Normal ratioNot run yet
Expected: 2.5 · double
Show JSON input
{
  "metrics": {
    "numerator": 10,
    "denominator": 4
  }
}
Zero denominatorNot run yet
Expected: 0.0 · double
Show JSON input
{
  "metrics": {
    "numerator": 10,
    "denominator": 0
  }
}
Negative denominatorNot run yet
Expected: -3.0 · double
Show JSON input
{
  "metrics": {
    "numerator": 9,
    "denominator": -3
  }
}
Zero numeratorNot run yet
Expected: 0.0 · double
Show JSON input
{
  "metrics": {
    "numerator": 0,
    "denominator": 5
  }
}
Test result

Not run yet

The same expression runs against every case. Test data is fixed so that boundary and failure scenarios cannot be skipped.

Explained solution

Show the complete expression
metrics.denominator != 0.0 ? metrics.numerator / metrics.denominator : 0.0

Why the solution works

  1. The conditional tests the denominator before the risky operation.
  2. Only the true branch contains division.
  3. CEL evaluates only the selected branch.
  4. The false branch uses 0.0 so its type matches the calculated double result.

What the test cases prove

  • Normal ratio: expected 2.5 (double).
  • Zero denominator: expected 0.0 (double).
  • Negative denominator: expected -3.0 (double).
  • Zero numerator: expected 0.0 (double).

The cases are intentionally different. A solution that passes only the first case has not yet implemented the complete rule.

Common mistake

Do not optimise the expression for the first successful input. A requirement is implemented only when its positive case, negative case, and boundary case all produce the stated result.

Knowledge check

Can you point to the exact part of the solution that makes each false test case fail? If two different failures depend on the same condition, explain why.

Key takeaway

You can place a risky operation only in the branch where it can run safely.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition for operators, types, macros, presence, strings, and time values used in this challenge.