not-so-float 0.0.1 → 1.0.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 +16 -18
- package/README.md +213 -0
- package/dist/index.cjs +776 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +121 -2
- package/dist/index.d.ts +121 -2
- package/dist/index.js +696 -4
- package/dist/index.js.map +1 -1
- package/package.json +11 -5
package/LICENSE
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
obtaining a copy of the software and accompanying documentation covered by
|
|
5
|
-
this license (the "Software") to use, reproduce, display, distribute,
|
|
6
|
-
execute, and transmit the Software, and to prepare derivative works of the
|
|
7
|
-
Software, and to permit third-parties to whom the Software is furnished to
|
|
8
|
-
do so, all subject to the following:
|
|
3
|
+
Copyright (c) 2025 Victor Poughon
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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.
|
|
16
14
|
|
|
17
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
FITNESS FOR A PARTICULAR PURPOSE
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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,213 @@
|
|
|
1
|
+
# Not So Float
|
|
2
|
+
|
|
3
|
+
**Not So Float** is a pure TypeScript library that implements **Interval Union
|
|
4
|
+
Arithmetic** over IEEE 754 double precision floats (JS numbers).
|
|
5
|
+
|
|
6
|
+
```typescript
|
|
7
|
+
import * as nsf from "not-so-float";
|
|
8
|
+
|
|
9
|
+
const X = nsf.union([nsf.inter(-10, -1), nsf.inter(100, 120)]);
|
|
10
|
+
const Y = nsf.union([nsf.inter(-10, 2)]);
|
|
11
|
+
const Q = nsf.udiv(X, Y);
|
|
12
|
+
|
|
13
|
+
console.log(`(${X.toString()}) / ${Y.toString()} = ${Q.toString()}`);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
([-10, -1] U [100, 120]) / [-10, 2] = [-Infinity, -0.49999999999999994] U [0.09999999999999999, Infinity]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
References:
|
|
21
|
+
|
|
22
|
+
- [Hickey, T., Ju, Q. and Van Emden, M.H., 2001. Interval arithmetic: From principles to implementation. Journal of the ACM (JACM), 48(5), pp.1038-1068.](https://fab.cba.mit.edu/classes/S62.12/docs/Hickey_interval.pdf)
|
|
23
|
+
- [Schichl, H., Domes, F., Montanher, T. and Kofler, K., 2017. Interval unions. BIT Numerical Mathematics, 57(2), pp.531-556.](https://www.ime.usp.br/~montanhe/unions.pdf)
|
|
24
|
+
|
|
25
|
+
Design goals:
|
|
26
|
+
|
|
27
|
+
- No dependencies
|
|
28
|
+
- Functional (stateless) API
|
|
29
|
+
- Strong focus on simplicity and clarity of implementation
|
|
30
|
+
- No signed zero convention, `-0 === 0` should still be true in the context of interval bounds
|
|
31
|
+
- No empty interval, but empty unions
|
|
32
|
+
- Support division by intervals containing zero without compromise
|
|
33
|
+
|
|
34
|
+
> [!TIP]
|
|
35
|
+
> If you like this open-source project consider [sponsoring me](https://github.com/sponsors/victorpoughon) on GitHub, thank you ❤️
|
|
36
|
+
|
|
37
|
+
## Outward Rounding
|
|
38
|
+
|
|
39
|
+
The main interest of interval arithmetic is the inclusion property. Any interval
|
|
40
|
+
operation result is always guaranteed to contain all possible results obtained
|
|
41
|
+
from the underlying real operation. However floating point operations are
|
|
42
|
+
inexact. So we must use _outward rounding_ when computing interval bounds to
|
|
43
|
+
maintain the inclusion property.
|
|
44
|
+
|
|
45
|
+
Unfortunately JS doesn't allow controling the IEEE 754 rounding mode, and rounds
|
|
46
|
+
to the nearest representable float, like most programming languages. This is a
|
|
47
|
+
computer science tragedy of the first order, because IEEE 754 actually specifies
|
|
48
|
+
rounding modes that would allow clean implementations of interval arithmetic
|
|
49
|
+
natively. Instead, we implement two functions: `next()` and `prev()` using typed
|
|
50
|
+
arrays bit manipulation. These functions return the next and previous
|
|
51
|
+
representable floating point value, exactly like the C function
|
|
52
|
+
[`nextafter()`](https://en.cppreference.com/w/c/numeric/math/nextafter).
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
function next(x: number): number;
|
|
56
|
+
function prev(x: number): number;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
By making sure every interval bound operation rounds outward, we can maintain
|
|
60
|
+
the inclusion property even with numerical precision errors inherent to floating
|
|
61
|
+
point.
|
|
62
|
+
|
|
63
|
+
## Interval
|
|
64
|
+
|
|
65
|
+
An interval is a pair of IEEE 754 double precision numbers called the lower (lo)
|
|
66
|
+
and upper (hi) bounds:
|
|
67
|
+
|
|
68
|
+
$$
|
|
69
|
+
[\text{ lo}, \text{ hi }]
|
|
70
|
+
$$
|
|
71
|
+
|
|
72
|
+
The only restriction is that bounds must always be less or equal: $\text{lo}
|
|
73
|
+
\leq \text{hi}$. The lower bound may be negative infinity, the upper bound may
|
|
74
|
+
be positive infinity. This table describes the set of real numbers that an
|
|
75
|
+
interval represents:
|
|
76
|
+
|
|
77
|
+
<div align="center">
|
|
78
|
+
|
|
79
|
+
| lo | hi | Interval Type | Represents<br> any $x \in \mathbb{R}$ such that |
|
|
80
|
+
| :-------: | :-------: | :-----------: | :---------------------------------------------: |
|
|
81
|
+
| $a$ | $b$ | Real | $a \leq x \leq$ b |
|
|
82
|
+
| $-\infty$ | $b$ | Semi-Infinite | $x \leq b$ |
|
|
83
|
+
| $a$ | $+\infty$ | Semi-Infinite | $a \leq x$ |
|
|
84
|
+
| $-\infty$ | $+\infty$ | Infinite | $x \in \mathbb{R}$ |
|
|
85
|
+
|
|
86
|
+
</div>
|
|
87
|
+
|
|
88
|
+
Where $a$ and $b$ are real IEEE 754 values (not infinite).
|
|
89
|
+
The special case when both bounds are equal is called a _degenerate_ interval.
|
|
90
|
+
|
|
91
|
+
To construct an interval, use the `nsf.inter()` function:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
nsf.inter(a: number, b: number) : Interval
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
For example:
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
> I = nsf.inter(0, 6)
|
|
101
|
+
Interval { lo: 0, hi: 6 }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The interval class member functions:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
class Interval {
|
|
108
|
+
isFull(): boolean;
|
|
109
|
+
isFinite(): boolean;
|
|
110
|
+
isDegenerate(): boolean;
|
|
111
|
+
contains(value: number): boolean;
|
|
112
|
+
superset(other: Interval): boolean;
|
|
113
|
+
subset(other: Interval): boolean;
|
|
114
|
+
toString(numbers?: (x: number) => string): string;
|
|
115
|
+
print(): void;
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Union
|
|
120
|
+
|
|
121
|
+
A union is an ordered disjoint set of intervals:
|
|
122
|
+
|
|
123
|
+
$$
|
|
124
|
+
[a, b] \cup [c, d] \ldots \cup [e, f]
|
|
125
|
+
$$
|
|
126
|
+
|
|
127
|
+
To construct a union, use the `nsf.union()` function:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
nsf.union(intervals: Interval[]) : Union
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Union class member functions:
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
class Union {
|
|
137
|
+
forEach(callback: (x: Interval, index: number) => void): void;
|
|
138
|
+
toString(numbers?: (x: number) => string): string;
|
|
139
|
+
print(): void;
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
For example:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
> const U = nsf.union([nsf.inter(1, 2), nsf.inter(4, 6)])
|
|
147
|
+
> U.toString()
|
|
148
|
+
'[1, 2] U [4, 6]'
|
|
149
|
+
> U.intervals
|
|
150
|
+
[ Interval { lo: 1, hi: 2 }, Interval { lo: 4, hi: 6 } ]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Compare
|
|
154
|
+
|
|
155
|
+
Compare intervals and unions with the following utility functions:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
function overlap(A: Interval | Union, B: Interval | Union): boolean;
|
|
159
|
+
function disjoint(A: Interval | Union, B: Interval | Union): boolean;
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Arithmetic
|
|
163
|
+
|
|
164
|
+
<div align="center">
|
|
165
|
+
|
|
166
|
+
| Operation | API function |
|
|
167
|
+
| ------------------------------------------: | :----------------- |
|
|
168
|
+
| Addition | `nsf.add(A, B)` |
|
|
169
|
+
| Unary negation | `nsf.neg(A)` |
|
|
170
|
+
| Subtraction | `nsf.sub(A, B)` |
|
|
171
|
+
| Multiplication | `nsf.mul(A, B)` |
|
|
172
|
+
| Division | `nsf.div(A, B)` |
|
|
173
|
+
| Exponentiation<br>(integer exponent) | `nsf.powInt(A, n)` |
|
|
174
|
+
| Exponentiation<br>(interval/union exponent) | `nsf.pow(A, B)` |
|
|
175
|
+
|
|
176
|
+
</div>
|
|
177
|
+
|
|
178
|
+
> [!IMPORTANT]
|
|
179
|
+
> Note that **every operation returns a union**. This is for consistency, because
|
|
180
|
+
> while some operations (add, sub, mul) between intervals can return a single
|
|
181
|
+
> interval, others (division, exponentiation, log, sqrt) can return zero, one or
|
|
182
|
+
> two disjoint intervals even with interval inputs.
|
|
183
|
+
|
|
184
|
+
## Unary Functions
|
|
185
|
+
|
|
186
|
+
<div align="center">
|
|
187
|
+
|
|
188
|
+
| Operation | API function |
|
|
189
|
+
| ----------------: | :------------ |
|
|
190
|
+
| Absolute value | `nsf.abs(A)` |
|
|
191
|
+
| Natural logarithm | `nsf.log(A)` |
|
|
192
|
+
| Exponential | `nsf.exp(A)` |
|
|
193
|
+
| Square root | `nsf.sqrt(A)` |
|
|
194
|
+
| Cosine | `nsf.cos(A)` |
|
|
195
|
+
| Sine | `nsf.sin(A)` |
|
|
196
|
+
| Tangent | `nsf.tan(A)` |
|
|
197
|
+
|
|
198
|
+
</div>
|
|
199
|
+
|
|
200
|
+
## Binary Functions
|
|
201
|
+
|
|
202
|
+
<div align="center">
|
|
203
|
+
|
|
204
|
+
| Operation | API function |
|
|
205
|
+
| --------: | :-------------- |
|
|
206
|
+
| Minimum | `nsf.min(A, B)` |
|
|
207
|
+
| Maximum | `nsf.max(A, B)` |
|
|
208
|
+
|
|
209
|
+
</div>
|
|
210
|
+
|
|
211
|
+
## Future improvements
|
|
212
|
+
|
|
213
|
+
* Support multiple rounding modes
|