@gitlon/math 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +210 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +117 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Long
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# @gitlon/math
|
|
2
|
+
|
|
3
|
+
防精度丢失的十进制四则运算工具。适合金额、数量、比例等不能直接依赖 JavaScript 浮点运算的场景。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @gitlon/math
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
也可安装聚合包:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add gitlon
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 导入
|
|
18
|
+
|
|
19
|
+
直接使用子包:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { add, divide, multiply, round, subtract, toFixed } from '@gitlon/math'
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
通过主包使用:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { add, divide, toFixed } from 'gitlon'
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 基础用法
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
add(0.1, 0.2) // 0.3
|
|
35
|
+
subtract(0.3, 0.1) // 0.2
|
|
36
|
+
multiply(0.1, 0.2) // 0.02
|
|
37
|
+
divide(0.3, 0.1) // 3
|
|
38
|
+
|
|
39
|
+
round(1.005, 2) // 1.01
|
|
40
|
+
toFixed(1.2, 2) // '1.20'
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
函数内部按十进制字符串拆分为整数系数和小数位,再进行计算,避免常见浮点误差:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
0.1 + 0.2 // 0.30000000000000004
|
|
47
|
+
add(0.1, 0.2) // 0.3
|
|
48
|
+
|
|
49
|
+
0.3 / 0.1 // 2.9999999999999996
|
|
50
|
+
// divide(0.3, 0.1) // 3
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 类型
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
type Numeric = number | string
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
所有函数均接受 `number` 或 `string`:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
add('0.1', '0.2') // 0.3
|
|
63
|
+
multiply('12.5', 2) // 25
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- 空字符串、空白字符串、非法数字字符串返回 `NaN`。
|
|
67
|
+
- 四则运算结果为 `number`。
|
|
68
|
+
- `toFixed` 结果为 `string`。
|
|
69
|
+
- 不抛出输入错误异常,可使用 `Number.isNaN()` 判断失败。
|
|
70
|
+
|
|
71
|
+
## 加法
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
function add(...values: Numeric[]): number
|
|
75
|
+
const plus: typeof add
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
add(0.1, 0.2) // 0.3
|
|
80
|
+
add(1, 2, 3) // 6
|
|
81
|
+
add('10.50', 0.25) // 10.75
|
|
82
|
+
add() // 0
|
|
83
|
+
add('invalid', 1) // NaN
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`plus` 是 `add` 的别名:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { plus } from '@gitlon/math'
|
|
90
|
+
|
|
91
|
+
plus(0.1, 0.2) // 0.3
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## 减法
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
function subtract(...values: Numeric[]): number
|
|
98
|
+
const minus: typeof subtract
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
从左到右计算:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
subtract(10, 2, 3) // 5
|
|
105
|
+
subtract('1.00', 0.1) // 0.9
|
|
106
|
+
subtract(10) // 10
|
|
107
|
+
subtract() // NaN
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`minus` 是 `subtract` 的别名。
|
|
111
|
+
|
|
112
|
+
## 乘法
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
function multiply(...values: Numeric[]): number
|
|
116
|
+
const times: typeof multiply
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
从左到右计算:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
multiply(2, 3, 4) // 24
|
|
123
|
+
multiply(0.1, 0.2) // 0.02
|
|
124
|
+
multiply('1.25', 8) // 10
|
|
125
|
+
multiply() // 1
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
`times` 是 `multiply` 的别名。
|
|
129
|
+
|
|
130
|
+
## 除法
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
function divide(...values: Numeric[]): number
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
从左到右计算:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
divide(100, 2, 5) // 10
|
|
140
|
+
divide(0.3, 0.1) // 3
|
|
141
|
+
divide(1, 3) // 0.3333333333333333
|
|
142
|
+
divide(10) // 10
|
|
143
|
+
divide() // NaN
|
|
144
|
+
divide(10, 0) // NaN
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
除数为零、参数为空或任意参数非法时返回 `NaN`。
|
|
148
|
+
|
|
149
|
+
## 舍入
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
function round(value: Numeric, decimals?: number): number
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
使用十进制四舍五入,默认保留 0 位:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
round(1.005, 2) // 1.01
|
|
159
|
+
round(1.234, 2) // 1.23
|
|
160
|
+
round(-1.005, 2) // -1.01
|
|
161
|
+
round(12.5) // 13
|
|
162
|
+
round(1.234, -1) // 1
|
|
163
|
+
round('invalid') // NaN
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`decimals` 会向下取整;负数按 `0` 处理。
|
|
167
|
+
|
|
168
|
+
## 固定小数位
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
function toFixed(value: Numeric, decimals?: number): string
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
返回字符串,保留指定小数位:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
toFixed(1.005, 2) // '1.01'
|
|
178
|
+
toFixed(1.2, 2) // '1.20'
|
|
179
|
+
toFixed(12.5) // '13'
|
|
180
|
+
toFixed(12.5, 3) // '12.500'
|
|
181
|
+
toFixed('invalid', 2) // ''
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
与 `Number.prototype.toFixed` 不同,先进行十进制四舍五入,避免 `1.005.toFixed(2)` 这类浮点误差。
|
|
185
|
+
|
|
186
|
+
## 多参数规则
|
|
187
|
+
|
|
188
|
+
四则运算支持两个或多个参数,均从左到右计算:
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
add(a, b, c) // (a + b) + c
|
|
192
|
+
subtract(a, b, c) // (a - b) - c
|
|
193
|
+
multiply(a, b, c) // (a * b) * c
|
|
194
|
+
divide(a, b, c) // (a / b) / c
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
| 函数 | 无参数结果 |
|
|
198
|
+
| --- | --- |
|
|
199
|
+
| `add` | `0` |
|
|
200
|
+
| `subtract` | `NaN` |
|
|
201
|
+
| `multiply` | `1` |
|
|
202
|
+
| `divide` | `NaN` |
|
|
203
|
+
|
|
204
|
+
## 边界行为
|
|
205
|
+
|
|
206
|
+
- 输入支持普通十进制、负数、小数和科学计数法字符串。
|
|
207
|
+
- 非法字符串、空字符串、空白字符串返回 `NaN`;`toFixed` 返回 `''`。
|
|
208
|
+
- 除零返回 `NaN`。
|
|
209
|
+
- 运算最终仍返回 JavaScript `number`,超出 `number` 可表示范围时遵循 JavaScript 数值限制。
|
|
210
|
+
- 需要保留尾零时使用 `toFixed`,不要使用返回 `number` 的 `round`。
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e){if(typeof e==`string`&&e.trim()===``)return;let t=String(e).trim();if(!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(t))return;let[n,r]=t.toLowerCase().split(`e`),i=Number(r??0);if(!Number.isFinite(Number(t))||Math.abs(i)>1e3)return;let a=n.startsWith(`-`)?-1n:1n,[o,s=``]=n.replace(/^[+-]/,``).split(`.`),c=`${o}${s}`.replace(/^0+(?=\d)/,``)||`0`,l=s.length-i,u=a*BigInt(c);return l>=0?{coefficient:u,scale:l}:{coefficient:u*10n**BigInt(-l),scale:0}}function t(t){let n=t.map(e);return n.every(e=>e!==void 0)?n:void 0}function n(e,t){let n=Math.max(e.scale,t.scale);return[e.coefficient*10n**BigInt(n-e.scale),t.coefficient*10n**BigInt(n-t.scale),n]}function r(e,t=e.scale){let n=e.coefficient<0n?`-`:``,r=(e.coefficient<0n?-e.coefficient:e.coefficient).toString(),i=Math.max(0,t),a=r.padStart(i+1,`0`),o=a.length-i,s=a.slice(0,o),c=a.slice(o).replace(/0+$/,``);return`${n}${s}${c?`.${c}`:``}`}function i(e){return Number(r(e))}function a(e,t){let[r,i,a]=n(e,t);return{coefficient:r+i,scale:a}}function o(e,t){let[r,i,a]=n(e,t);return{coefficient:r-i,scale:a}}function s(e,t){return{coefficient:e.coefficient*t.coefficient,scale:e.scale+t.scale}}function c(...e){if(e.length===0)return 0;let n=t(e);return n?i(n.reduce(a)):NaN}var l=c;function u(...e){if(e.length===0)return NaN;let n=t(e);return n?i(n.slice(1).reduce(o,n[0])):NaN}var d=u;function f(...e){if(e.length===0)return 1;let n=t(e);return n?i(n.reduce(s)):NaN}var p=f;function m(...e){if(e.length===0)return NaN;let n=t(e);if(!n||n.some((e,t)=>t>0&&e.coefficient===0n))return NaN;let r=n[0];for(let e of n.slice(1)){let t=r.coefficient<0n==e.coefficient<0n?1n:-1n,n=r.coefficient<0n?-r.coefficient:r.coefficient,i=e.coefficient<0n?-e.coefficient:e.coefficient,a=20+e.scale-r.scale;r={coefficient:t*((a>=0?n*10n**BigInt(a):n/10n**BigInt(-a))/i),scale:20}}return i(r)}function h(e){return Number.isFinite(e)?Math.max(0,Math.floor(e)):0}function g(e,t){if(e.scale<=t)return{coefficient:e.coefficient*10n**BigInt(t-e.scale),scale:t};let n=10n**BigInt(e.scale-t),r=e.coefficient/n;return{coefficient:(e.coefficient<0n?-e.coefficient%n:e.coefficient%n)*2n>=n?r+(e.coefficient<0n?-1n:1n):r,scale:t}}function _(t,n=0){let r=e(t);return r?i(g(r,h(n))):NaN}function v(t,n=0){let r=e(t);if(!r)return``;let i=h(n),a=g(r,i),o=a.coefficient<0n?`-`:``,s=(a.coefficient<0n?-a.coefficient:a.coefficient).toString().padStart(i+1,`0`);if(i===0)return`${o}${s}`;let c=s.length-i;return`${o}${s.slice(0,c)}.${s.slice(c)}`}exports.add=c,exports.divide=m,exports.minus=d,exports.multiply=f,exports.plus=l,exports.round=_,exports.subtract=u,exports.times=p,exports.toFixed=v;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["export type Numeric = number | string\n\ntype Decimal = {\n coefficient: bigint\n scale: number\n}\n\nfunction parseDecimal(value: Numeric): Decimal | undefined {\n if (typeof value === 'string' && value.trim() === '') return undefined\n\n const text = String(value).trim()\n if (!/^[+-]?(?:\\d+\\.?\\d*|\\.\\d+)(?:e[+-]?\\d+)?$/i.test(text)) return undefined\n\n const [base, exponentText] = text.toLowerCase().split('e')\n const exponent = Number(exponentText ?? 0)\n if (!Number.isFinite(Number(text)) || Math.abs(exponent) > 1000) return undefined\n const sign = base.startsWith('-') ? -1n : 1n\n const unsigned = base.replace(/^[+-]/, '')\n const [integer, fraction = ''] = unsigned.split('.')\n const digits = `${integer}${fraction}`.replace(/^0+(?=\\d)/, '') || '0'\n const scale = fraction.length - exponent\n const coefficient = sign * BigInt(digits)\n\n if (scale >= 0) return { coefficient, scale }\n return { coefficient: coefficient * 10n ** BigInt(-scale), scale: 0 }\n}\n\nfunction parseValues(values: Numeric[]): Decimal[] | undefined {\n const decimals = values.map(parseDecimal)\n return decimals.every((value): value is Decimal => value !== undefined) ? decimals : undefined\n}\n\nfunction align(left: Decimal, right: Decimal): [bigint, bigint, number] {\n const scale = Math.max(left.scale, right.scale)\n return [\n left.coefficient * 10n ** BigInt(scale - left.scale),\n right.coefficient * 10n ** BigInt(scale - right.scale),\n scale,\n ]\n}\n\nfunction decimalToString(value: Decimal, fixedScale = value.scale): string {\n const sign = value.coefficient < 0n ? '-' : ''\n const digits = (value.coefficient < 0n ? -value.coefficient : value.coefficient).toString()\n const scale = Math.max(0, fixedScale)\n const padded = digits.padStart(scale + 1, '0')\n const point = padded.length - scale\n const integer = padded.slice(0, point)\n const fraction = padded.slice(point).replace(/0+$/, '')\n return `${sign}${integer}${fraction ? `.${fraction}` : ''}`\n}\n\nfunction decimalToNumber(value: Decimal): number {\n return Number(decimalToString(value))\n}\n\nfunction addDecimals(left: Decimal, right: Decimal): Decimal {\n const [leftCoefficient, rightCoefficient, scale] = align(left, right)\n return { coefficient: leftCoefficient + rightCoefficient, scale }\n}\n\nfunction subtractDecimals(left: Decimal, right: Decimal): Decimal {\n const [leftCoefficient, rightCoefficient, scale] = align(left, right)\n return { coefficient: leftCoefficient - rightCoefficient, scale }\n}\n\nfunction multiplyDecimals(left: Decimal, right: Decimal): Decimal {\n return {\n coefficient: left.coefficient * right.coefficient,\n scale: left.scale + right.scale,\n }\n}\n\nexport function add(...values: Numeric[]): number {\n if (values.length === 0) return 0\n const decimals = parseValues(values)\n if (!decimals) return Number.NaN\n return decimalToNumber(decimals.reduce(addDecimals))\n}\n\nexport const plus = add\n\nexport function subtract(...values: Numeric[]): number {\n if (values.length === 0) return Number.NaN\n const decimals = parseValues(values)\n if (!decimals) return Number.NaN\n return decimalToNumber(decimals.slice(1).reduce(subtractDecimals, decimals[0]))\n}\n\nexport const minus = subtract\n\nexport function multiply(...values: Numeric[]): number {\n if (values.length === 0) return 1\n const decimals = parseValues(values)\n if (!decimals) return Number.NaN\n return decimalToNumber(decimals.reduce(multiplyDecimals))\n}\n\nexport const times = multiply\n\nexport function divide(...values: Numeric[]): number {\n if (values.length === 0) return Number.NaN\n const decimals = parseValues(values)\n if (!decimals || decimals.some((value, index) => index > 0 && value.coefficient === 0n)) {\n return Number.NaN\n }\n\n let result = decimals[0]\n for (const divisor of decimals.slice(1)) {\n const sameSign = (result.coefficient < 0n) === (divisor.coefficient < 0n)\n const sign = sameSign ? 1n : -1n\n const dividend = result.coefficient < 0n ? -result.coefficient : result.coefficient\n const divisorCoefficient = divisor.coefficient < 0n ? -divisor.coefficient : divisor.coefficient\n const scale = 20 + divisor.scale - result.scale\n const scaledDividend = scale >= 0 ? dividend * 10n ** BigInt(scale) : dividend / 10n ** BigInt(-scale)\n const quotient = scaledDividend / divisorCoefficient\n result = { coefficient: sign * quotient, scale: 20 }\n }\n\n return decimalToNumber(result)\n}\n\nfunction normalizeDecimals(decimals: number): number {\n return Number.isFinite(decimals) ? Math.max(0, Math.floor(decimals)) : 0\n}\n\nfunction roundDecimal(decimal: Decimal, targetScale: number): Decimal {\n if (decimal.scale <= targetScale) {\n return {\n coefficient: decimal.coefficient * 10n ** BigInt(targetScale - decimal.scale),\n scale: targetScale,\n }\n }\n\n const divisor = 10n ** BigInt(decimal.scale - targetScale)\n const quotient = decimal.coefficient / divisor\n const remainder = decimal.coefficient < 0n\n ? (-decimal.coefficient) % divisor\n : decimal.coefficient % divisor\n const coefficient = remainder * 2n >= divisor\n ? quotient + (decimal.coefficient < 0n ? -1n : 1n)\n : quotient\n\n return { coefficient, scale: targetScale }\n}\n\nexport function round(value: Numeric, decimals = 0): number {\n const decimal = parseDecimal(value)\n if (!decimal) return Number.NaN\n return decimalToNumber(roundDecimal(decimal, normalizeDecimals(decimals)))\n}\n\nexport function toFixed(value: Numeric, decimals = 0): string {\n const decimal = parseDecimal(value)\n if (!decimal) return ''\n\n const targetScale = normalizeDecimals(decimals)\n const rounded = roundDecimal(decimal, targetScale)\n const sign = rounded.coefficient < 0n ? '-' : ''\n const digits = (rounded.coefficient < 0n ? -rounded.coefficient : rounded.coefficient)\n .toString()\n .padStart(targetScale + 1, '0')\n\n if (targetScale === 0) return `${sign}${digits}`\n const point = digits.length - targetScale\n return `${sign}${digits.slice(0, point)}.${digits.slice(point)}`\n}\n"],"mappings":"mEAOA,SAAS,EAAa,EAAqC,CACzD,GAAI,OAAO,GAAU,UAAY,EAAM,KAAK,IAAM,GAAI,OAEtD,IAAM,EAAO,OAAO,CAAK,CAAC,CAAC,KAAK,EAChC,GAAI,CAAC,4CAA4C,KAAK,CAAI,EAAG,OAE7D,GAAM,CAAC,EAAM,GAAgB,EAAK,YAAY,CAAC,CAAC,MAAM,GAAG,EACnD,EAAW,OAAO,GAAgB,CAAC,EACzC,GAAI,CAAC,OAAO,SAAS,OAAO,CAAI,CAAC,GAAK,KAAK,IAAI,CAAQ,EAAI,IAAM,OACjE,IAAM,EAAO,EAAK,WAAW,GAAG,EAAI,CAAC,GAAK,GAEpC,CAAC,EAAS,EAAW,IADV,EAAK,QAAQ,QAAS,EACN,CAAA,CAAS,MAAM,GAAG,EAC7C,EAAS,GAAG,IAAU,IAAW,QAAQ,YAAa,EAAE,GAAK,IAC7D,EAAQ,EAAS,OAAS,EAC1B,EAAc,EAAO,OAAO,CAAM,EAGxC,OADI,GAAS,EAAU,CAAE,cAAa,OAAM,EACrC,CAAE,YAAa,EAAc,KAAO,OAAO,CAAC,CAAK,EAAG,MAAO,CAAE,CACtE,CAEA,SAAS,EAAY,EAA0C,CAC7D,IAAM,EAAW,EAAO,IAAI,CAAY,EACxC,OAAO,EAAS,MAAO,GAA4B,IAAU,IAAA,EAAS,EAAI,EAAW,IAAA,EACvF,CAEA,SAAS,EAAM,EAAe,EAA0C,CACtE,IAAM,EAAQ,KAAK,IAAI,EAAK,MAAO,EAAM,KAAK,EAC9C,MAAO,CACL,EAAK,YAAc,KAAO,OAAO,EAAQ,EAAK,KAAK,EACnD,EAAM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MAAQ,CAAW,EACnD,EAAW,EAAQ,YAAc,EAQvC,MAAO,CAAE,aAPS,EAAQ,YAAc,GACnC,CAAC,EAAQ,YAAe,EACzB,EAAQ,YAAc,GACM,IAAM,EAClC,GAAY,EAAQ,YAAc,GAAK,CAAC,GAAK,IAC7C,EAEkB,MAAO,CAAY,CAC3C,CAEA,SAAgB,EAAM,EAAgB,EAAW,EAAW,CAC1D,IAAM,EAAU,EAAa,CAAK,EAElC,OADK,EACE,EAAgB,EAAa,EAAS,EAAkB,CAAQ,CAAC,CAAC,EADpD,GAEvB,CAEA,SAAgB,EAAQ,EAAgB,EAAW,EAAW,CAC5D,IAAM,EAAU,EAAa,CAAK,EAClC,GAAI,CAAC,EAAS,MAAO,GAErB,IAAM,EAAc,EAAkB,CAAQ,EACxC,EAAU,EAAa,EAAS,CAAW,EAC3C,EAAO,EAAQ,YAAc,GAAK,IAAM,GACxC,GAAU,EAAQ,YAAc,GAAK,CAAC,EAAQ,YAAc,EAAQ,YAAA,CACvE,SAAS,CAAC,CACV,SAAS,EAAc,EAAG,GAAG,EAEhC,GAAI,IAAgB,EAAG,MAAO,GAAG,IAAO,IACxC,IAAM,EAAQ,EAAO,OAAS,EAC9B,MAAO,GAAG,IAAO,EAAO,MAAM,EAAG,CAAK,EAAE,GAAG,EAAO,MAAM,CAAK,GAC/D"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare function add(...values: Numeric[]): number;
|
|
2
|
+
|
|
3
|
+
export declare function divide(...values: Numeric[]): number;
|
|
4
|
+
|
|
5
|
+
export declare const minus: typeof subtract;
|
|
6
|
+
|
|
7
|
+
export declare function multiply(...values: Numeric[]): number;
|
|
8
|
+
|
|
9
|
+
export declare type Numeric = number | string;
|
|
10
|
+
|
|
11
|
+
export declare const plus: typeof add;
|
|
12
|
+
|
|
13
|
+
export declare function round(value: Numeric, decimals?: number): number;
|
|
14
|
+
|
|
15
|
+
export declare function subtract(...values: Numeric[]): number;
|
|
16
|
+
|
|
17
|
+
export declare const times: typeof multiply;
|
|
18
|
+
|
|
19
|
+
export declare function toFixed(value: Numeric, decimals?: number): string;
|
|
20
|
+
|
|
21
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
function e(e) {
|
|
3
|
+
if (typeof e == "string" && e.trim() === "") return;
|
|
4
|
+
let t = String(e).trim();
|
|
5
|
+
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(t)) return;
|
|
6
|
+
let [n, r] = t.toLowerCase().split("e"), i = Number(r ?? 0);
|
|
7
|
+
if (!Number.isFinite(Number(t)) || Math.abs(i) > 1e3) return;
|
|
8
|
+
let a = n.startsWith("-") ? -1n : 1n, [o, s = ""] = n.replace(/^[+-]/, "").split("."), c = `${o}${s}`.replace(/^0+(?=\d)/, "") || "0", l = s.length - i, u = a * BigInt(c);
|
|
9
|
+
return l >= 0 ? {
|
|
10
|
+
coefficient: u,
|
|
11
|
+
scale: l
|
|
12
|
+
} : {
|
|
13
|
+
coefficient: u * 10n ** BigInt(-l),
|
|
14
|
+
scale: 0
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function t(t) {
|
|
18
|
+
let n = t.map(e);
|
|
19
|
+
return n.every((e) => e !== void 0) ? n : void 0;
|
|
20
|
+
}
|
|
21
|
+
function n(e, t) {
|
|
22
|
+
let n = Math.max(e.scale, t.scale);
|
|
23
|
+
return [
|
|
24
|
+
e.coefficient * 10n ** BigInt(n - e.scale),
|
|
25
|
+
t.coefficient * 10n ** BigInt(n - t.scale),
|
|
26
|
+
n
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
function r(e, t = e.scale) {
|
|
30
|
+
let n = e.coefficient < 0n ? "-" : "", r = (e.coefficient < 0n ? -e.coefficient : e.coefficient).toString(), i = Math.max(0, t), a = r.padStart(i + 1, "0"), o = a.length - i, s = a.slice(0, o), c = a.slice(o).replace(/0+$/, "");
|
|
31
|
+
return `${n}${s}${c ? `.${c}` : ""}`;
|
|
32
|
+
}
|
|
33
|
+
function i(e) {
|
|
34
|
+
return Number(r(e));
|
|
35
|
+
}
|
|
36
|
+
function a(e, t) {
|
|
37
|
+
let [r, i, a] = n(e, t);
|
|
38
|
+
return {
|
|
39
|
+
coefficient: r + i,
|
|
40
|
+
scale: a
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function o(e, t) {
|
|
44
|
+
let [r, i, a] = n(e, t);
|
|
45
|
+
return {
|
|
46
|
+
coefficient: r - i,
|
|
47
|
+
scale: a
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function s(e, t) {
|
|
51
|
+
return {
|
|
52
|
+
coefficient: e.coefficient * t.coefficient,
|
|
53
|
+
scale: e.scale + t.scale
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function c(...e) {
|
|
57
|
+
if (e.length === 0) return 0;
|
|
58
|
+
let n = t(e);
|
|
59
|
+
return n ? i(n.reduce(a)) : NaN;
|
|
60
|
+
}
|
|
61
|
+
var l = c;
|
|
62
|
+
function u(...e) {
|
|
63
|
+
if (e.length === 0) return NaN;
|
|
64
|
+
let n = t(e);
|
|
65
|
+
return n ? i(n.slice(1).reduce(o, n[0])) : NaN;
|
|
66
|
+
}
|
|
67
|
+
var d = u;
|
|
68
|
+
function f(...e) {
|
|
69
|
+
if (e.length === 0) return 1;
|
|
70
|
+
let n = t(e);
|
|
71
|
+
return n ? i(n.reduce(s)) : NaN;
|
|
72
|
+
}
|
|
73
|
+
var p = f;
|
|
74
|
+
function m(...e) {
|
|
75
|
+
if (e.length === 0) return NaN;
|
|
76
|
+
let n = t(e);
|
|
77
|
+
if (!n || n.some((e, t) => t > 0 && e.coefficient === 0n)) return NaN;
|
|
78
|
+
let r = n[0];
|
|
79
|
+
for (let e of n.slice(1)) {
|
|
80
|
+
let t = r.coefficient < 0n == e.coefficient < 0n ? 1n : -1n, n = r.coefficient < 0n ? -r.coefficient : r.coefficient, i = e.coefficient < 0n ? -e.coefficient : e.coefficient, a = 20 + e.scale - r.scale;
|
|
81
|
+
r = {
|
|
82
|
+
coefficient: t * ((a >= 0 ? n * 10n ** BigInt(a) : n / 10n ** BigInt(-a)) / i),
|
|
83
|
+
scale: 20
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return i(r);
|
|
87
|
+
}
|
|
88
|
+
function h(e) {
|
|
89
|
+
return Number.isFinite(e) ? Math.max(0, Math.floor(e)) : 0;
|
|
90
|
+
}
|
|
91
|
+
function g(e, t) {
|
|
92
|
+
if (e.scale <= t) return {
|
|
93
|
+
coefficient: e.coefficient * 10n ** BigInt(t - e.scale),
|
|
94
|
+
scale: t
|
|
95
|
+
};
|
|
96
|
+
let n = 10n ** BigInt(e.scale - t), r = e.coefficient / n;
|
|
97
|
+
return {
|
|
98
|
+
coefficient: (e.coefficient < 0n ? -e.coefficient % n : e.coefficient % n) * 2n >= n ? r + (e.coefficient < 0n ? -1n : 1n) : r,
|
|
99
|
+
scale: t
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function _(t, n = 0) {
|
|
103
|
+
let r = e(t);
|
|
104
|
+
return r ? i(g(r, h(n))) : NaN;
|
|
105
|
+
}
|
|
106
|
+
function v(t, n = 0) {
|
|
107
|
+
let r = e(t);
|
|
108
|
+
if (!r) return "";
|
|
109
|
+
let i = h(n), a = g(r, i), o = a.coefficient < 0n ? "-" : "", s = (a.coefficient < 0n ? -a.coefficient : a.coefficient).toString().padStart(i + 1, "0");
|
|
110
|
+
if (i === 0) return `${o}${s}`;
|
|
111
|
+
let c = s.length - i;
|
|
112
|
+
return `${o}${s.slice(0, c)}.${s.slice(c)}`;
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
export { c as add, m as divide, d as minus, f as multiply, l as plus, _ as round, u as subtract, p as times, v as toFixed };
|
|
116
|
+
|
|
117
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["export type Numeric = number | string\n\ntype Decimal = {\n coefficient: bigint\n scale: number\n}\n\nfunction parseDecimal(value: Numeric): Decimal | undefined {\n if (typeof value === 'string' && value.trim() === '') return undefined\n\n const text = String(value).trim()\n if (!/^[+-]?(?:\\d+\\.?\\d*|\\.\\d+)(?:e[+-]?\\d+)?$/i.test(text)) return undefined\n\n const [base, exponentText] = text.toLowerCase().split('e')\n const exponent = Number(exponentText ?? 0)\n if (!Number.isFinite(Number(text)) || Math.abs(exponent) > 1000) return undefined\n const sign = base.startsWith('-') ? -1n : 1n\n const unsigned = base.replace(/^[+-]/, '')\n const [integer, fraction = ''] = unsigned.split('.')\n const digits = `${integer}${fraction}`.replace(/^0+(?=\\d)/, '') || '0'\n const scale = fraction.length - exponent\n const coefficient = sign * BigInt(digits)\n\n if (scale >= 0) return { coefficient, scale }\n return { coefficient: coefficient * 10n ** BigInt(-scale), scale: 0 }\n}\n\nfunction parseValues(values: Numeric[]): Decimal[] | undefined {\n const decimals = values.map(parseDecimal)\n return decimals.every((value): value is Decimal => value !== undefined) ? decimals : undefined\n}\n\nfunction align(left: Decimal, right: Decimal): [bigint, bigint, number] {\n const scale = Math.max(left.scale, right.scale)\n return [\n left.coefficient * 10n ** BigInt(scale - left.scale),\n right.coefficient * 10n ** BigInt(scale - right.scale),\n scale,\n ]\n}\n\nfunction decimalToString(value: Decimal, fixedScale = value.scale): string {\n const sign = value.coefficient < 0n ? '-' : ''\n const digits = (value.coefficient < 0n ? -value.coefficient : value.coefficient).toString()\n const scale = Math.max(0, fixedScale)\n const padded = digits.padStart(scale + 1, '0')\n const point = padded.length - scale\n const integer = padded.slice(0, point)\n const fraction = padded.slice(point).replace(/0+$/, '')\n return `${sign}${integer}${fraction ? `.${fraction}` : ''}`\n}\n\nfunction decimalToNumber(value: Decimal): number {\n return Number(decimalToString(value))\n}\n\nfunction addDecimals(left: Decimal, right: Decimal): Decimal {\n const [leftCoefficient, rightCoefficient, scale] = align(left, right)\n return { coefficient: leftCoefficient + rightCoefficient, scale }\n}\n\nfunction subtractDecimals(left: Decimal, right: Decimal): Decimal {\n const [leftCoefficient, rightCoefficient, scale] = align(left, right)\n return { coefficient: leftCoefficient - rightCoefficient, scale }\n}\n\nfunction multiplyDecimals(left: Decimal, right: Decimal): Decimal {\n return {\n coefficient: left.coefficient * right.coefficient,\n scale: left.scale + right.scale,\n }\n}\n\nexport function add(...values: Numeric[]): number {\n if (values.length === 0) return 0\n const decimals = parseValues(values)\n if (!decimals) return Number.NaN\n return decimalToNumber(decimals.reduce(addDecimals))\n}\n\nexport const plus = add\n\nexport function subtract(...values: Numeric[]): number {\n if (values.length === 0) return Number.NaN\n const decimals = parseValues(values)\n if (!decimals) return Number.NaN\n return decimalToNumber(decimals.slice(1).reduce(subtractDecimals, decimals[0]))\n}\n\nexport const minus = subtract\n\nexport function multiply(...values: Numeric[]): number {\n if (values.length === 0) return 1\n const decimals = parseValues(values)\n if (!decimals) return Number.NaN\n return decimalToNumber(decimals.reduce(multiplyDecimals))\n}\n\nexport const times = multiply\n\nexport function divide(...values: Numeric[]): number {\n if (values.length === 0) return Number.NaN\n const decimals = parseValues(values)\n if (!decimals || decimals.some((value, index) => index > 0 && value.coefficient === 0n)) {\n return Number.NaN\n }\n\n let result = decimals[0]\n for (const divisor of decimals.slice(1)) {\n const sameSign = (result.coefficient < 0n) === (divisor.coefficient < 0n)\n const sign = sameSign ? 1n : -1n\n const dividend = result.coefficient < 0n ? -result.coefficient : result.coefficient\n const divisorCoefficient = divisor.coefficient < 0n ? -divisor.coefficient : divisor.coefficient\n const scale = 20 + divisor.scale - result.scale\n const scaledDividend = scale >= 0 ? dividend * 10n ** BigInt(scale) : dividend / 10n ** BigInt(-scale)\n const quotient = scaledDividend / divisorCoefficient\n result = { coefficient: sign * quotient, scale: 20 }\n }\n\n return decimalToNumber(result)\n}\n\nfunction normalizeDecimals(decimals: number): number {\n return Number.isFinite(decimals) ? Math.max(0, Math.floor(decimals)) : 0\n}\n\nfunction roundDecimal(decimal: Decimal, targetScale: number): Decimal {\n if (decimal.scale <= targetScale) {\n return {\n coefficient: decimal.coefficient * 10n ** BigInt(targetScale - decimal.scale),\n scale: targetScale,\n }\n }\n\n const divisor = 10n ** BigInt(decimal.scale - targetScale)\n const quotient = decimal.coefficient / divisor\n const remainder = decimal.coefficient < 0n\n ? (-decimal.coefficient) % divisor\n : decimal.coefficient % divisor\n const coefficient = remainder * 2n >= divisor\n ? quotient + (decimal.coefficient < 0n ? -1n : 1n)\n : quotient\n\n return { coefficient, scale: targetScale }\n}\n\nexport function round(value: Numeric, decimals = 0): number {\n const decimal = parseDecimal(value)\n if (!decimal) return Number.NaN\n return decimalToNumber(roundDecimal(decimal, normalizeDecimals(decimals)))\n}\n\nexport function toFixed(value: Numeric, decimals = 0): string {\n const decimal = parseDecimal(value)\n if (!decimal) return ''\n\n const targetScale = normalizeDecimals(decimals)\n const rounded = roundDecimal(decimal, targetScale)\n const sign = rounded.coefficient < 0n ? '-' : ''\n const digits = (rounded.coefficient < 0n ? -rounded.coefficient : rounded.coefficient)\n .toString()\n .padStart(targetScale + 1, '0')\n\n if (targetScale === 0) return `${sign}${digits}`\n const point = digits.length - targetScale\n return `${sign}${digits.slice(0, point)}.${digits.slice(point)}`\n}\n"],"mappings":";AAOA,SAAS,EAAa,GAAqC;CACzD,IAAI,OAAO,KAAU,YAAY,EAAM,KAAK,MAAM,IAAI;CAEtD,IAAM,IAAO,OAAO,CAAK,CAAC,CAAC,KAAK;CAChC,IAAI,CAAC,4CAA4C,KAAK,CAAI,GAAG;CAE7D,IAAM,CAAC,GAAM,KAAgB,EAAK,YAAY,CAAC,CAAC,MAAM,GAAG,GACnD,IAAW,OAAO,KAAgB,CAAC;CACzC,IAAI,CAAC,OAAO,SAAS,OAAO,CAAI,CAAC,KAAK,KAAK,IAAI,CAAQ,IAAI,KAAM;CACjE,IAAM,IA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AC,EAAQ,cAAc,EAAQ,YAAA,CACvE,SAAS,CAAC,CACV,SAAS,IAAc,GAAG,GAAG;CAEhC,IAAI,MAAgB,GAAG,OAAO,GAAG,IAAO;CACxC,IAAM,IAAQ,EAAO,SAAS;CAC9B,OAAO,GAAG,IAAO,EAAO,MAAM,GAAG,CAAK,EAAE,GAAG,EAAO,MAAM,CAAK;AAC/D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gitlon/math",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Precision-safe arithmetic utilities for gitlon",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"keywords": [
|
|
21
|
+
"gitlon",
|
|
22
|
+
"math",
|
|
23
|
+
"decimal",
|
|
24
|
+
"precision"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "Long",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "vite build",
|
|
33
|
+
"dev": "vite build --watch",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"clean": "rm -rf dist"
|
|
36
|
+
}
|
|
37
|
+
}
|