npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mop233/algebra

v1.1.3

Published

A small, fast, easy-to-use library for primary mathematics.

Downloads

32

Readme

Algebra 是一个小巧、快速且易用的 JavaScript 库,它用于初级的代数运算。

安装使用

Common JS

npm install @mop233/algebra

安装后,可以直接导入 algebra 对象:

const algebra = require('@mop233/algebra')

console.log(algebra.version) // '1.0.0'

也可以解构导入所需的 API:

const { Fraction } = require('@mop233/algebra')
const f = new Fraction(1, 2)
console.log(f.toString()) // '1/2'

ES Module

npm install @mop233/algebra

algebra.js 默认导出 algebra 对象:

import algebra from '@mop233/algebra'

console.log(algebra.version) // '1.0.0'

const { Decimal } = algebra
const d1 = new Decimal(0.1)
const d2 = new Decimal(0.2)
console.log(d1.add(d2).toString()) // '0.3'

CDN 引入

浏览器中使用 algebra.js 最简单的方式就是 CDN 引入,引入后浏览器会自动将 algebra 对象添加到全局作用域:

<script src="https://unpkg.com/@mop233/algebra"></script>
<script>
  console.log(algebra.version) // '1.0.0'

  const { Decimal } = algebra
  console.log(Decimal.random().toString())  // '7'
</script>

快速开始

algebra 对象包含三个属性:

  • version:用于返回 algebra.js 库的版本号。
  • Decimal:一个用于生成整数和小数的类,并包含了它们的各种运算和操作方法。
  • Fraction:一个用于生成分数的类,并包含了分数的各种运算和操作方法。

Decimal

Decimal 是用于生成整数和小数的构造函数,该构造函数接受一个参数,并返回一个 Decimal 实例对象:

import algebra from '@mop233/algebra'
const { Decimal } = algebra

const d1 = new Decimal(9)
d1.valueOf() // 9

const d2 = new Decimal(3.14)
d2.toString() // '3.14'

Decimal 实例对象可以进行加减乘除四则运算和各种其他运算操作:

import algebra from '@mop233/algebra'
const { Decimal } = algebra

const d1 = new Decimal(9)
const d2 = new Decimal(3)

// 加
d1.add(d2).valueOf() // 12
// 减
d1.sub(d2).valueOf() // 6
// 乘
d1.mul(d2).valueOf() // 27
// 除
d1.div(d2).valueOf() // 3

// 绝对值
d1.abs().valueOf() // 9
// 相反数
d2.neg().valueOf() // -3
// 取余
d1.mod(d2).valueOf() // 0

// 等于
d1.eq(d2) // false
// 大于
d1.gt(d2) // true
// 小于
d1.lt(d2) // false

Fraction

Fraction 是用于生成分数的构造函数,该构造函数接受两个参数,并返回一个 Fraction 实例对象:

import algebra from '@mop233/algebra'
const { Fraction } = algebra

const f = new Fraction(1, 2)
f.valueOf() // 0.5
f.toString() // '1/2'
f.toTex() // '\frac{1}{2}'

Fraction 实例对象可以进行加减乘除四则运算和各种其他运算操作:

import algebra from '@mop233/algebra'
const { Fraction } = algebra

const f1 = new Fraction(1, 2)
const f2 = new Fraction(1, 3)

// 加
f1.add(f2).toString() // '5/6'
// 减
f1.sub(f2).toString() // '1/6'
// 乘
f1.mul(f2).toString() // '1/6'
// 除
f1.div(f2).toString() // '3/2'

// 绝对值
f1.abs().toString() // '1/2'
// 相反数
f1.neg().toString() // '-1/2'
// 倒数
f1.rec().toString() // '2'

// 等于
f1.eq(f2) // false
// 大于
f1.gt(f2) // true
// 小于
f1.lt(f2) // false

更多使用方法,请查看 Algebra 官方文档