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

@ma9pie/math

v1.0.0

Published

Simple math utility

Downloads

11

Readme

Easy math utility

A simple and easy-to-use mathematical calculation utility package.

🤔 Reason for development

When using precision calculation libraries such as big.js, bignumber.js, and decimals.js, we must create a constructor function with the new operator every time.

And the value 1e-7 is displayed as an exponential expression, such as '1e-7', not '0.0000001'.

To improve these points and make it easier to use, I developed this math utility library.

console.log(new Big(1e-7).valueOf()); // '1e-7'
console.log(math(1e-7).value()); // '0.0000001'

📦 Install

npm i @ma9pie/math

✨ Features

  • Simple API
  • Integer and floating point
  • Rest parameters
  • Test code

🕹 Use

import { math } from '@ma9pie/math';

You can develop calculation logic simply by using the math function.

math(1).add(2).sub(3).value(); // 0

Calculation-related methods can be chained.

math(10).add(5).sub(7).mul(2).div(4).value(); // 4

value

value() => string returns calculated value as string

math(1000).value(); // '1000'
math('1e7').value(); // '10000000'
math(1e-7).value(); // '0.0000001'

toNumber

toNumber() => number returns calculated value as number

math(1000).toNumber(); // 1000
math('1e7').toNumber(); // 10000000
math(1e-7).toNumber(); // 1e-7

add

.add(n) => Big n : number | string | BigInt returns the current value added by n. if the n is invalid, it is treated as 0.

0.1 + 0.2, // 0.30000000000000004
math(0.1).add(0.2).value(); // '0.3'
math(1).add(2).add(3).value(); // '6'
math(1).add(2, 3, 4).value(); // '10'

sub

.sub(n) => Big n : number | string | BigInt returns the current value subtracted by n. if the n is invalid, it is treated as 0.

0.3 - 0.1, // 0.19999999999999998
math(0.3).sub(0.1).value(); // '0.2'
math(1).sub(2).sub(3).value(); // '-4'
math(1).sub(2, 3, 4).value(); // '-8'

mul

.mul(n) => Big n : number | string | BigInt returns the current value multiplied by n. if the n is invalid, it is treated as 0.

0.6 * 3, // 1.7999999999999998
math(0.6).mul(3).value(); // '1.8'
math(2).mul(2).mul(2).value(); // '8'
math(2).mul(2, 2, 2).value(); // '16'

div

.div(n) => Big n : number | string | BigInt returns the current value divided by n. if the n is invalid, it is treated as 0.

355 / 113, // 3.1415929203539825
math(355).div(113).value(); // '3.141592920353982300'
math(10).div(3).value(); // '3.333333333333333333'
math(1000).div(10, 10, 10).value(); // '1'

abs

.abs() => Big returns the absolute current value.

math(-0.8).abs().value(); // '0.8'

pow

.pow(n) => Big n : number | string | BigInt returns the current value nth power. if the n is invalid, it is treated as 0.

0.7 ** 2, // 0.48999999999999994
Math.pow(0.7, 2); // 0.48999999999999994
math(0.7).pow(2).value(); // '0.49'

eq

.eq(n) => boolean n : number | string | BigInt returns whether the current value and n are the same.

0.1 + 0.2 === 0.3, // false
math(0.1).add(0.2).eq(0.3); // true
math('0.0000001').eq(1e-7); // true
math(1e7).eq(10000000); // true

gt

.gt(n) => boolean n : number | string | BigInt returns whether the current value is greater than n.

0.1 + 0.2 > 0.3, // true
math(0.1).add(0.2).gt(0.3); // false

gte

.gte(n) => boolean n : number | string | BigInt returns whether the current value is greater than or equal to n.

0.1 + 0.2 <= 0.3, // false
math(0.1).add(0.2).gte(0.3); // true

lt

.lt(n) => boolean n : number | string | BigInt returns whether the current value is less than n.

0.1 + 0.2 > 0.3, // true
math(0.1).add(0.2).lt(0.3); // false

lte

.lte(n) => boolean n : number | string | BigInt returns whether the current value is less than or equal to n.

0.1 + 0.2 <= 0.3, // false
math(0.1).add(0.2).lte(0.3); // true

isZero

.isZero() => boolean returns whether the current value is 0.

math(0).isZero(); // true
math(-0).isZero(); // true
math(0.0).isZero(); // true
math('0').isZero(); // true
math('-0').isZero(); // true
math(Number.MIN_SAFE_INTEGER).isZero(); // false
math(Number.MAX_SAFE_INTEGER).isZero(); // false

🔍 Test

npm run test