@arki/math agent guide
For @arki/[email protected] · source docs/agent-guide.md
Read llms.txt first for the map. When this guide and AGENTS.md disagree, AGENTS.md wins.
The model
Decimal(coefficient × 10^-scale) andRational(abigintfraction) are exact. They are immutable and never become floats by accident.- The plain-number modules (
number,stats,geometry,roots,linear-algebra, and others) are float64 with an honest error estimate. - Rounding is always explicit. The modes are
up,down,ceiling,floor,halfUp,halfDown,halfEvenandunnecessary. - Failure is a typed error. Nothing returns
NaNornullto signal it. - Every module is its own subpath, and each has a page in
docs/modules/.
Task map
Add exact amounts
- Import
decimalfrom@arki/math/decimal. - Build values from strings:
decimal('19.99'). - Use
plus,minus,times. They never round. - Round once, at the end, with
toScale(2, 'halfEven').
import { decimal } from '@arki/math/decimal';
const total = decimal('19.99').times(3).plus('0.30');
total.toString(); // '60.27'
total.times('0.19').toScale(2, 'halfEven').toString(); // '11.45'Divide without losing digits
- Use
ratio(a, b)from@arki/math/rationalwhen the answer is a fraction. - Use
decimal(1).dividedBy(3, { scale: 4, rounding: 'halfEven' })when you need a decimal.
Format and parse numbers
- Show with
formatNumber,formatCurrencyorformatPercentfrom@arki/math/format. - Read locale text with
parseNumberorparseDecimalfrom@arki/math/parse.
Statistics, distributions and roots
- Use
statsformean,median,stddev, regression.meantostddevare exact onDecimal[]. - Use
distributionsfornormal,studentT,erf,gamma. - Use
roots.findRoot(f, [a, b])with a bracket that changes sign.
Money
Use @arki/money. It builds on this package.
Rules that bite
decimal(0.1 + 0.2)is already wrong. Build from strings.decimal(1).dividedBy(3)throwsRoundingNecessaryError. Pass a scale and a mode.d.toScale(1)throws when it would drop a non-zero digit. Pass a mode.- A
Decimalover 10 000 digits throwsResourceLimitError. - Hermes on iOS has no
formatToParts: locale parsing throwsUnsupportedFeatureError. Passsymbols. linear-algebrais not re-exported from the root.
Errors
| Code | Error | What to do |
|---|---|---|
ARKI_MATH_E001 | NumberFormatError | Pass plain ASCII decimal text |
ARKI_MATH_E002 | InvalidArgumentError | Fix the argument named in the message |
ARKI_MATH_E003 | DivisionByZeroError | Check the divisor |
ARKI_MATH_E004 | RoundingNecessaryError | Pass a scale and a rounding mode |
ARKI_MATH_E005 | NumericRangeError | Use bigint or Decimal, or stay in the finite range |
ARKI_MATH_E006 | EmptyDataError | Check the data length first |
ARKI_MATH_E007 | ResourceLimitError | Stay within 10 000 digits |
ARKI_MATH_E008 | UnsupportedFeatureError | Pass symbols, or use another runtime |
ARKI_MATH_E009 | SingularMatrixError | Use leastSquares or pseudoInverse |
ARKI_MATH_E010 | ConvergenceError | Read .best; widen the tolerance or the bracket |
ARKI_MATH_E011 | NotPositiveDefiniteError | Use LU or QR instead of Cholesky |
Verify
bun run test:docs
bun run typecheck
bun run test