cmp

Compares the receiver with another value and returns numeric ordering.

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

const x = Arith.from("Infinity");
const y = Arith.from("5");

x.cmp(y); // 1
x.cmp(x.sub("1")); // 0
y.cmp("NaN"); // null

// Use the (string, base) overload when the comparison value is written in another base.
y.cmp("110", 2); // -1
Arith.from("10").cmp("a", 16); // 0

API Reference

Signature

cmp(n: ArithValue): 1 | -1 | 0 | null;
cmp(n: string, base: number): 1 | -1 | 0 | null;

Parameters

ParameterTypeRequiredNotes
nArithValueYesValue to convert to Arith before comparison.
basenumberNoOnly valid with the (string, base) overload. Must be an integer from 2 through ALPHABET.length.

Returns

Returns 1 when the receiver is greater, -1 when it is less, 0 when values are equal, or null when either side is NaN.

Throws

  • Throws if base is invalid.
  • Throws if the comparison value is invalid while STRICT is true.

Agent Contract

FieldValue
Kindinstance method
Canonical namecmp
AliasesNone
Mutates receiverNo
Returns`1
Accepts (string, base) overloadYes
Configuration dependenciesSTRICT
Related methodseq, ne, gt, ge, lt, le

Agent Notes

  • Use cmp instead of JavaScript comparison operators when a tri-state order is required.
  • Use eq, ne, gt, ge, lt, or le for boolean checks.
  • Use Arith.from(...) to create values. Do not generate new Arith(...) or Arith(...).
  • Use string inputs for exact decimal values, especially money-like values.