0.0.12 • Published 10 months ago

@mojir/pretty-pi v0.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

PrettyPi

A TypeScript library for converting numbers to their symbolic mathematical representations.

Features

  • Identifies common mathematical constants (π, e, φ, etc.)
  • Recognizes and formats square roots, cube roots, and other radicals
  • Handles fractions with appropriate simplification
  • Converts decimal approximations of trigonometric values to their exact forms
  • Performs algebraic simplifications (e.g., 12·cos(π/6) → 6·√3)
  • Uses a flexible expression tree for robust representation

Installation

npm install pretty-pi

Usage

import { prettyPi } from "pretty-pi";

console.log(prettyPi(Math.PI)); // "π"
console.log(prettyPi(Math.E)); // "e"
console.log(prettyPi(0.5)); // "1/2"
console.log(prettyPi(Math.sqrt(2))); // "√2"
console.log(prettyPi(2 * Math.PI)); // "2·π"
console.log(prettyPi(Math.PI / 2)); // "π/2"
console.log(prettyPi(Math.sin(Math.PI / 4))); // "√2/2"
console.log(prettyPi(Math.PI ** 2)); // "π²"
console.log(prettyPi(Math.E * Math.PI)); // "e·π"
console.log(prettyPi(12 * Math.cos(Math.PI / 6))); // "6·√3"

How It Works

The library uses an expression tree representation to model mathematical expressions, which enables sophisticated simplification logic and consistent formatting. The main components include:

  • Expression Nodes: A hierarchy of node types representing different mathematical constructs (numbers, constants, operations)
  • Parser: Converts numeric values to expression trees
  • Simplification: Applies mathematical rules to produce the cleanest representation possible

Supported Representations

  • Basic constants: π, e, φ (golden ratio)
  • Fractions: 1/2, 3/4, etc.
  • Powers: π², e³
  • Square roots: √2, √3, etc.
  • Cube roots: ∛2, ∛3, etc.
  • Combinations: 2·π, π/4, π+1, etc.
  • Trigonometric values: sin(π/4) → √2/2, cos(π/3) → 1/2, etc.

Development

# Install dependencies
npm install

# Run tests
npm test

# Run tests in watch mode
npm run test:watch

# Build the library
npm run build

License

MIT

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.9

11 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago