Docs / Arithmetic and number theory

Arithmetic and number theory

These endpoints exist because the usual way of computing gets small things wrong. Binary floating point cannot represent 0.1, so 0.1 + 0.2 becomes 0.30000000000000004, and a sum of prices drifts by a cent. Rounding is worse: languages disagree on what happens at exactly half, and a VAT or loan calculation that rounds at the wrong step, or with the wrong rule, produces totals that do not match an invoice.

Every calculation in this group uses decimal or exact rational arithmetic. Expressions are evaluated with a stated precision, fractions stay fractions until you ask for a decimal expansion, and repeating decimals are shown as such. Rounding never happens implicitly; where an answer must be rounded, the rule is a parameter, with half-even and half-up among the choices, and the answer names the rule it used.

Number theory endpoints say what their answers rest on. A primality answer names its method, and a prime too large for the deterministic test is reported as a probable prime with its error bound, not as a proof. Factorisation of large numbers can take real time, so inputs are sized before any work starts: a request whose result would be too big to compute or to send is refused with the estimate, instead of running until a timeout.

Loans and compound interest follow the formulas lenders use, and the schedule shows how each payment splits into interest and principal after rounding, so the last payment absorbs the difference openly.

Every endpoint in this group

/calc?expr=&[precision=]

Evaluate an arithmetic expression with arbitrary precision (never float)

expr: expression: + - * / // % ^ ( ) and functions sqrt ln log10 log(x,b) exp sin cos tan abs floor ceil round min max factorial gcd lcm; constants pi e tau; precision (optional): significant digits, 1-100000 (the digits of the result are what the cap applies to)

curl "https://agent-helper.org/calc?expr=0.1+0.2"

/calc/exact?expr=

Evaluate with exact rational arithmetic; result as a fraction and repeating decimal

expr: expression with + - * / // % ^ (integer powers) ( ) abs floor ceil min max factorial gcd lcm

curl "https://agent-helper.org/calc/exact?expr=1/3+1/6"

/calc/round?value=&[places=]&[mode=]

Round a decimal number with an explicit rule

value: decimal number; places (optional): decimal places (negative rounds to tens, hundreds...); mode (optional): rounding rule

curl "https://agent-helper.org/calc/round?value=2.675&places=2&mode=half-up"

/calc/base?value=&[from=]&[to=]

Convert an integer between bases 2-36

value: integer written in base from; 0x/0o/0b prefixes accepted; from (optional): source base 2-36; to (optional): target base 2-36

curl "https://agent-helper.org/calc/base?value=ff&from=16&to=2"

/calc/bitwise?op=&a=&[b=]&[bits=]

Bitwise and, or, xor, not, shifts on integers of a given width

op: and, or, xor, not, shl, shr; a: integer (decimal, or 0x/0b prefixed); b (optional): second integer, or shift amount; bits (optional): word width 1-512; required for not and shl so the result is well defined

curl "https://agent-helper.org/calc/bitwise?op=and&a=0b1100&b=0b1010"

/calc/percent?percent=&of=

A percentage of a value

percent: percentage, e.g. 15; of: the base value

curl "https://agent-helper.org/calc/percent?percent=15&of=200"

/calc/percent-change?from=&to=&[precision=]

Percentage change from one value to another

from: old value; to: new value; precision (optional): significant digits

curl "https://agent-helper.org/calc/percent-change?from=80&to=100"

/calc/vat?amount=&rate=&mode=&[places=]&[rounding=]

Add VAT/sales tax to a net amount, or extract it from a gross amount

amount: amount; rate: tax rate in percent; mode: add (amount is net) or extract (amount is gross); places (optional): decimal places of the rounded result; rounding (optional): rounding rule

curl "https://agent-helper.org/calc/vat?amount=100&rate=20&mode=add"

/calc/compound?principal=&rate=&years=&[per_year=]&[places=]&[rounding=]

Compound interest: future value

principal: starting amount; rate: nominal annual rate in percent; years: duration in years; per_year (optional): compounding periods per year, or 'continuous'; places (optional): decimal places of the rounded result; rounding (optional): rounding rule

curl "https://agent-helper.org/calc/compound?principal=1000&rate=5&years=10"

/calc/loan?principal=&rate=&months=&[schedule=]&[places=]&[rounding=]

Annuity loan: monthly payment and optional amortisation schedule

principal: loan amount; rate: nominal annual interest rate in percent; months: number of monthly payments, 1-1200; schedule (optional): true to list every month; places (optional): decimal places of the rounded result; rounding (optional): rounding rule

curl "https://agent-helper.org/calc/loan?principal=250000&rate=4.5&months=360"

/calc/prime/{n}

Whether an integer is prime (deterministic below 3.3e24)

n: integer up to 5000 digits

curl "https://agent-helper.org/calc/prime/2147483647"

/calc/factor/{n}

Prime factorisation (trial division + Pollard-Brent rho)

n: positive integer up to 60 digits

curl "https://agent-helper.org/calc/factor/600851475143"

/calc/next-prime/{n}

Smallest prime greater than n

n: integer

curl "https://agent-helper.org/calc/next-prime/1000000"

/calc/prev-prime/{n}

Largest prime smaller than n

n: integer greater than 2

curl "https://agent-helper.org/calc/prev-prime/1000000"

/calc/gcd?values=

Greatest common divisor of integers

values: comma-separated integers

curl "https://agent-helper.org/calc/gcd?values=12,18,24"

/calc/lcm?values=

Least common multiple of integers

values: comma-separated integers

curl "https://agent-helper.org/calc/lcm?values=4,6,10"

/calc/modpow?base=&exp=&mod=

Modular exponentiation base^exp mod m

base: integer; exp: integer (negative uses the modular inverse); mod: positive modulus

curl "https://agent-helper.org/calc/modpow?base=4&exp=13&mod=497"

/calc/modinv?a=&mod=

Modular multiplicative inverse

a: integer; mod: positive modulus

curl "https://agent-helper.org/calc/modinv?a=3&mod=11"

/calc/stats?values=&[stat=]&[precision=]

Descriptive statistics of a list of numbers

values: comma-separated numbers; stat (optional): return only one statistic; precision (optional): significant digits for inexact results

curl "https://agent-helper.org/calc/stats?values=2,4,4,4,5,5,7,9"

/calc/regression?x=&y=

Least-squares linear regression y = a*x + b

x: comma-separated numbers; y: comma-separated numbers, same count as x

curl "https://agent-helper.org/calc/regression?x=1,2,3,4,5&y=2.1,3.9,6.2,7.8,10.1"