functionalscript 0.0.513 → 0.0.514

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.513",
3
+ "version": "0.0.514",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -4,6 +4,8 @@ const { todo } = require('../../dev/module.f.cjs')
4
4
 
5
5
  /** @typedef {readonly[bigint,number]} BigFloat */
6
6
 
7
+ /** @typedef {readonly[BigFloat,bigint]} BigFloatWithRemainder */
8
+
7
9
  const twoPow53 = 0b0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n
8
10
  const twoPow54 = 0b0100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n
9
11
 
@@ -44,20 +46,25 @@ const pow = base => exp => base ** BigInt(exp)
44
46
 
45
47
  const pow5 = pow(5n)
46
48
 
47
- /** @type {(div: BigFloat) => (p: bigint) => BigFloat} */
48
- const divide = ([m, e]) => div => {
49
+ /** @type {(b: BigFloat) => (mul: bigint) => BigFloat} */
50
+ const multiply = ([m, e]) => mul => [m * mul, e]
51
+
52
+ /** @type {(b: BigFloat) => (div: bigint) => BigFloatWithRemainder} */
53
+ const divide = ([m, e]) => div => [[m / div, e], m % div]
54
+
55
+ /** @type {(b: BigFloatWithRemainder) => BigFloat} */
56
+ const round53 = ([[m, e], r]) => {
49
57
  const mabs = abs(m)
50
- const q = mabs / div
51
58
  const s = BigInt(sign(m))
52
- const [q53, e53] = decreaseMantissa([q, e])(twoPow54)
53
- const r = q53 & 1n
54
- const q52 = q53 >> 1n
55
- const e52 = e53 + 1
56
- if (r === 1n && mabs === q * div && q === q53 >> BigInt(e - e53)) {
57
- const odd = q52 & 1n
58
- return [s * (q52 + odd), e52]
59
+ const [m54, e54] = decreaseMantissa([mabs, e])(twoPow54)
60
+ const o54 = m54 & 1n
61
+ const m53 = m54 >> 1n
62
+ const e53 = e54 + 1
63
+ if (o54 === 1n && r === 0n && mabs === m54 >> BigInt(e - e54)) {
64
+ const odd = m53 & 1n
65
+ return multiply([m53 + odd, e53])(s)
59
66
  }
60
- return [s * (q52 + r), e52]
67
+ return multiply([m53 + o54, e53])(s)
61
68
  }
62
69
 
63
70
  /** @type {(dec: BigFloat) => BigFloat} */
@@ -68,11 +75,16 @@ const decToBin = dec => {
68
75
  if (dec[1] >= 0) {
69
76
  /** @type {BigFloat} */
70
77
  const bin = [dec[0] * pow5(dec[1]), dec[1]]
71
- return divide(increaseMantissa(bin)(twoPow53))(1n)
78
+ const inc = increaseMantissa(bin)(twoPow53)
79
+ return round53([inc, 0n])
72
80
  }
73
81
  const p = pow5(-dec[1])
74
82
  const [m, e] = increaseMantissa(dec)(p * twoPow53)
75
- return divide([m, e])(p)
83
+ const mAbs = abs(m)
84
+ const s = BigInt(sign(m))
85
+ const qr = divide([mAbs, e])(p)
86
+ const r53 = round53(qr)
87
+ return multiply(r53)(s)
76
88
  }
77
89
 
78
90
  module.exports = {