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 🙏

© 2025 – Pkg Stats / Ryan Hefner

calculator-by-str

v1.0.7

Published

Enter the string formula to get the calculation result.

Readme

公式

介绍

字符串公式插件

calculator-by-str功能

输入字符串公式,得到最终计算值 请看代码注释

born-fm功能

用于生成公式 详细细节 请看代码注释

安装

npm i calculator-by-str

calculator-by-str使用

const { fmFunc } = require("./calculator-by-str"); // 公式计算工具
let ret = fmFunc("1+1+(Math.round(1.5)*Math.sin(-0.5))")
console.log(ret)

born-fm使用

const { getRandomFm, ErrorTree } = require("./born-fm"); // 生成公式
const layer = 3 // 复杂度
let [newFm, newNum] = getRandomFm(layer)
// newFm 生成的公式
// newNum 生成公式的值 这个值的算法 和 calculator-by-str的算法不同
// 用newNum 的值 和 fmFunc(newFm) 的值比较 就知道算法是否正确

支持

  1. 支持函数随意增加括号,但要保持公式正确。
  2. 支持检查函数是否正确,检查Math函数参数个数是否正确,检查除0问题,如果有问题直接返回NaN。
  3. 支持生成字符串公式 born-fm.js
  4. 支持控制难度测试test.js - layer
  5. 支持测试进度条显示
  6. 支持json公式测试,outMath.json
  7. 支持公式写入文件 printToFile
  8. 支持忽略大小写 Math.round = Math.round
// 支持的函数
const formulas = {
    "Math.abs": [Math.abs, 1],
    "Math.acos": [Math.acos, 1],
    "Math.asin": [Math.asin, 1],
    "Math.atan": [Math.atan, 1],
    "Math.atan2": [Math.atan2, 2],
    "Math.ceil": [Math.ceil, 1],
    "Math.cos": [Math.cos, 1],
    "Math.floor": [Math.floor, 1],
    "Math.log": [Math.log, 1],
    "Math.max": [Math.max, 2], // max 为了和其他语言同步 改成两个 如果需要用多个参数 可以把2改成"more"
    "Math.min": [Math.min, 2], // min 为了和其他语言同步 改成两个 如果需要用多个参数 可以把2改成"more"
    "Math.pow": [Math.pow, 2],
    "Math.round": [round, 1],
    "Math.sin": [Math.sin, 1],
    "Math.sqrt": [Math.sqrt, 1],
    "Math.tan": [Math.tan, 1],

    "Math.mod": [mod, 2], // 非Math方法

    "Math.PI": [Math.PI],
    // "Math.SQRT2": [Math.SQRT2],// 需要的话可以打开注释,测试文件记得同步打开注释
    // "Math.LN10": [Math.LN10],
    // "Math.LOG10E": [Math.LOG10E],
    // "Math.LOG2E": [Math.LOG2E],
    // "Math.SQRT1_2": [Math.SQRT1_2],
    // "Math.SQRT2": [Math.SQRT2],
}

// 支持的计算符号
1. +
2. -
3. *
4. /
5. **

注意

  1. 目前没有使用JavaScript下面的math.round 可能与其他的语言计算方式不同,详见JavaScript Math.round()四舍五入误差问题。所有为了各种语言都是一样的计算结果,建议全部使用math.floor(x+0.5)的方式
  2. 取模(求余数)modmath下面没有实现,JavaScript里面可以使用%符号求余数,但是Go语言里面的%取模不支持小数,gomath.mod支持,所以程序里面,没有使用%,而是包装在math.mod里面了,字符串里面可以使用Math.mod(0.5)