# cmath-js

> C's and C++'s math functions in TypeScript & JavaScript

Latest version **5.14.1** (published 2026-08-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install cmath-js
pnpm add cmath-js
yarn add cmath-js
bun add cmath-js
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.14.1 |
| Published | 2026-08-23 |
| First published | 2020-12-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 53.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 2 |
| Author | Oskar Larsson Högfeldt |
| Maintainers | olh |
| Keywords | bigint, Bit operations, Bits, C, C++, cmath-js, cmath, double, ECMAScript, float, float64, frexp, gcd, IEEE754, int32, int53, iota, JavaScript, lcm, Math, Mathematics, Maths, NaN, Node, NodeJS, Numbers, Numerics, popcount, signbit, STL, TypeScript |

## Links

- npm: https://www.npmjs.com/package/cmath-js
- Repository: https://github.com/oskarlh/cmath-js
- Issues: https://github.com/oskarlh/cmath-js/issues
- Funding: https://github.com/oskarlh/cmath-js?sponsor=1
- npm.io page: https://npm.io/package/cmath-js

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 5.14.1 (latest) — 2026-08-23
- 5.14.0 — 2026-05-24
- 5.13.0 — 2026-05-15
- 5.12.0 — 2026-04-25
- 5.11.0 — 2026-04-25
- 5.10.0 — 2026-04-25
- 5.9.0 — 2026-04-25
- 5.8.0 — 2026-04-25
- 5.7.0 — 2026-04-25
- 5.6.4 — 2026-04-25
- 5.6.3 — 2026-04-25
- 5.6.2 — 2026-04-25
- 5.6.1 — 2026-04-25
- 5.6.0 — 2026-04-25
- 5.5.0 — 2026-04-25
- … 51 more at https://npm.io/package/cmath-js/versions

## README

# cmath-js

Implementation of parts of C++'s numerics libraries in TypeScript/JavaScript, including functions C++ inherits from C.

## Floating-point functions

- [`copysign`](https://en.cppreference.com/c/numeric/math/copysign)
- [`fabs`](https://en.cppreference.com/c/numeric/math/fabs)
- [`fpclassify`](https://en.cppreference.com/c/numeric/math/fpclassify)
- [`frexp`](https://en.cppreference.com/c/numeric/math/frexp)
- [`hypot`](https://en.cppreference.com/cpp/numeric/math/hypot)
- [`iscanonical`](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf#subsubsection.7.12.3.2)
- [`isfinite`](https://en.cppreference.com/c/numeric/math/isfinite)
- [`isinf`](https://en.cppreference.com/c/numeric/math/isinf)
- [`isnan`](https://en.cppreference.com/c/numeric/math/isnan)
- [`isnormal`](https://en.cppreference.com/c/numeric/math/isnormal)
- [`issignaling`](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf#subsubsection.7.12.3.8)
- [`issubnormal`](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf#subsubsection.7.12.3.9)
- [`iszero`](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf#subsubsection.7.12.3.10)
- [`ldexp`](https://en.cppreference.com/c/numeric/math/ldexp)
- [`nan`](https://en.cppreference.com/c/numeric/math/nan)
- [`nextafter`](https://en.cppreference.com/c/numeric/math/nextafter)
- [`pow`](https://en.cppreference.com/c/numeric/math/pow)

## Integer functions

These functions accept either a `bigint` or an integer `number`:

- [`abs`](https://en.cppreference.com/c/numeric/math/abs)
- [`bit_ceil`](https://en.cppreference.com/cpp/numeric/bit_ceil)
- [`bit_floor`](https://en.cppreference.com/cpp/numeric/bit_floor)
- [`bit_width`](https://en.cppreference.com/cpp/numeric/bit_width)
- [`countl_one`](https://en.cppreference.com/cpp/numeric/countl_one) as `create_countl_one`, which is used this way:
  ```ts
  import { create_countl_one } from "cmath-js";
  const countl_one_u16 = create_countl_one({ bits: 16 });
  const integer = 0xf0_00;
  // Prints "1111000000000000 has 4 leading one bits."
  console.log(
  	`${integer.toString(2).padStart(16, "0")} has ${countl_one_u16(integer)} leading one bits.`,
  );
  ```
- [`countl_zero`](https://en.cppreference.com/cpp/numeric/countl_zero) as `create_countl_zero`
- [`countr_one`](https://en.cppreference.com/cpp/numeric/countr_one) as `create_countr_one`
- [`countr_zero`](https://en.cppreference.com/cpp/numeric/countr_zero) as `create_countr_zero`
- [`div`](https://en.cppreference.com/cpp/numeric/math/div)
- [`gcd`](https://en.cppreference.com/cpp/numeric/gcd). Example:
  ```ts
  import { gcd } from "cmath-js";
  // Prints "The greatest common divisor of 24 and 32 is 8."
  console.log(`The greatest common divisor of 24 and 32 is ${gcd(24, 32)}.`);
  ```
- [`has_single_bit`](https://en.cppreference.com/cpp/numeric/has_single_bit)
- [`lcm`](https://en.cppreference.com/cpp/numeric/lcm)
- [`popcount`](https://en.cppreference.com/cpp/numeric/popcount)

## Functions for both floats and ints

- [`iota`](https://en.cppreference.com/cpp/algorithm/iota)
- [`signbit`](https://en.cppreference.com/c/numeric/math/signbit)

## Mathematical constants

- Constants in [the `std::numbers` namespace](https://en.cppreference.com/cpp/numeric/constants) are available from the `numbers` subpath export. Example:
  ```ts
  import { sqrt3 } from "cmath-js/numbers";
  console.log(`The square root of 3 is ${sqrt3}.`);
  ```

## Test coverage

The test coverage is a perfect 100% and enforced by the publishing and pull request verification workflows.

## Contributing

Contributions are welcome! See [CONTRIBUTING.md](./CONTRIBUTING.md)

---
_Source: https://npm.io/package/cmath-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
