CH02-L01 · foundation

Numbers and arithmetic

Calculate a final price and distinguish integer division from floating-point division.

General CELRuns hereDocumented
What will I learn?

You understand why CEL does not mix numeric types automatically and when to convert explicitly.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

CEL provides int, uint, and double numeric types, and arithmetic requires matching types. JSON has only one general number syntax, so this playground maps JSON numbers to CEL double. The self-contained example therefore writes 3.0, not 3, beside 12.5. Integer division truncates toward zero, while double division keeps the fractional part.

Read the prepared example

The CEL + JSON rule is:

price * quantity - discount

Its input is:

{
  "price": 12.5,
  "quantity": 3,
  "discount": 3
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. 12.5 * 3.0 produces 37.5.
  2. 37.5 - 3.0 produces 34.5.
  3. The final type is double.

Your task

  1. Run both modes and confirm 34.5.
  2. Try 7 / 2 in CEL only.
  3. Then try 7.0 / 2.0 and compare the results.

Expected observation: 7 / 2 returns 3; 7.0 / 2.0 returns 3.5.

Common mistake

Writing 12.5 * 3 mixes double and int. CEL requires an explicit choice instead of silently widening one operand.

Show the explanation

It makes the quantity a double, matching the price and discount.

Knowledge check

Why is the quantity written as 3.0 in the self-contained example?

Key takeaway

Choose a numeric type deliberately, keep arithmetic operands compatible, and convert only where the data boundary requires it.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.