> For the complete documentation index, see [llms.txt](https://bubblegum-reality.gitbook.io/bubblegum-reality-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bubblegum-reality.gitbook.io/bubblegum-reality-docs/reference/math.md).

# Math Utilities

`src/lib/money.ts` — the only place token arithmetic happens. `decimal.js` is configured to 40-digit precision with `ROUND_DOWN`.

## Conversions

```ts
fromBaseUnits(value: bigint | string, decimals: number): Decimal
toBaseUnits(value: Decimal.Value, decimals: number): bigint
```

* `decimals` must be an integer in `[0, 36]` — otherwise it throws.
* `toBaseUnits` truncates excess precision downward and throws on negatives.

## Reference pricing

```ts
adjustedReference(rawPrice, multiplier): Decimal   // throws if multiplier ≤ 0
spreadBps(bid, ask): number | null                // null if either side ≤ 0
```

## Execution cost

```ts
executionCostBps({
  side: "buy" | "sell",
  usdgAmount: Decimal.Value,   // human USDG moved
  tokenAmount: Decimal.Value,  // human stock-token moved
  referencePrice: Decimal.Value,// multiplier-adjusted price of one token
}): number | null
```

Positive is always worse than reference, on both sides. `null` when any input is non-positive.

## Formatting

```ts
formatUsd(value, dp = 2)     // "$214.87"  | "—"
formatAmount(value, dp = 6)  // "12.345678"| "—"
formatBps(value)             // "+48.3 bps"| "—"
```

All three render `—` for `null` / `undefined`. `formatBps` prefixes `+` for positive values so direction is unambiguous.

## Rules

1. **Never `Number(amount)` on a token value.** Parse to `Decimal` or `bigint`.
2. **Never round up** a user-supplied amount.
3. **Return `null`, not `0`**, when a metric is undefined — `0 bps` is a claim, `—` is not.
4. **Throw on corrupt inputs** (non-positive multiplier, bad decimals) instead of degrading into a wrong number.

Covered by `src/lib/__tests__/money.test.ts`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://bubblegum-reality.gitbook.io/bubblegum-reality-docs/reference/math.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
