median

Sorts a non-empty array by numeric value and returns the middle value.

import { Arith } from "@teakit/arith";

Arith.median(["5", "1", "3"]).toString(); // "3"
Arith.median(["1", "2", "3", "4"]).toString(); // "2.5"

// The input array is not mutated.
const values = ["5", "1", "3"];
Arith.median(values).toString(); // "3"
values; // ["5", "1", "3"]

API Reference

Signature

Arith.median(values: readonly ArithValue[]): ArithInstance;

Parameters

ParameterTypeRequiredNotes
valuesreadonly ArithValue[]YesNon-empty array of finite values.

Returns

Returns the middle value for odd-length arrays, or the mean of the two middle values for even-length arrays.

Throws

  • Throws if values is not a non-empty array.
  • Throws if any value is NaN, Infinity, or -Infinity.

Agent Contract

FieldValue
Kindstatic method
Canonical namemedian
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesDECIMAL_PLACES, ROUNDING_MODE, STRICT
Related methodsmean, quantile

Agent Notes

  • Do not mutate the caller's array while sorting.
  • Use median for robust central tendency where outliers may exist.