# Arithmetic and comparisons
source: https://docs.chalk.ai/docs/chalkdf/expressions/arithmetic

## Calculate transaction totals, compare columns, and handle zero denominators.

Build a line total from unit price and quantity, then compare it with the authorized amount. A literal such as 100 applies to each row. Two column expressions read values from the same row.

### Calculate a line total

Multiply two columns before converting cents to dollars. Name the intermediate value so later expressions can use it.

```python
from chalk import _, functions as F
from chalkdf import DataFrame

tx = DataFrame({
    "id": [1, 2, 3], "unit_cents": [125, 300, 75],
    "units": [4, 2, 0], "authorized_cents": [500, 550, 0],
})

priced = tx.with_columns((_.unit_cents * _.units).alias("total_cents"))
priced.project({"id": _.id, "cents": _.total_cents, "dollars": _.total_cents / 100}).run()
```

| id · `int64` | cents · `int64` | dollars · `double` |
| ------------ | --------------- | ------------------ |
| 1            | 500             | 5.0                |
| 2            | 600             | 6.0                |
| 3            | 0               | 0.0                |

Keeping cents in the intermediate result lets you compare two amounts in the same unit.

### Compare two columns

Calculate the signed difference and retain a boolean that identifies overcharges.

```python
review = priced.project({
    "id": _.id,
    "difference": _.total_cents - _.authorized_cents,
    "over_limit": _.total_cents > _.authorized_cents,
})
review.run()
```

| id · `int64` | difference · `int64` | over_limit · `bool` |
| ------------ | -------------------- | ------------------- |
| 1            | 0                    | False               |
| 2            | 50                   | True                |
| 3            | 0                    | False               |

Equality at the authorization limit produces false. To keep only rows that need review, use [`review.filter(_.over_limit)`](/dataframe-docs#DataFrame.filter).

### Handle a zero denominator

Calculate the fraction of the authorization used. Use safe_divide to handle a zero authorization.

```python
priced.project({
    "id": _.id,
    "fraction_used": F.safe_divide(_.total_cents.cast(float), _.authorized_cents),
}).run()
```

| id · `int64` | fraction_used · `double` |
| ------------ | ------------------------ |
| 1            | 1.0                      |
| 2            | 1.0909090909090908       |
| 3            | null                     |

Here [`safe_divide`](/api-docs#safe_divide) returns null for the zero authorization. Keep the denominator in your output when downstream users need to distinguish it from missing inputs.

### Use a comparison to choose a value

Translate the over-limit flag into a review amount. Retain the row for every transaction so the result can feed a later group summary.

```python
review.project({
    "id": _.id,
    "to_review": F.if_then_else(_.over_limit, _.difference, 0),
}).run()
```

| id · `int64` | to_review · `int64` |
| ------------ | ------------------- |
| 1            | 0                   |
| 2            | 50                  |
| 3            | 0                   |

Only the excess 50 cents needs review on transaction 2. Keeping the other rows with a zero lets a downstream count include approved transactions.

### Test a flag in a bit mask

An integer flag field can encode several checks on the same transaction. Mask the review bit before converting the result to a boolean.

```python
flags = DataFrame({"flags": [0, 2, 3, None]})
flags.project({
    "flags": _.flags,
    "review": F.bitwise_and(_.flags, 2) != 0,
}).run()
```

| flags · `int64` | review · `bool` |
| --------------- | --------------- |
| 0               | False           |
| 2               | True            |
| 3               | True            |
| null            | True            |

Both 2 and 3 have the review bit set. A missing flag stays unknown.

See [casting](/docs/chalkdf/expressions/casting) and [filtering](/docs/chalkdf/expressions/filtering).





