@xort/cga 0.1.0 → 0.1.1
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/THIRD_PARTY_NOTICES.md +29 -0
- package/build/@xort_cga.js +14908 -8876
- package/build/cga.d.ts +1266 -448
- package/build/cga.esm.js +14891 -8895
- package/build/shaders/cga.glsl +204 -0
- package/build/shaders/common.glsl +36 -0
- package/build/shaders/noise/GLSL-Noise.md +1108 -0
- package/build/shaders/noise/LICENSE +20 -0
- package/build/shaders/noise/cellular2D.glsl +69 -0
- package/build/shaders/noise/cellular2x2.glsl +64 -0
- package/build/shaders/noise/cellular2x2x2.glsl +88 -0
- package/build/shaders/noise/cellular3D.glsl +187 -0
- package/build/shaders/noise/classicnoise2D.glsl +113 -0
- package/build/shaders/noise/classicnoise3D.glsl +177 -0
- package/build/shaders/noise/classicnoise4D.glsl +302 -0
- package/build/shaders/noise/noise2D.glsl +71 -0
- package/build/shaders/noise/noise3D.glsl +103 -0
- package/build/shaders/noise/noise3Dgrad.glsl +111 -0
- package/build/shaders/noise/noise4D.glsl +129 -0
- package/build/shaders/noise/psrdnoise2D.glsl +463 -0
- package/dist/THIRD_PARTY_NOTICES.md +29 -0
- package/dist/index.cjs +14908 -8876
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1266 -448
- package/dist/index.d.ts +1266 -448
- package/dist/index.js +14891 -8895
- package/dist/index.js.map +1 -1
- package/dist/shaders.cjs +21 -0
- package/dist/shaders.d.cts +20 -0
- package/dist/shaders.d.ts +20 -0
- package/dist/shaders.js +20 -0
- package/dist/three.cjs +3034 -0
- package/dist/three.cjs.map +1 -0
- package/dist/three.d.cts +308 -0
- package/dist/three.d.ts +308 -0
- package/dist/three.js +3007 -0
- package/dist/three.js.map +1 -0
- package/package.json +42 -5
- package/readme.md +68 -119
package/dist/three.js
ADDED
|
@@ -0,0 +1,3007 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import { AmbientLight, BoxHelper, BufferGeometry, CircleGeometry, Color, CubeTexture, DirectionalLight, Float32BufferAttribute, Mesh, MeshBasicMaterial, MeshNormalMaterial, Object3D, PMREMGenerator, PerspectiveCamera, Raycaster, SRGBColorSpace, Scene, Texture, Vector2, Vector3, WebGLRenderer } from "three";
|
|
3
|
+
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
|
|
4
|
+
import { TransformControls } from "three/addons/controls/TransformControls.js";
|
|
5
|
+
import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js";
|
|
6
|
+
import { RenderPass } from "three/addons/postprocessing/RenderPass.js";
|
|
7
|
+
import { OutputPass } from "three/addons/postprocessing/OutputPass.js";
|
|
8
|
+
import { UnrealBloomPass } from "three/addons/postprocessing/UnrealBloomPass.js";
|
|
9
|
+
// src/render/eventhandler.ts
|
|
10
|
+
var EventHandler = class {
|
|
11
|
+
listeners = /* @__PURE__ */ new Map();
|
|
12
|
+
on(type, listener) {
|
|
13
|
+
let set = this.listeners.get(type);
|
|
14
|
+
if (!set) this.listeners.set(type, set = /* @__PURE__ */ new Set());
|
|
15
|
+
set.add(listener);
|
|
16
|
+
return () => this.off(type, listener);
|
|
17
|
+
}
|
|
18
|
+
off(type, listener) {
|
|
19
|
+
const set = this.listeners.get(type);
|
|
20
|
+
set?.delete(listener);
|
|
21
|
+
if (!set?.size) this.listeners.delete(type);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
once(type, listener) {
|
|
25
|
+
const off = this.on(type, (data) => {
|
|
26
|
+
off();
|
|
27
|
+
listener(data);
|
|
28
|
+
});
|
|
29
|
+
return off;
|
|
30
|
+
}
|
|
31
|
+
fire(type, data) {
|
|
32
|
+
for (const listener of [...this.listeners.get(type) ?? []]) listener(data);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
removeAllListeners() {
|
|
36
|
+
this.listeners.clear();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// src/glview.ts
|
|
40
|
+
var GLView = class extends EventHandler {
|
|
41
|
+
renderer;
|
|
42
|
+
container;
|
|
43
|
+
effectComposer;
|
|
44
|
+
_controls;
|
|
45
|
+
_transfromControl;
|
|
46
|
+
_scene;
|
|
47
|
+
_camera;
|
|
48
|
+
_modelScene = new Object3D();
|
|
49
|
+
_helperScene = new Object3D();
|
|
50
|
+
outputPass = new OutputPass();
|
|
51
|
+
renderPass;
|
|
52
|
+
observer;
|
|
53
|
+
abort = new AbortController();
|
|
54
|
+
pmrem;
|
|
55
|
+
environment;
|
|
56
|
+
selected = /* @__PURE__ */ new Map();
|
|
57
|
+
disposed = false;
|
|
58
|
+
postProcessing = false;
|
|
59
|
+
updates = /* @__PURE__ */ new Set();
|
|
60
|
+
allowSelected = true;
|
|
61
|
+
pointer = new Vector2();
|
|
62
|
+
constructor(settings = {}) {
|
|
63
|
+
super();
|
|
64
|
+
if (typeof document === "undefined") throw new Error("GLView requires a browser document");
|
|
65
|
+
this.container = settings.container ?? document.body;
|
|
66
|
+
this.renderer = settings.renderer ?? new WebGLRenderer({ antialias: true });
|
|
67
|
+
this.renderer.outputColorSpace = SRGBColorSpace;
|
|
68
|
+
this.renderer.setPixelRatio(settings.pixelRatio ?? Math.min(devicePixelRatio, 2));
|
|
69
|
+
this.container.appendChild(this.renderer.domElement);
|
|
70
|
+
this._scene = settings.scene ?? new Scene();
|
|
71
|
+
this._camera = settings.camera ?? new PerspectiveCamera(45, 1, .1, 2e4);
|
|
72
|
+
if (!settings.camera) this._camera.position.set(16, 4, 30);
|
|
73
|
+
this._scene.add(this._modelScene, this._helperScene);
|
|
74
|
+
this.clearColor = settings.clearColor ?? 0xaaeeff;
|
|
75
|
+
this._controls = new OrbitControls(this._camera, this.domElement);
|
|
76
|
+
this._transfromControl = new TransformControls(this._camera, this.domElement);
|
|
77
|
+
this._helperScene.add(this._transfromControl.getHelper());
|
|
78
|
+
this._transfromControl.addEventListener("dragging-changed", (event) => this._controls.enabled = !event.value);
|
|
79
|
+
const light = new DirectionalLight(0xffffff, 3);
|
|
80
|
+
light.position.set(1, 1, 1);
|
|
81
|
+
this._helperScene.add(light, new AmbientLight(0xffffff, .5));
|
|
82
|
+
this.effectComposer = new EffectComposer(this.renderer);
|
|
83
|
+
this.renderPass = new RenderPass(this.scene, this.camera);
|
|
84
|
+
this.effectComposer.addPass(this.renderPass);
|
|
85
|
+
this.effectComposer.addPass(this.outputPass);
|
|
86
|
+
this.postProcessing = settings.enablePostProcessing ?? settings._enablePostProcessing ?? false;
|
|
87
|
+
this.pmrem = new PMREMGenerator(this.renderer);
|
|
88
|
+
this.observer = new ResizeObserver(() => this.onResize());
|
|
89
|
+
this.observer.observe(this.container);
|
|
90
|
+
this.domElement.addEventListener("pointerdown", (event) => this.onMousedown(event), { signal: this.abort.signal });
|
|
91
|
+
this.onResize();
|
|
92
|
+
}
|
|
93
|
+
get scene() {
|
|
94
|
+
return this._scene;
|
|
95
|
+
}
|
|
96
|
+
set scene(value) {
|
|
97
|
+
this._scene.remove(this._modelScene, this._helperScene);
|
|
98
|
+
this._scene = value;
|
|
99
|
+
value.add(this._modelScene, this._helperScene);
|
|
100
|
+
this.renderPass.scene = value;
|
|
101
|
+
}
|
|
102
|
+
get camera() {
|
|
103
|
+
return this._camera;
|
|
104
|
+
}
|
|
105
|
+
set camera(value) {
|
|
106
|
+
this._camera = value;
|
|
107
|
+
this._controls.object = value;
|
|
108
|
+
this._transfromControl.camera = value;
|
|
109
|
+
this.renderPass.camera = value;
|
|
110
|
+
this.onResize();
|
|
111
|
+
}
|
|
112
|
+
get modelScene() {
|
|
113
|
+
return this._modelScene;
|
|
114
|
+
}
|
|
115
|
+
set modelScene(value) {
|
|
116
|
+
this._scene.remove(this._modelScene);
|
|
117
|
+
this._modelScene = value;
|
|
118
|
+
this._scene.add(value);
|
|
119
|
+
}
|
|
120
|
+
get helperScene() {
|
|
121
|
+
return this._helperScene;
|
|
122
|
+
}
|
|
123
|
+
set helperScene(value) {
|
|
124
|
+
this._scene.remove(this._helperScene);
|
|
125
|
+
this._helperScene = value;
|
|
126
|
+
this._scene.add(value);
|
|
127
|
+
value.add(this._transfromControl.getHelper());
|
|
128
|
+
}
|
|
129
|
+
get domElement() {
|
|
130
|
+
return this.renderer.domElement;
|
|
131
|
+
}
|
|
132
|
+
get width() {
|
|
133
|
+
return this.domElement.clientWidth;
|
|
134
|
+
}
|
|
135
|
+
get height() {
|
|
136
|
+
return this.domElement.clientHeight;
|
|
137
|
+
}
|
|
138
|
+
set clearColor(value) {
|
|
139
|
+
this.renderer.setClearColor(new Color(value));
|
|
140
|
+
}
|
|
141
|
+
set toneExposure(value) {
|
|
142
|
+
this.renderer.toneMappingExposure = value;
|
|
143
|
+
}
|
|
144
|
+
get enablePostProcessing() {
|
|
145
|
+
return this.postProcessing;
|
|
146
|
+
}
|
|
147
|
+
set enablePostProcessing(value) {
|
|
148
|
+
this.postProcessing = value;
|
|
149
|
+
}
|
|
150
|
+
get selecteds() {
|
|
151
|
+
return [...this.selected.keys()];
|
|
152
|
+
}
|
|
153
|
+
setTransformMode(mode) {
|
|
154
|
+
this._transfromControl.setMode(mode);
|
|
155
|
+
}
|
|
156
|
+
setTransfromMode(mode) {
|
|
157
|
+
this.setTransformMode(mode);
|
|
158
|
+
}
|
|
159
|
+
attach(model) {
|
|
160
|
+
this._transfromControl.attach(model);
|
|
161
|
+
}
|
|
162
|
+
detach(_model) {
|
|
163
|
+
this._transfromControl.detach();
|
|
164
|
+
this._controls.enabled = true;
|
|
165
|
+
}
|
|
166
|
+
pmrenvMap(texture, type = 1) {
|
|
167
|
+
let target;
|
|
168
|
+
if (type === 0 && texture instanceof Scene) target = this.pmrem.fromScene(texture);
|
|
169
|
+
else if (type === 2 && texture instanceof CubeTexture) target = this.pmrem.fromCubemap(texture);
|
|
170
|
+
else if (texture instanceof Texture) target = this.pmrem.fromEquirectangular(texture);
|
|
171
|
+
else throw new TypeError("Invalid PMREM source");
|
|
172
|
+
this.environment?.dispose();
|
|
173
|
+
this.environment = target;
|
|
174
|
+
this.scene.environment = target.texture;
|
|
175
|
+
return target.texture;
|
|
176
|
+
}
|
|
177
|
+
addPass(pass) {
|
|
178
|
+
this.effectComposer.insertPass(pass, this.effectComposer.passes.length - 1);
|
|
179
|
+
return pass;
|
|
180
|
+
}
|
|
181
|
+
addPassEx(name, params = {
|
|
182
|
+
threshold: .01,
|
|
183
|
+
strength: .1,
|
|
184
|
+
radius: .4
|
|
185
|
+
}) {
|
|
186
|
+
if (name !== "Bloom") throw new RangeError("Unknown postprocessing pass");
|
|
187
|
+
const pass = new UnrealBloomPass(new Vector2(this.width, this.height), params.strength, params.radius, params.threshold);
|
|
188
|
+
return this.addPass(pass);
|
|
189
|
+
}
|
|
190
|
+
add(...objects) {
|
|
191
|
+
this.modelScene.add(...objects);
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
remove(...objects) {
|
|
195
|
+
this.unselect(objects);
|
|
196
|
+
this.modelScene.remove(...objects);
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
size(width, height) {
|
|
200
|
+
if (![width, height].every((v) => Number.isFinite(v) && v > 0)) throw new RangeError("Viewport size must be positive");
|
|
201
|
+
this.renderer.setSize(width, height);
|
|
202
|
+
this._camera.aspect = width / height;
|
|
203
|
+
this._camera.updateProjectionMatrix();
|
|
204
|
+
this.effectComposer.setSize(width, height);
|
|
205
|
+
}
|
|
206
|
+
onResize() {
|
|
207
|
+
this.size(Math.max(1, this.container.clientWidth), Math.max(1, this.container.clientHeight));
|
|
208
|
+
}
|
|
209
|
+
addUpdates(...callbacks) {
|
|
210
|
+
callbacks.forEach((callback) => this.updates.add(callback));
|
|
211
|
+
}
|
|
212
|
+
removeUpdates(...callbacks) {
|
|
213
|
+
callbacks.forEach((callback) => this.updates.delete(callback));
|
|
214
|
+
}
|
|
215
|
+
onMousedown(event) {
|
|
216
|
+
if (event.button !== 0 || !this.allowSelected || this._transfromControl.dragging) return;
|
|
217
|
+
this.pointer.set(event.clientX, event.clientY);
|
|
218
|
+
this.selectMD(event.ctrlKey || event.metaKey);
|
|
219
|
+
}
|
|
220
|
+
project(point) {
|
|
221
|
+
return point.clone().project(this.camera);
|
|
222
|
+
}
|
|
223
|
+
unproject(point) {
|
|
224
|
+
return point.clone().unproject(this.camera);
|
|
225
|
+
}
|
|
226
|
+
getScreenPosition(point) {
|
|
227
|
+
const p = this.project(point), rect = this.domElement.getBoundingClientRect();
|
|
228
|
+
return new Vector3(rect.left + (p.x + 1) * rect.width / 2, rect.top + (1 - p.y) * rect.height / 2, p.z);
|
|
229
|
+
}
|
|
230
|
+
getSelect(point, isMousePos = true, first = true) {
|
|
231
|
+
const rect = this.domElement.getBoundingClientRect(), ndc = isMousePos ? new Vector2((point.x - rect.left) / rect.width * 2 - 1, -(point.y - rect.top) / rect.height * 2 + 1) : point;
|
|
232
|
+
const ray = new Raycaster();
|
|
233
|
+
this.scene.updateMatrixWorld(true);
|
|
234
|
+
ray.setFromCamera(ndc, this.camera);
|
|
235
|
+
const hits = ray.intersectObjects(this.modelScene.children, true).filter((hit) => {
|
|
236
|
+
let obj = hit.object;
|
|
237
|
+
while (obj) {
|
|
238
|
+
if (!obj.visible) return false;
|
|
239
|
+
obj = obj.parent;
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
});
|
|
243
|
+
return first ? hits[0] : hits;
|
|
244
|
+
}
|
|
245
|
+
selectMD(multiple = false) {
|
|
246
|
+
const hit = this.getSelect(this.pointer);
|
|
247
|
+
this.selectModels(hit ? [hit.object] : [], multiple);
|
|
248
|
+
return this.selecteds;
|
|
249
|
+
}
|
|
250
|
+
selectModels(models, multiple = false) {
|
|
251
|
+
if (!multiple) this.unselect();
|
|
252
|
+
this.select(models);
|
|
253
|
+
}
|
|
254
|
+
select(models = []) {
|
|
255
|
+
if (!this.allowSelected) return;
|
|
256
|
+
for (const model of models) if (!this.selected.has(model)) {
|
|
257
|
+
const helper = new BoxHelper(model, 0xaa3366);
|
|
258
|
+
this.selected.set(model, helper);
|
|
259
|
+
this.helperScene.add(helper);
|
|
260
|
+
}
|
|
261
|
+
this.fire("selectionChanged", this.selecteds);
|
|
262
|
+
}
|
|
263
|
+
unselect(models = this.selecteds) {
|
|
264
|
+
for (const model of models) {
|
|
265
|
+
const helper = this.selected.get(model);
|
|
266
|
+
if (helper) {
|
|
267
|
+
helper.removeFromParent();
|
|
268
|
+
helper.dispose();
|
|
269
|
+
this.selected.delete(model);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
this.fire("selectionChanged", this.selecteds);
|
|
273
|
+
}
|
|
274
|
+
inRendererArea(point) {
|
|
275
|
+
const p = this.project(point);
|
|
276
|
+
return Math.abs(p.x) <= 1 && Math.abs(p.y) <= 1 && Math.abs(p.z) <= 1;
|
|
277
|
+
}
|
|
278
|
+
render() {
|
|
279
|
+
if (this.disposed) return;
|
|
280
|
+
this._controls.update();
|
|
281
|
+
this.selected.forEach((helper) => helper.update());
|
|
282
|
+
if (this.postProcessing) this.effectComposer.render();
|
|
283
|
+
else this.renderer.render(this.scene, this.camera);
|
|
284
|
+
}
|
|
285
|
+
run() {
|
|
286
|
+
if (this.disposed) throw new Error("GLView has been disposed");
|
|
287
|
+
this.renderer.setAnimationLoop((time) => {
|
|
288
|
+
this.updates.forEach((callback) => callback(time));
|
|
289
|
+
this.render();
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
stop() {
|
|
293
|
+
this.renderer.setAnimationLoop(null);
|
|
294
|
+
}
|
|
295
|
+
hide(object) {
|
|
296
|
+
object.visible = false;
|
|
297
|
+
}
|
|
298
|
+
hideOther(object) {
|
|
299
|
+
this.modelScene.traverse((node) => node.visible = false);
|
|
300
|
+
object.traverse((node) => node.visible = true);
|
|
301
|
+
for (let node = object; node; node = node.parent) node.visible = true;
|
|
302
|
+
}
|
|
303
|
+
showAll() {
|
|
304
|
+
this.modelScene.traverse((node) => node.visible = true);
|
|
305
|
+
}
|
|
306
|
+
dispose() {
|
|
307
|
+
if (this.disposed) return;
|
|
308
|
+
this.stop();
|
|
309
|
+
this.abort.abort();
|
|
310
|
+
this.observer.disconnect();
|
|
311
|
+
this.unselect();
|
|
312
|
+
this.updates.clear();
|
|
313
|
+
this._controls.dispose();
|
|
314
|
+
this._transfromControl.dispose();
|
|
315
|
+
this.effectComposer.passes.forEach((pass) => pass.dispose());
|
|
316
|
+
this.effectComposer.dispose();
|
|
317
|
+
this.environment?.dispose();
|
|
318
|
+
this.pmrem.dispose();
|
|
319
|
+
this.renderer.dispose();
|
|
320
|
+
this.domElement.remove();
|
|
321
|
+
this.removeAllListeners();
|
|
322
|
+
this.disposed = true;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
// src/effect/breath-light.ts
|
|
326
|
+
var BreathLight = class extends Object3D {
|
|
327
|
+
geometry = new CircleGeometry(1, 32);
|
|
328
|
+
rings = [];
|
|
329
|
+
constructor(color = 2071759) {
|
|
330
|
+
super();
|
|
331
|
+
this.geometry.rotateX(-Math.PI / 2);
|
|
332
|
+
for (let i = 0; i < 3; i++) {
|
|
333
|
+
const ring = new Mesh(this.geometry, new MeshBasicMaterial({
|
|
334
|
+
color,
|
|
335
|
+
transparent: true,
|
|
336
|
+
opacity: 1 - i / 3,
|
|
337
|
+
depthWrite: false
|
|
338
|
+
}));
|
|
339
|
+
this.rings.push(ring);
|
|
340
|
+
this.add(ring);
|
|
341
|
+
}
|
|
342
|
+
this.update(0);
|
|
343
|
+
}
|
|
344
|
+
update(time) {
|
|
345
|
+
this.rings.forEach((ring, i) => {
|
|
346
|
+
const phase = ((time / 2e3 + i / 3) % 1 + 1) % 1;
|
|
347
|
+
ring.scale.setScalar(1 + phase * 3);
|
|
348
|
+
ring.material.opacity = 1 - phase;
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
dispose() {
|
|
352
|
+
this.geometry.dispose();
|
|
353
|
+
this.rings.forEach((ring) => ring.material.dispose());
|
|
354
|
+
this.clear();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
// src/diamondMaterialShader.ts
|
|
358
|
+
var DiamondShader = {
|
|
359
|
+
defines: { RAY_BOUNCES: 5 },
|
|
360
|
+
vertexShader: [
|
|
361
|
+
"varying vec2 vUv;",
|
|
362
|
+
"varying vec3 Normal;",
|
|
363
|
+
"varying vec3 worldNormal;",
|
|
364
|
+
"varying vec3 vecPos;",
|
|
365
|
+
"varying vec3 viewPos;",
|
|
366
|
+
"void main() {",
|
|
367
|
+
"vUv = uv;",
|
|
368
|
+
"Normal = normal;",
|
|
369
|
+
"worldNormal = (modelMatrix * vec4(normal,0.0)).xyz;",
|
|
370
|
+
"vecPos = (modelMatrix * vec4(position, 1.0 )).xyz;",
|
|
371
|
+
"viewPos = (modelViewMatrix * vec4(position, 1.0 )).xyz;",
|
|
372
|
+
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
|
|
373
|
+
"}"
|
|
374
|
+
].join("\n"),
|
|
375
|
+
fragmentShader: [
|
|
376
|
+
"varying vec2 vUv;",
|
|
377
|
+
"varying vec3 Normal;",
|
|
378
|
+
"varying vec3 worldNormal;",
|
|
379
|
+
"varying vec3 vecPos;",
|
|
380
|
+
"varying vec3 viewPos;",
|
|
381
|
+
"uniform samplerCube tCubeMapNormals;",
|
|
382
|
+
"uniform samplerCube envMap;",
|
|
383
|
+
"uniform samplerCube envRefractionMap;",
|
|
384
|
+
"uniform sampler2D sphereMap;",
|
|
385
|
+
"uniform float envMapIntensity;",
|
|
386
|
+
"uniform float tanAngleSqCone;",
|
|
387
|
+
"uniform float coneHeight;",
|
|
388
|
+
"uniform int maxBounces;",
|
|
389
|
+
"uniform mat4 modelMatrix;",
|
|
390
|
+
"uniform mat4 invModelMatrix;",
|
|
391
|
+
"uniform float n2;",
|
|
392
|
+
"uniform float radius;",
|
|
393
|
+
"uniform bool bDebugBounces;",
|
|
394
|
+
"uniform float rIndexDelta;",
|
|
395
|
+
"uniform float normalOffset;",
|
|
396
|
+
"uniform float squashFactor;",
|
|
397
|
+
"uniform float distanceOffset;",
|
|
398
|
+
"uniform float geometryFactor;",
|
|
399
|
+
"uniform vec3 Absorbption;",
|
|
400
|
+
"uniform vec3 colorCorrection;",
|
|
401
|
+
"uniform vec3 boostFactors;",
|
|
402
|
+
"uniform vec3 centreOffset;",
|
|
403
|
+
"vec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {",
|
|
404
|
+
"float dotNV = abs( dot( normal, viewDir ) );",
|
|
405
|
+
"const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );",
|
|
406
|
+
"const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );",
|
|
407
|
+
"vec4 r = roughness * c0 + c1;",
|
|
408
|
+
"float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;",
|
|
409
|
+
"vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;",
|
|
410
|
+
"return specularColor * AB.x + AB.y;",
|
|
411
|
+
"}",
|
|
412
|
+
"vec4 SampleSpecularReflection(vec4 specularColor, vec3 direction ) {",
|
|
413
|
+
"direction.x *= -1.0;",
|
|
414
|
+
"direction.z *= -1.0;",
|
|
415
|
+
"vec3 tempDir = normalize(vec3(0., 0., 1.) + direction);",
|
|
416
|
+
"vec4 sampleColorRGB = envMapIntensity * textureCube( envMap, direction );",
|
|
417
|
+
"vec4 sampleColorRefraction = envMapIntensity * ( texture2D( sphereMap, tempDir.xy * 0.5 + 0.5 ) );",
|
|
418
|
+
"vec3 toneMappedColor = pow(sampleColorRGB.rgb,vec3(1./1.));",
|
|
419
|
+
"return vec4(toneMappedColor, 1.0);",
|
|
420
|
+
"}",
|
|
421
|
+
"vec4 SampleSpecularContribution(vec4 specularColor, vec3 direction ) {",
|
|
422
|
+
"direction = normalize(direction);",
|
|
423
|
+
"direction.x *= -1.0;",
|
|
424
|
+
"direction.z *= -1.0;",
|
|
425
|
+
"vec4 sampleColorRGB = envMapIntensity * textureCube( envMap, direction );",
|
|
426
|
+
"vec3 tempDir = normalize(vec3(0., 0., 1.) + direction);",
|
|
427
|
+
"float m = 2.8284271247461903 * sqrt( direction.z+1.0 );",
|
|
428
|
+
"vec4 sampleColorRefraction = envMapIntensity * texture2D( sphereMap, clamp(direction.xy / m + 0.45, vec2(0.), vec2(1.)) );",
|
|
429
|
+
"vec3 toneMappedColor = pow(sampleColorRGB.rgb,vec3(1./1.));",
|
|
430
|
+
"return vec4(toneMappedColor, 1.0);",
|
|
431
|
+
"}",
|
|
432
|
+
"vec3 intersectSphere(vec3 origin, vec3 direction) {",
|
|
433
|
+
"origin -= centreOffset;",
|
|
434
|
+
"direction.y /= squashFactor;",
|
|
435
|
+
"float A = dot(direction, direction);",
|
|
436
|
+
"float B = 2.0*dot(origin, direction);",
|
|
437
|
+
"float C = dot(origin, origin) - radius * radius;",
|
|
438
|
+
"float disc = B*B - 4.0 * A * C;",
|
|
439
|
+
"if(disc > 0.0)",
|
|
440
|
+
"{",
|
|
441
|
+
"disc = sqrt(disc);",
|
|
442
|
+
"float t1 = (-B + disc)*geometryFactor/A;",
|
|
443
|
+
"float t2 = (-B - disc)*geometryFactor/A;",
|
|
444
|
+
"float t = (t1 > t2) ? t1 : t2;",
|
|
445
|
+
"direction.y *= squashFactor;",
|
|
446
|
+
"return vec3(origin + centreOffset + direction * t);",
|
|
447
|
+
"}",
|
|
448
|
+
"return vec3(0.0);",
|
|
449
|
+
"}",
|
|
450
|
+
"vec3 debugBounces(int count) {",
|
|
451
|
+
"vec3 color = vec3(1.,1.,1.);",
|
|
452
|
+
"if(count == 1)",
|
|
453
|
+
"color = vec3(0.0,1.0,0.0);",
|
|
454
|
+
"else if(count == 2)",
|
|
455
|
+
"color = vec3(0.0,0.0,1.0);",
|
|
456
|
+
"else if(count == 3)",
|
|
457
|
+
"color = vec3(1.0,1.0,0.0);",
|
|
458
|
+
"else if(count == 4)",
|
|
459
|
+
"color = vec3(0.0,1.0,1.0);",
|
|
460
|
+
"else",
|
|
461
|
+
"color = vec3(0.0,1.0,0.0);",
|
|
462
|
+
"if(count ==0)",
|
|
463
|
+
"color = vec3(1.0,0.0,0.0);",
|
|
464
|
+
"return color;",
|
|
465
|
+
"}",
|
|
466
|
+
"vec3 traceRay(vec3 origin, vec3 direction, vec3 normal) {",
|
|
467
|
+
"vec3 outColor = vec3(0.0);",
|
|
468
|
+
"// Reflect/Refract ray entering the diamond",
|
|
469
|
+
"const float n1 = 1.0;",
|
|
470
|
+
"const float epsilon = 1e-4;",
|
|
471
|
+
"float f0 = (2.4- n1)/(2.4 + n1);",
|
|
472
|
+
"f0 *= f0;",
|
|
473
|
+
"vec3 attenuationFactor = vec3(1.0);",
|
|
474
|
+
"vec3 newDirection = refract(direction, normal, n1/n2);",
|
|
475
|
+
"vec3 reflectedDirection = reflect(direction, normal);",
|
|
476
|
+
"vec3 brdfReflected = BRDF_Specular_GGX_Environment(reflectedDirection, normal, vec3(f0), 0.0);",
|
|
477
|
+
"vec3 brdfRefracted = BRDF_Specular_GGX_Environment(newDirection, -normal, vec3(f0), 0.0);",
|
|
478
|
+
"attenuationFactor *= ( vec3(1.0) - brdfRefracted);",
|
|
479
|
+
"outColor += SampleSpecularReflection(vec4(1.0), reflectedDirection ).rgb * brdfReflected;",
|
|
480
|
+
"int count = 0;",
|
|
481
|
+
"newDirection = (invModelMatrix * vec4(newDirection, 0.0)).xyz;",
|
|
482
|
+
"newDirection = normalize(newDirection);",
|
|
483
|
+
"origin = (invModelMatrix * vec4(origin, 1.0)).xyz;",
|
|
484
|
+
"// ray bounces ",
|
|
485
|
+
"for( int i=0; i<RAY_BOUNCES; i++) { ",
|
|
486
|
+
"vec3 intersectedPos;",
|
|
487
|
+
"intersectedPos = intersectSphere(origin + vec3(epsilon), newDirection);",
|
|
488
|
+
"vec3 dist = intersectedPos - origin;",
|
|
489
|
+
"vec3 d = normalize(intersectedPos - centreOffset);",
|
|
490
|
+
"vec3 mappedNormal = textureCube( tCubeMapNormals, d ).xyz;",
|
|
491
|
+
"mappedNormal = 2. * mappedNormal - 1.;",
|
|
492
|
+
"mappedNormal.y += normalOffset;",
|
|
493
|
+
"mappedNormal = normalize(mappedNormal);",
|
|
494
|
+
"dist = (modelMatrix * vec4(dist, 1.)).xyz;",
|
|
495
|
+
"float r = sqrt(dot(dist, dist));",
|
|
496
|
+
"attenuationFactor *= exp(-r*Absorbption);",
|
|
497
|
+
"// refract the ray at first intersection ",
|
|
498
|
+
"vec3 oldOrigin = origin;",
|
|
499
|
+
"origin = intersectedPos - normalize(intersectedPos - centreOffset) * distanceOffset;",
|
|
500
|
+
"vec3 oldDir = newDirection;",
|
|
501
|
+
"newDirection = refract(newDirection, mappedNormal, n2/n1);",
|
|
502
|
+
"if( dot(newDirection, newDirection) == 0.0) { // Total Internal Reflection. Continue inside the diamond ",
|
|
503
|
+
"newDirection = reflect(oldDir, mappedNormal);",
|
|
504
|
+
"if(i == RAY_BOUNCES-1 ) //If the ray got trapped even after max iterations, simply sample along the outgoing refraction! ",
|
|
505
|
+
"{",
|
|
506
|
+
"vec3 brdfReflected = BRDF_Specular_GGX_Environment(-oldDir, mappedNormal, vec3(f0), 0.0);",
|
|
507
|
+
"vec3 d1 = (modelMatrix * vec4(oldDir, 0.0)).xyz;",
|
|
508
|
+
"outColor += SampleSpecularContribution(vec4(1.0), d1 ).rgb * colorCorrection * attenuationFactor * boostFactors * (vec3(1.0) - brdfReflected);",
|
|
509
|
+
"//outColor = vec3(1.,0.,0.);",
|
|
510
|
+
"//if(d1.y > 0.95) {",
|
|
511
|
+
"//outColor += d1.y * vec3(1.,0.,0) * attenuationFactor * (vec3(1.0) - brdfReflected) * boostFactors;",
|
|
512
|
+
"//}",
|
|
513
|
+
"}",
|
|
514
|
+
"} else { // Add the contribution from outgoing ray, and continue the reflected ray inside the diamond ",
|
|
515
|
+
"vec3 brdfRefracted = BRDF_Specular_GGX_Environment(newDirection, -mappedNormal, vec3(f0), 0.0);",
|
|
516
|
+
"// outgoing(refracted) ray's contribution ",
|
|
517
|
+
"vec3 d1 = (modelMatrix * vec4(newDirection, 0.0)).xyz;",
|
|
518
|
+
"vec3 colorG = SampleSpecularContribution(vec4(1.0), d1 ).rgb * ( vec3(1.0) - brdfRefracted);",
|
|
519
|
+
"vec3 dir1 = refract(oldDir, mappedNormal, (n2+rIndexDelta)/n1);",
|
|
520
|
+
"vec3 dir2 = refract(oldDir, mappedNormal, (n2-rIndexDelta)/n1);",
|
|
521
|
+
"vec3 d2 = (modelMatrix * vec4(dir1, 0.0)).xyz;",
|
|
522
|
+
"vec3 d3 = (modelMatrix * vec4(dir2, 0.0)).xyz;",
|
|
523
|
+
"vec3 colorR = SampleSpecularContribution(vec4(1.0), d2 ).rgb * ( vec3(1.0) - brdfRefracted);",
|
|
524
|
+
"vec3 colorB = SampleSpecularContribution(vec4(1.0), d3 ).rgb * ( vec3(1.0) - brdfRefracted);",
|
|
525
|
+
"outColor += vec3(colorR.r, colorG.g, colorB.b) * colorCorrection * attenuationFactor * boostFactors;",
|
|
526
|
+
"//outColor = oldDir;",
|
|
527
|
+
"//new reflected ray inside the diamond ",
|
|
528
|
+
"newDirection = reflect(oldDir, mappedNormal);",
|
|
529
|
+
"vec3 brdfReflected = BRDF_Specular_GGX_Environment(newDirection, mappedNormal, vec3(f0), 0.0);",
|
|
530
|
+
"attenuationFactor *= brdfReflected * boostFactors;",
|
|
531
|
+
"count++;",
|
|
532
|
+
"}",
|
|
533
|
+
"}",
|
|
534
|
+
"if(false)",
|
|
535
|
+
"outColor = debugBounces(count);",
|
|
536
|
+
"return outColor;",
|
|
537
|
+
"}",
|
|
538
|
+
"void main() {",
|
|
539
|
+
"vec3 normalizedNormal = normalize(worldNormal);",
|
|
540
|
+
"vec3 viewVector = normalize(vecPos - cameraPosition);",
|
|
541
|
+
"vec3 color = traceRay(vecPos, viewVector, normalizedNormal);",
|
|
542
|
+
"gl_FragColor = vec4(color.rgb,1.);",
|
|
543
|
+
"#include <tonemapping_fragment>",
|
|
544
|
+
"#include <colorspace_fragment>",
|
|
545
|
+
"//gl_FragColor = textureCube(tCubeMapNormals, normalize(Normal));",
|
|
546
|
+
"}"
|
|
547
|
+
].join("\n"),
|
|
548
|
+
uniforms: {
|
|
549
|
+
tCubeMapNormals: {
|
|
550
|
+
type: "t",
|
|
551
|
+
value: null
|
|
552
|
+
},
|
|
553
|
+
envMap: {
|
|
554
|
+
type: "t",
|
|
555
|
+
value: null
|
|
556
|
+
},
|
|
557
|
+
envRefractionMap: {
|
|
558
|
+
type: "t",
|
|
559
|
+
value: null
|
|
560
|
+
},
|
|
561
|
+
sphereMap: {
|
|
562
|
+
type: "t",
|
|
563
|
+
value: null
|
|
564
|
+
},
|
|
565
|
+
envMapIntensity: {
|
|
566
|
+
type: "f",
|
|
567
|
+
value: 1
|
|
568
|
+
},
|
|
569
|
+
maxBounces: {
|
|
570
|
+
type: "i",
|
|
571
|
+
value: 1
|
|
572
|
+
},
|
|
573
|
+
tanAngleSqCone: {
|
|
574
|
+
type: "f",
|
|
575
|
+
value: 0
|
|
576
|
+
},
|
|
577
|
+
coneHeight: {
|
|
578
|
+
type: "f",
|
|
579
|
+
value: 0
|
|
580
|
+
},
|
|
581
|
+
bDebugBounces: {
|
|
582
|
+
type: "i",
|
|
583
|
+
value: !1
|
|
584
|
+
},
|
|
585
|
+
rIndexDelta: {
|
|
586
|
+
type: "f",
|
|
587
|
+
value: .012
|
|
588
|
+
},
|
|
589
|
+
n2: {
|
|
590
|
+
type: "f",
|
|
591
|
+
value: 2.4
|
|
592
|
+
},
|
|
593
|
+
radius: {
|
|
594
|
+
type: "f",
|
|
595
|
+
value: 1
|
|
596
|
+
},
|
|
597
|
+
normalOffset: {
|
|
598
|
+
type: "f",
|
|
599
|
+
value: 0
|
|
600
|
+
},
|
|
601
|
+
squashFactor: {
|
|
602
|
+
type: "f",
|
|
603
|
+
value: .98
|
|
604
|
+
},
|
|
605
|
+
distanceOffset: {
|
|
606
|
+
type: "f",
|
|
607
|
+
value: 0
|
|
608
|
+
},
|
|
609
|
+
geometryFactor: {
|
|
610
|
+
type: "f",
|
|
611
|
+
value: .28
|
|
612
|
+
},
|
|
613
|
+
Absorbption: {
|
|
614
|
+
type: "v3",
|
|
615
|
+
value: new THREE.Vector3(0, 0, 0)
|
|
616
|
+
},
|
|
617
|
+
colorCorrection: {
|
|
618
|
+
type: "v3",
|
|
619
|
+
value: new THREE.Vector3(1, 1, 1)
|
|
620
|
+
},
|
|
621
|
+
boostFactors: {
|
|
622
|
+
type: "v3",
|
|
623
|
+
value: new THREE.Vector3(.892, .892, .98595025)
|
|
624
|
+
},
|
|
625
|
+
centreOffset: {
|
|
626
|
+
type: "v3",
|
|
627
|
+
value: new THREE.Vector3(0, 0, 0)
|
|
628
|
+
}
|
|
629
|
+
},
|
|
630
|
+
side: THREE.DoubleSide
|
|
631
|
+
};
|
|
632
|
+
new THREE.ShaderMaterial(DiamondShader);
|
|
633
|
+
// src/csg/node.ts
|
|
634
|
+
var Node = class Node {
|
|
635
|
+
front;
|
|
636
|
+
back;
|
|
637
|
+
divider;
|
|
638
|
+
polygons = [];
|
|
639
|
+
constructor(polygons = []) {
|
|
640
|
+
this.build(polygons);
|
|
641
|
+
}
|
|
642
|
+
clone() {
|
|
643
|
+
const node = new Node();
|
|
644
|
+
node.divider = this.divider?.clone();
|
|
645
|
+
node.polygons = this.polygons.map((p) => p.clone());
|
|
646
|
+
node.front = this.front?.clone();
|
|
647
|
+
node.back = this.back?.clone();
|
|
648
|
+
return node;
|
|
649
|
+
}
|
|
650
|
+
invert() {
|
|
651
|
+
this.polygons.forEach((p) => p.flip());
|
|
652
|
+
this.divider?.flip();
|
|
653
|
+
this.front?.invert();
|
|
654
|
+
this.back?.invert();
|
|
655
|
+
[this.front, this.back] = [this.back, this.front];
|
|
656
|
+
return this;
|
|
657
|
+
}
|
|
658
|
+
clipPolygons(polygons) {
|
|
659
|
+
if (!this.divider) return polygons.map((p) => p.clone());
|
|
660
|
+
let front = [], back = [];
|
|
661
|
+
for (const p of polygons) this.divider.splitPolygon(p, front, back, front, back);
|
|
662
|
+
if (this.front) front = this.front.clipPolygons(front);
|
|
663
|
+
back = this.back ? this.back.clipPolygons(back) : [];
|
|
664
|
+
return front.concat(back);
|
|
665
|
+
}
|
|
666
|
+
clipTo(node) {
|
|
667
|
+
this.polygons = node.clipPolygons(this.polygons);
|
|
668
|
+
this.front?.clipTo(node);
|
|
669
|
+
this.back?.clipTo(node);
|
|
670
|
+
return this;
|
|
671
|
+
}
|
|
672
|
+
allPolygons() {
|
|
673
|
+
return [
|
|
674
|
+
...this.polygons.map((p) => p.clone()),
|
|
675
|
+
...this.front?.allPolygons() ?? [],
|
|
676
|
+
...this.back?.allPolygons() ?? []
|
|
677
|
+
];
|
|
678
|
+
}
|
|
679
|
+
build(polygons) {
|
|
680
|
+
if (!polygons.length) return this;
|
|
681
|
+
this.divider ??= polygons[0].clone();
|
|
682
|
+
const front = [], back = [];
|
|
683
|
+
for (const p of polygons) this.divider.splitPolygon(p, this.polygons, this.polygons, front, back);
|
|
684
|
+
if (front.length) (this.front ??= new Node()).build(front);
|
|
685
|
+
if (back.length) (this.back ??= new Node()).build(back);
|
|
686
|
+
return this;
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
// src/csg/bsp.ts
|
|
690
|
+
/** BSP solid booleans on closed, consistently outward oriented convex polygon faces. */
|
|
691
|
+
var BSP = class BSP {
|
|
692
|
+
tree;
|
|
693
|
+
constructor(faces = []) {
|
|
694
|
+
this.tree = new Node(faces);
|
|
695
|
+
}
|
|
696
|
+
toPolygons() {
|
|
697
|
+
return this.tree.allPolygons();
|
|
698
|
+
}
|
|
699
|
+
clone() {
|
|
700
|
+
return new BSP(this.toPolygons());
|
|
701
|
+
}
|
|
702
|
+
inverse() {
|
|
703
|
+
const result = this.clone();
|
|
704
|
+
result.tree.invert();
|
|
705
|
+
return result;
|
|
706
|
+
}
|
|
707
|
+
sub(other) {
|
|
708
|
+
if (!this.toPolygons().length || !other.toPolygons().length) return this.clone();
|
|
709
|
+
const a = this.tree.clone(), b = other.tree.clone();
|
|
710
|
+
a.invert();
|
|
711
|
+
a.clipTo(b);
|
|
712
|
+
b.clipTo(a);
|
|
713
|
+
b.invert();
|
|
714
|
+
b.clipTo(a);
|
|
715
|
+
b.invert();
|
|
716
|
+
a.build(b.allPolygons());
|
|
717
|
+
a.invert();
|
|
718
|
+
return new BSP(a.allPolygons());
|
|
719
|
+
}
|
|
720
|
+
subtract(other) {
|
|
721
|
+
return this.sub(other);
|
|
722
|
+
}
|
|
723
|
+
union(other) {
|
|
724
|
+
if (!this.toPolygons().length) return other.clone();
|
|
725
|
+
if (!other.toPolygons().length) return this.clone();
|
|
726
|
+
const a = this.tree.clone(), b = other.tree.clone();
|
|
727
|
+
a.clipTo(b);
|
|
728
|
+
b.clipTo(a);
|
|
729
|
+
b.invert();
|
|
730
|
+
b.clipTo(a);
|
|
731
|
+
b.invert();
|
|
732
|
+
a.build(b.allPolygons());
|
|
733
|
+
return new BSP(a.allPolygons());
|
|
734
|
+
}
|
|
735
|
+
intersect(other) {
|
|
736
|
+
if (!this.toPolygons().length || !other.toPolygons().length) return new BSP();
|
|
737
|
+
const a = this.tree.clone(), b = other.tree.clone();
|
|
738
|
+
a.invert();
|
|
739
|
+
b.clipTo(a);
|
|
740
|
+
b.invert();
|
|
741
|
+
a.clipTo(b);
|
|
742
|
+
b.clipTo(a);
|
|
743
|
+
a.build(b.allPolygons());
|
|
744
|
+
a.invert();
|
|
745
|
+
return new BSP(a.allPolygons());
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
// node_modules/decimal.js/decimal.mjs
|
|
749
|
+
/*!
|
|
750
|
+
* decimal.js v10.6.0
|
|
751
|
+
* An arbitrary-precision Decimal type for JavaScript.
|
|
752
|
+
* https://github.com/MikeMcl/decimal.js
|
|
753
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
754
|
+
* MIT Licence
|
|
755
|
+
*/
|
|
756
|
+
var EXP_LIMIT = 9e15;
|
|
757
|
+
var MAX_DIGITS = 1e9;
|
|
758
|
+
var NUMERALS = "0123456789abcdef";
|
|
759
|
+
var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
|
|
760
|
+
var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
|
|
761
|
+
var DEFAULTS = {
|
|
762
|
+
precision: 20,
|
|
763
|
+
rounding: 4,
|
|
764
|
+
modulo: 1,
|
|
765
|
+
toExpNeg: -7,
|
|
766
|
+
toExpPos: 21,
|
|
767
|
+
minE: -EXP_LIMIT,
|
|
768
|
+
maxE: EXP_LIMIT,
|
|
769
|
+
crypto: false
|
|
770
|
+
};
|
|
771
|
+
var inexact;
|
|
772
|
+
var quadrant;
|
|
773
|
+
var external = true;
|
|
774
|
+
var decimalError = "[DecimalError] ";
|
|
775
|
+
var invalidArgument = decimalError + "Invalid argument: ";
|
|
776
|
+
var precisionLimitExceeded = decimalError + "Precision limit exceeded";
|
|
777
|
+
var cryptoUnavailable = decimalError + "crypto unavailable";
|
|
778
|
+
var tag = "[object Decimal]";
|
|
779
|
+
var mathfloor = Math.floor;
|
|
780
|
+
var mathpow = Math.pow;
|
|
781
|
+
var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
|
|
782
|
+
var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
|
|
783
|
+
var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
|
|
784
|
+
var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
785
|
+
var BASE = 1e7;
|
|
786
|
+
var LOG_BASE = 7;
|
|
787
|
+
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
|
788
|
+
var LN10_PRECISION = LN10.length - 1;
|
|
789
|
+
var PI_PRECISION = PI.length - 1;
|
|
790
|
+
var P = { toStringTag: tag };
|
|
791
|
+
P.absoluteValue = P.abs = function() {
|
|
792
|
+
var x = new this.constructor(this);
|
|
793
|
+
if (x.s < 0) x.s = 1;
|
|
794
|
+
return finalise(x);
|
|
795
|
+
};
|
|
796
|
+
P.ceil = function() {
|
|
797
|
+
return finalise(new this.constructor(this), this.e + 1, 2);
|
|
798
|
+
};
|
|
799
|
+
P.clampedTo = P.clamp = function(min, max) {
|
|
800
|
+
var k, x = this, Ctor = x.constructor;
|
|
801
|
+
min = new Ctor(min);
|
|
802
|
+
max = new Ctor(max);
|
|
803
|
+
if (!min.s || !max.s) return new Ctor(NaN);
|
|
804
|
+
if (min.gt(max)) throw Error(invalidArgument + max);
|
|
805
|
+
k = x.cmp(min);
|
|
806
|
+
return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
|
|
807
|
+
};
|
|
808
|
+
P.comparedTo = P.cmp = function(y) {
|
|
809
|
+
var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
|
|
810
|
+
if (!xd || !yd) return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
|
|
811
|
+
if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
|
|
812
|
+
if (xs !== ys) return xs;
|
|
813
|
+
if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
|
|
814
|
+
xdL = xd.length;
|
|
815
|
+
ydL = yd.length;
|
|
816
|
+
for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
|
|
817
|
+
return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
|
|
818
|
+
};
|
|
819
|
+
P.cosine = P.cos = function() {
|
|
820
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
821
|
+
if (!x.d) return new Ctor(NaN);
|
|
822
|
+
if (!x.d[0]) return new Ctor(1);
|
|
823
|
+
pr = Ctor.precision;
|
|
824
|
+
rm = Ctor.rounding;
|
|
825
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
826
|
+
Ctor.rounding = 1;
|
|
827
|
+
x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
828
|
+
Ctor.precision = pr;
|
|
829
|
+
Ctor.rounding = rm;
|
|
830
|
+
return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
|
|
831
|
+
};
|
|
832
|
+
P.cubeRoot = P.cbrt = function() {
|
|
833
|
+
var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
|
|
834
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
835
|
+
external = false;
|
|
836
|
+
s = x.s * mathpow(x.s * x, 1 / 3);
|
|
837
|
+
if (!s || Math.abs(s) == 1 / 0) {
|
|
838
|
+
n = digitsToString(x.d);
|
|
839
|
+
e = x.e;
|
|
840
|
+
if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
|
|
841
|
+
s = mathpow(n, 1 / 3);
|
|
842
|
+
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
|
|
843
|
+
if (s == 1 / 0) n = "5e" + e;
|
|
844
|
+
else {
|
|
845
|
+
n = s.toExponential();
|
|
846
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
847
|
+
}
|
|
848
|
+
r = new Ctor(n);
|
|
849
|
+
r.s = x.s;
|
|
850
|
+
} else r = new Ctor(s.toString());
|
|
851
|
+
sd = (e = Ctor.precision) + 3;
|
|
852
|
+
for (;;) {
|
|
853
|
+
t = r;
|
|
854
|
+
t3 = t.times(t).times(t);
|
|
855
|
+
t3plusx = t3.plus(x);
|
|
856
|
+
r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
|
|
857
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
858
|
+
n = n.slice(sd - 3, sd + 1);
|
|
859
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
860
|
+
if (!rep) {
|
|
861
|
+
finalise(t, e + 1, 0);
|
|
862
|
+
if (t.times(t).times(t).eq(x)) {
|
|
863
|
+
r = t;
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
sd += 4;
|
|
868
|
+
rep = 1;
|
|
869
|
+
} else {
|
|
870
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
871
|
+
finalise(r, e + 1, 1);
|
|
872
|
+
m = !r.times(r).times(r).eq(x);
|
|
873
|
+
}
|
|
874
|
+
break;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
external = true;
|
|
879
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
880
|
+
};
|
|
881
|
+
P.decimalPlaces = P.dp = function() {
|
|
882
|
+
var w, d = this.d, n = NaN;
|
|
883
|
+
if (d) {
|
|
884
|
+
w = d.length - 1;
|
|
885
|
+
n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
886
|
+
w = d[w];
|
|
887
|
+
if (w) for (; w % 10 == 0; w /= 10) n--;
|
|
888
|
+
if (n < 0) n = 0;
|
|
889
|
+
}
|
|
890
|
+
return n;
|
|
891
|
+
};
|
|
892
|
+
P.dividedBy = P.div = function(y) {
|
|
893
|
+
return divide(this, new this.constructor(y));
|
|
894
|
+
};
|
|
895
|
+
P.dividedToIntegerBy = P.divToInt = function(y) {
|
|
896
|
+
var x = this, Ctor = x.constructor;
|
|
897
|
+
return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
|
|
898
|
+
};
|
|
899
|
+
P.equals = P.eq = function(y) {
|
|
900
|
+
return this.cmp(y) === 0;
|
|
901
|
+
};
|
|
902
|
+
P.floor = function() {
|
|
903
|
+
return finalise(new this.constructor(this), this.e + 1, 3);
|
|
904
|
+
};
|
|
905
|
+
P.greaterThan = P.gt = function(y) {
|
|
906
|
+
return this.cmp(y) > 0;
|
|
907
|
+
};
|
|
908
|
+
P.greaterThanOrEqualTo = P.gte = function(y) {
|
|
909
|
+
var k = this.cmp(y);
|
|
910
|
+
return k == 1 || k === 0;
|
|
911
|
+
};
|
|
912
|
+
P.hyperbolicCosine = P.cosh = function() {
|
|
913
|
+
var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
|
|
914
|
+
if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
|
|
915
|
+
if (x.isZero()) return one;
|
|
916
|
+
pr = Ctor.precision;
|
|
917
|
+
rm = Ctor.rounding;
|
|
918
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
919
|
+
Ctor.rounding = 1;
|
|
920
|
+
len = x.d.length;
|
|
921
|
+
if (len < 32) {
|
|
922
|
+
k = Math.ceil(len / 3);
|
|
923
|
+
n = (1 / tinyPow(4, k)).toString();
|
|
924
|
+
} else {
|
|
925
|
+
k = 16;
|
|
926
|
+
n = "2.3283064365386962890625e-10";
|
|
927
|
+
}
|
|
928
|
+
x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
|
|
929
|
+
var cosh2_x, i = k, d8 = new Ctor(8);
|
|
930
|
+
for (; i--;) {
|
|
931
|
+
cosh2_x = x.times(x);
|
|
932
|
+
x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
|
|
933
|
+
}
|
|
934
|
+
return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
935
|
+
};
|
|
936
|
+
P.hyperbolicSine = P.sinh = function() {
|
|
937
|
+
var k, pr, rm, len, x = this, Ctor = x.constructor;
|
|
938
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
939
|
+
pr = Ctor.precision;
|
|
940
|
+
rm = Ctor.rounding;
|
|
941
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
942
|
+
Ctor.rounding = 1;
|
|
943
|
+
len = x.d.length;
|
|
944
|
+
if (len < 3) x = taylorSeries(Ctor, 2, x, x, true);
|
|
945
|
+
else {
|
|
946
|
+
k = 1.4 * Math.sqrt(len);
|
|
947
|
+
k = k > 16 ? 16 : k | 0;
|
|
948
|
+
x = x.times(1 / tinyPow(5, k));
|
|
949
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
950
|
+
var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
951
|
+
for (; k--;) {
|
|
952
|
+
sinh2_x = x.times(x);
|
|
953
|
+
x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
Ctor.precision = pr;
|
|
957
|
+
Ctor.rounding = rm;
|
|
958
|
+
return finalise(x, pr, rm, true);
|
|
959
|
+
};
|
|
960
|
+
P.hyperbolicTangent = P.tanh = function() {
|
|
961
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
962
|
+
if (!x.isFinite()) return new Ctor(x.s);
|
|
963
|
+
if (x.isZero()) return new Ctor(x);
|
|
964
|
+
pr = Ctor.precision;
|
|
965
|
+
rm = Ctor.rounding;
|
|
966
|
+
Ctor.precision = pr + 7;
|
|
967
|
+
Ctor.rounding = 1;
|
|
968
|
+
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
|
|
969
|
+
};
|
|
970
|
+
P.inverseCosine = P.acos = function() {
|
|
971
|
+
var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
|
|
972
|
+
if (k !== -1) return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
|
|
973
|
+
if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(.5);
|
|
974
|
+
Ctor.precision = pr + 6;
|
|
975
|
+
Ctor.rounding = 1;
|
|
976
|
+
x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
|
|
977
|
+
Ctor.precision = pr;
|
|
978
|
+
Ctor.rounding = rm;
|
|
979
|
+
return x.times(2);
|
|
980
|
+
};
|
|
981
|
+
P.inverseHyperbolicCosine = P.acosh = function() {
|
|
982
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
983
|
+
if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
|
|
984
|
+
if (!x.isFinite()) return new Ctor(x);
|
|
985
|
+
pr = Ctor.precision;
|
|
986
|
+
rm = Ctor.rounding;
|
|
987
|
+
Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
|
|
988
|
+
Ctor.rounding = 1;
|
|
989
|
+
external = false;
|
|
990
|
+
x = x.times(x).minus(1).sqrt().plus(x);
|
|
991
|
+
external = true;
|
|
992
|
+
Ctor.precision = pr;
|
|
993
|
+
Ctor.rounding = rm;
|
|
994
|
+
return x.ln();
|
|
995
|
+
};
|
|
996
|
+
P.inverseHyperbolicSine = P.asinh = function() {
|
|
997
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
998
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
999
|
+
pr = Ctor.precision;
|
|
1000
|
+
rm = Ctor.rounding;
|
|
1001
|
+
Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
|
|
1002
|
+
Ctor.rounding = 1;
|
|
1003
|
+
external = false;
|
|
1004
|
+
x = x.times(x).plus(1).sqrt().plus(x);
|
|
1005
|
+
external = true;
|
|
1006
|
+
Ctor.precision = pr;
|
|
1007
|
+
Ctor.rounding = rm;
|
|
1008
|
+
return x.ln();
|
|
1009
|
+
};
|
|
1010
|
+
P.inverseHyperbolicTangent = P.atanh = function() {
|
|
1011
|
+
var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
|
|
1012
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1013
|
+
if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
|
|
1014
|
+
pr = Ctor.precision;
|
|
1015
|
+
rm = Ctor.rounding;
|
|
1016
|
+
xsd = x.sd();
|
|
1017
|
+
if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
|
|
1018
|
+
Ctor.precision = wpr = xsd - x.e;
|
|
1019
|
+
x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
|
|
1020
|
+
Ctor.precision = pr + 4;
|
|
1021
|
+
Ctor.rounding = 1;
|
|
1022
|
+
x = x.ln();
|
|
1023
|
+
Ctor.precision = pr;
|
|
1024
|
+
Ctor.rounding = rm;
|
|
1025
|
+
return x.times(.5);
|
|
1026
|
+
};
|
|
1027
|
+
P.inverseSine = P.asin = function() {
|
|
1028
|
+
var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
|
|
1029
|
+
if (x.isZero()) return new Ctor(x);
|
|
1030
|
+
k = x.abs().cmp(1);
|
|
1031
|
+
pr = Ctor.precision;
|
|
1032
|
+
rm = Ctor.rounding;
|
|
1033
|
+
if (k !== -1) {
|
|
1034
|
+
if (k === 0) {
|
|
1035
|
+
halfPi = getPi(Ctor, pr + 4, rm).times(.5);
|
|
1036
|
+
halfPi.s = x.s;
|
|
1037
|
+
return halfPi;
|
|
1038
|
+
}
|
|
1039
|
+
return new Ctor(NaN);
|
|
1040
|
+
}
|
|
1041
|
+
Ctor.precision = pr + 6;
|
|
1042
|
+
Ctor.rounding = 1;
|
|
1043
|
+
x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
|
|
1044
|
+
Ctor.precision = pr;
|
|
1045
|
+
Ctor.rounding = rm;
|
|
1046
|
+
return x.times(2);
|
|
1047
|
+
};
|
|
1048
|
+
P.inverseTangent = P.atan = function() {
|
|
1049
|
+
var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
|
|
1050
|
+
if (!x.isFinite()) {
|
|
1051
|
+
if (!x.s) return new Ctor(NaN);
|
|
1052
|
+
if (pr + 4 <= PI_PRECISION) {
|
|
1053
|
+
r = getPi(Ctor, pr + 4, rm).times(.5);
|
|
1054
|
+
r.s = x.s;
|
|
1055
|
+
return r;
|
|
1056
|
+
}
|
|
1057
|
+
} else if (x.isZero()) return new Ctor(x);
|
|
1058
|
+
else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
|
|
1059
|
+
r = getPi(Ctor, pr + 4, rm).times(.25);
|
|
1060
|
+
r.s = x.s;
|
|
1061
|
+
return r;
|
|
1062
|
+
}
|
|
1063
|
+
Ctor.precision = wpr = pr + 10;
|
|
1064
|
+
Ctor.rounding = 1;
|
|
1065
|
+
k = Math.min(28, wpr / LOG_BASE + 2 | 0);
|
|
1066
|
+
for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
|
|
1067
|
+
external = false;
|
|
1068
|
+
j = Math.ceil(wpr / LOG_BASE);
|
|
1069
|
+
n = 1;
|
|
1070
|
+
x2 = x.times(x);
|
|
1071
|
+
r = new Ctor(x);
|
|
1072
|
+
px = x;
|
|
1073
|
+
for (; i !== -1;) {
|
|
1074
|
+
px = px.times(x2);
|
|
1075
|
+
t = r.minus(px.div(n += 2));
|
|
1076
|
+
px = px.times(x2);
|
|
1077
|
+
r = t.plus(px.div(n += 2));
|
|
1078
|
+
if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--;);
|
|
1079
|
+
}
|
|
1080
|
+
if (k) r = r.times(2 << k - 1);
|
|
1081
|
+
external = true;
|
|
1082
|
+
return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
1083
|
+
};
|
|
1084
|
+
P.isFinite = function() {
|
|
1085
|
+
return !!this.d;
|
|
1086
|
+
};
|
|
1087
|
+
P.isInteger = P.isInt = function() {
|
|
1088
|
+
return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
|
|
1089
|
+
};
|
|
1090
|
+
P.isNaN = function() {
|
|
1091
|
+
return !this.s;
|
|
1092
|
+
};
|
|
1093
|
+
P.isNegative = P.isNeg = function() {
|
|
1094
|
+
return this.s < 0;
|
|
1095
|
+
};
|
|
1096
|
+
P.isPositive = P.isPos = function() {
|
|
1097
|
+
return this.s > 0;
|
|
1098
|
+
};
|
|
1099
|
+
P.isZero = function() {
|
|
1100
|
+
return !!this.d && this.d[0] === 0;
|
|
1101
|
+
};
|
|
1102
|
+
P.lessThan = P.lt = function(y) {
|
|
1103
|
+
return this.cmp(y) < 0;
|
|
1104
|
+
};
|
|
1105
|
+
P.lessThanOrEqualTo = P.lte = function(y) {
|
|
1106
|
+
return this.cmp(y) < 1;
|
|
1107
|
+
};
|
|
1108
|
+
P.logarithm = P.log = function(base) {
|
|
1109
|
+
var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
|
|
1110
|
+
if (base == null) {
|
|
1111
|
+
base = new Ctor(10);
|
|
1112
|
+
isBase10 = true;
|
|
1113
|
+
} else {
|
|
1114
|
+
base = new Ctor(base);
|
|
1115
|
+
d = base.d;
|
|
1116
|
+
if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
|
|
1117
|
+
isBase10 = base.eq(10);
|
|
1118
|
+
}
|
|
1119
|
+
d = arg.d;
|
|
1120
|
+
if (arg.s < 0 || !d || !d[0] || arg.eq(1)) return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
|
|
1121
|
+
if (isBase10) {
|
|
1122
|
+
if (d.length > 1) inf = true;
|
|
1123
|
+
else {
|
|
1124
|
+
for (k = d[0]; k % 10 === 0;) k /= 10;
|
|
1125
|
+
inf = k !== 1;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
external = false;
|
|
1129
|
+
sd = pr + guard;
|
|
1130
|
+
num = naturalLogarithm(arg, sd);
|
|
1131
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
1132
|
+
r = divide(num, denominator, sd, 1);
|
|
1133
|
+
if (checkRoundingDigits(r.d, k = pr, rm)) do {
|
|
1134
|
+
sd += 10;
|
|
1135
|
+
num = naturalLogarithm(arg, sd);
|
|
1136
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
1137
|
+
r = divide(num, denominator, sd, 1);
|
|
1138
|
+
if (!inf) {
|
|
1139
|
+
if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) r = finalise(r, pr + 1, 0);
|
|
1140
|
+
break;
|
|
1141
|
+
}
|
|
1142
|
+
} while (checkRoundingDigits(r.d, k += 10, rm));
|
|
1143
|
+
external = true;
|
|
1144
|
+
return finalise(r, pr, rm);
|
|
1145
|
+
};
|
|
1146
|
+
P.minus = P.sub = function(y) {
|
|
1147
|
+
var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
|
|
1148
|
+
y = new Ctor(y);
|
|
1149
|
+
if (!x.d || !y.d) {
|
|
1150
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
1151
|
+
else if (x.d) y.s = -y.s;
|
|
1152
|
+
else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
|
|
1153
|
+
return y;
|
|
1154
|
+
}
|
|
1155
|
+
if (x.s != y.s) {
|
|
1156
|
+
y.s = -y.s;
|
|
1157
|
+
return x.plus(y);
|
|
1158
|
+
}
|
|
1159
|
+
xd = x.d;
|
|
1160
|
+
yd = y.d;
|
|
1161
|
+
pr = Ctor.precision;
|
|
1162
|
+
rm = Ctor.rounding;
|
|
1163
|
+
if (!xd[0] || !yd[0]) {
|
|
1164
|
+
if (yd[0]) y.s = -y.s;
|
|
1165
|
+
else if (xd[0]) y = new Ctor(x);
|
|
1166
|
+
else return new Ctor(rm === 3 ? -0 : 0);
|
|
1167
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1168
|
+
}
|
|
1169
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1170
|
+
xe = mathfloor(x.e / LOG_BASE);
|
|
1171
|
+
xd = xd.slice();
|
|
1172
|
+
k = xe - e;
|
|
1173
|
+
if (k) {
|
|
1174
|
+
xLTy = k < 0;
|
|
1175
|
+
if (xLTy) {
|
|
1176
|
+
d = xd;
|
|
1177
|
+
k = -k;
|
|
1178
|
+
len = yd.length;
|
|
1179
|
+
} else {
|
|
1180
|
+
d = yd;
|
|
1181
|
+
e = xe;
|
|
1182
|
+
len = xd.length;
|
|
1183
|
+
}
|
|
1184
|
+
i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
|
|
1185
|
+
if (k > i) {
|
|
1186
|
+
k = i;
|
|
1187
|
+
d.length = 1;
|
|
1188
|
+
}
|
|
1189
|
+
d.reverse();
|
|
1190
|
+
for (i = k; i--;) d.push(0);
|
|
1191
|
+
d.reverse();
|
|
1192
|
+
} else {
|
|
1193
|
+
i = xd.length;
|
|
1194
|
+
len = yd.length;
|
|
1195
|
+
xLTy = i < len;
|
|
1196
|
+
if (xLTy) len = i;
|
|
1197
|
+
for (i = 0; i < len; i++) if (xd[i] != yd[i]) {
|
|
1198
|
+
xLTy = xd[i] < yd[i];
|
|
1199
|
+
break;
|
|
1200
|
+
}
|
|
1201
|
+
k = 0;
|
|
1202
|
+
}
|
|
1203
|
+
if (xLTy) {
|
|
1204
|
+
d = xd;
|
|
1205
|
+
xd = yd;
|
|
1206
|
+
yd = d;
|
|
1207
|
+
y.s = -y.s;
|
|
1208
|
+
}
|
|
1209
|
+
len = xd.length;
|
|
1210
|
+
for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
|
|
1211
|
+
for (i = yd.length; i > k;) {
|
|
1212
|
+
if (xd[--i] < yd[i]) {
|
|
1213
|
+
for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1;
|
|
1214
|
+
--xd[j];
|
|
1215
|
+
xd[i] += BASE;
|
|
1216
|
+
}
|
|
1217
|
+
xd[i] -= yd[i];
|
|
1218
|
+
}
|
|
1219
|
+
for (; xd[--len] === 0;) xd.pop();
|
|
1220
|
+
for (; xd[0] === 0; xd.shift()) --e;
|
|
1221
|
+
if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
|
|
1222
|
+
y.d = xd;
|
|
1223
|
+
y.e = getBase10Exponent(xd, e);
|
|
1224
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1225
|
+
};
|
|
1226
|
+
P.modulo = P.mod = function(y) {
|
|
1227
|
+
var q, x = this, Ctor = x.constructor;
|
|
1228
|
+
y = new Ctor(y);
|
|
1229
|
+
if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
|
|
1230
|
+
if (!y.d || x.d && !x.d[0]) return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
|
|
1231
|
+
external = false;
|
|
1232
|
+
if (Ctor.modulo == 9) {
|
|
1233
|
+
q = divide(x, y.abs(), 0, 3, 1);
|
|
1234
|
+
q.s *= y.s;
|
|
1235
|
+
} else q = divide(x, y, 0, Ctor.modulo, 1);
|
|
1236
|
+
q = q.times(y);
|
|
1237
|
+
external = true;
|
|
1238
|
+
return x.minus(q);
|
|
1239
|
+
};
|
|
1240
|
+
P.naturalExponential = P.exp = function() {
|
|
1241
|
+
return naturalExponential(this);
|
|
1242
|
+
};
|
|
1243
|
+
P.naturalLogarithm = P.ln = function() {
|
|
1244
|
+
return naturalLogarithm(this);
|
|
1245
|
+
};
|
|
1246
|
+
P.negated = P.neg = function() {
|
|
1247
|
+
var x = new this.constructor(this);
|
|
1248
|
+
x.s = -x.s;
|
|
1249
|
+
return finalise(x);
|
|
1250
|
+
};
|
|
1251
|
+
P.plus = P.add = function(y) {
|
|
1252
|
+
var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
|
|
1253
|
+
y = new Ctor(y);
|
|
1254
|
+
if (!x.d || !y.d) {
|
|
1255
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
1256
|
+
else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
|
|
1257
|
+
return y;
|
|
1258
|
+
}
|
|
1259
|
+
if (x.s != y.s) {
|
|
1260
|
+
y.s = -y.s;
|
|
1261
|
+
return x.minus(y);
|
|
1262
|
+
}
|
|
1263
|
+
xd = x.d;
|
|
1264
|
+
yd = y.d;
|
|
1265
|
+
pr = Ctor.precision;
|
|
1266
|
+
rm = Ctor.rounding;
|
|
1267
|
+
if (!xd[0] || !yd[0]) {
|
|
1268
|
+
if (!yd[0]) y = new Ctor(x);
|
|
1269
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1270
|
+
}
|
|
1271
|
+
k = mathfloor(x.e / LOG_BASE);
|
|
1272
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1273
|
+
xd = xd.slice();
|
|
1274
|
+
i = k - e;
|
|
1275
|
+
if (i) {
|
|
1276
|
+
if (i < 0) {
|
|
1277
|
+
d = xd;
|
|
1278
|
+
i = -i;
|
|
1279
|
+
len = yd.length;
|
|
1280
|
+
} else {
|
|
1281
|
+
d = yd;
|
|
1282
|
+
e = k;
|
|
1283
|
+
len = xd.length;
|
|
1284
|
+
}
|
|
1285
|
+
k = Math.ceil(pr / LOG_BASE);
|
|
1286
|
+
len = k > len ? k + 1 : len + 1;
|
|
1287
|
+
if (i > len) {
|
|
1288
|
+
i = len;
|
|
1289
|
+
d.length = 1;
|
|
1290
|
+
}
|
|
1291
|
+
d.reverse();
|
|
1292
|
+
for (; i--;) d.push(0);
|
|
1293
|
+
d.reverse();
|
|
1294
|
+
}
|
|
1295
|
+
len = xd.length;
|
|
1296
|
+
i = yd.length;
|
|
1297
|
+
if (len - i < 0) {
|
|
1298
|
+
i = len;
|
|
1299
|
+
d = yd;
|
|
1300
|
+
yd = xd;
|
|
1301
|
+
xd = d;
|
|
1302
|
+
}
|
|
1303
|
+
for (carry = 0; i;) {
|
|
1304
|
+
carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
|
|
1305
|
+
xd[i] %= BASE;
|
|
1306
|
+
}
|
|
1307
|
+
if (carry) {
|
|
1308
|
+
xd.unshift(carry);
|
|
1309
|
+
++e;
|
|
1310
|
+
}
|
|
1311
|
+
for (len = xd.length; xd[--len] == 0;) xd.pop();
|
|
1312
|
+
y.d = xd;
|
|
1313
|
+
y.e = getBase10Exponent(xd, e);
|
|
1314
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1315
|
+
};
|
|
1316
|
+
P.precision = P.sd = function(z) {
|
|
1317
|
+
var k, x = this;
|
|
1318
|
+
if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
|
|
1319
|
+
if (x.d) {
|
|
1320
|
+
k = getPrecision(x.d);
|
|
1321
|
+
if (z && x.e + 1 > k) k = x.e + 1;
|
|
1322
|
+
} else k = NaN;
|
|
1323
|
+
return k;
|
|
1324
|
+
};
|
|
1325
|
+
P.round = function() {
|
|
1326
|
+
var x = this, Ctor = x.constructor;
|
|
1327
|
+
return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
|
|
1328
|
+
};
|
|
1329
|
+
P.sine = P.sin = function() {
|
|
1330
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1331
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1332
|
+
if (x.isZero()) return new Ctor(x);
|
|
1333
|
+
pr = Ctor.precision;
|
|
1334
|
+
rm = Ctor.rounding;
|
|
1335
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
1336
|
+
Ctor.rounding = 1;
|
|
1337
|
+
x = sine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
1338
|
+
Ctor.precision = pr;
|
|
1339
|
+
Ctor.rounding = rm;
|
|
1340
|
+
return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
|
|
1341
|
+
};
|
|
1342
|
+
P.squareRoot = P.sqrt = function() {
|
|
1343
|
+
var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
|
|
1344
|
+
if (s !== 1 || !d || !d[0]) return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
|
|
1345
|
+
external = false;
|
|
1346
|
+
s = Math.sqrt(+x);
|
|
1347
|
+
if (s == 0 || s == 1 / 0) {
|
|
1348
|
+
n = digitsToString(d);
|
|
1349
|
+
if ((n.length + e) % 2 == 0) n += "0";
|
|
1350
|
+
s = Math.sqrt(n);
|
|
1351
|
+
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
1352
|
+
if (s == 1 / 0) n = "5e" + e;
|
|
1353
|
+
else {
|
|
1354
|
+
n = s.toExponential();
|
|
1355
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
1356
|
+
}
|
|
1357
|
+
r = new Ctor(n);
|
|
1358
|
+
} else r = new Ctor(s.toString());
|
|
1359
|
+
sd = (e = Ctor.precision) + 3;
|
|
1360
|
+
for (;;) {
|
|
1361
|
+
t = r;
|
|
1362
|
+
r = t.plus(divide(x, t, sd + 2, 1)).times(.5);
|
|
1363
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
1364
|
+
n = n.slice(sd - 3, sd + 1);
|
|
1365
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
1366
|
+
if (!rep) {
|
|
1367
|
+
finalise(t, e + 1, 0);
|
|
1368
|
+
if (t.times(t).eq(x)) {
|
|
1369
|
+
r = t;
|
|
1370
|
+
break;
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
sd += 4;
|
|
1374
|
+
rep = 1;
|
|
1375
|
+
} else {
|
|
1376
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
1377
|
+
finalise(r, e + 1, 1);
|
|
1378
|
+
m = !r.times(r).eq(x);
|
|
1379
|
+
}
|
|
1380
|
+
break;
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
external = true;
|
|
1385
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
1386
|
+
};
|
|
1387
|
+
P.tangent = P.tan = function() {
|
|
1388
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1389
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1390
|
+
if (x.isZero()) return new Ctor(x);
|
|
1391
|
+
pr = Ctor.precision;
|
|
1392
|
+
rm = Ctor.rounding;
|
|
1393
|
+
Ctor.precision = pr + 10;
|
|
1394
|
+
Ctor.rounding = 1;
|
|
1395
|
+
x = x.sin();
|
|
1396
|
+
x.s = 1;
|
|
1397
|
+
x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
|
|
1398
|
+
Ctor.precision = pr;
|
|
1399
|
+
Ctor.rounding = rm;
|
|
1400
|
+
return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
|
|
1401
|
+
};
|
|
1402
|
+
P.times = P.mul = function(y) {
|
|
1403
|
+
var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
|
|
1404
|
+
y.s *= x.s;
|
|
1405
|
+
if (!xd || !xd[0] || !yd || !yd[0]) return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
|
|
1406
|
+
e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
|
|
1407
|
+
xdL = xd.length;
|
|
1408
|
+
ydL = yd.length;
|
|
1409
|
+
if (xdL < ydL) {
|
|
1410
|
+
r = xd;
|
|
1411
|
+
xd = yd;
|
|
1412
|
+
yd = r;
|
|
1413
|
+
rL = xdL;
|
|
1414
|
+
xdL = ydL;
|
|
1415
|
+
ydL = rL;
|
|
1416
|
+
}
|
|
1417
|
+
r = [];
|
|
1418
|
+
rL = xdL + ydL;
|
|
1419
|
+
for (i = rL; i--;) r.push(0);
|
|
1420
|
+
for (i = ydL; --i >= 0;) {
|
|
1421
|
+
carry = 0;
|
|
1422
|
+
for (k = xdL + i; k > i;) {
|
|
1423
|
+
t = r[k] + yd[i] * xd[k - i - 1] + carry;
|
|
1424
|
+
r[k--] = t % BASE | 0;
|
|
1425
|
+
carry = t / BASE | 0;
|
|
1426
|
+
}
|
|
1427
|
+
r[k] = (r[k] + carry) % BASE | 0;
|
|
1428
|
+
}
|
|
1429
|
+
for (; !r[--rL];) r.pop();
|
|
1430
|
+
if (carry) ++e;
|
|
1431
|
+
else r.shift();
|
|
1432
|
+
y.d = r;
|
|
1433
|
+
y.e = getBase10Exponent(r, e);
|
|
1434
|
+
return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
|
|
1435
|
+
};
|
|
1436
|
+
P.toBinary = function(sd, rm) {
|
|
1437
|
+
return toStringBinary(this, 2, sd, rm);
|
|
1438
|
+
};
|
|
1439
|
+
P.toDecimalPlaces = P.toDP = function(dp, rm) {
|
|
1440
|
+
var x = this, Ctor = x.constructor;
|
|
1441
|
+
x = new Ctor(x);
|
|
1442
|
+
if (dp === void 0) return x;
|
|
1443
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1444
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1445
|
+
else checkInt32(rm, 0, 8);
|
|
1446
|
+
return finalise(x, dp + x.e + 1, rm);
|
|
1447
|
+
};
|
|
1448
|
+
P.toExponential = function(dp, rm) {
|
|
1449
|
+
var str, x = this, Ctor = x.constructor;
|
|
1450
|
+
if (dp === void 0) str = finiteToString(x, true);
|
|
1451
|
+
else {
|
|
1452
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1453
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1454
|
+
else checkInt32(rm, 0, 8);
|
|
1455
|
+
x = finalise(new Ctor(x), dp + 1, rm);
|
|
1456
|
+
str = finiteToString(x, true, dp + 1);
|
|
1457
|
+
}
|
|
1458
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1459
|
+
};
|
|
1460
|
+
P.toFixed = function(dp, rm) {
|
|
1461
|
+
var str, y, x = this, Ctor = x.constructor;
|
|
1462
|
+
if (dp === void 0) str = finiteToString(x);
|
|
1463
|
+
else {
|
|
1464
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1465
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1466
|
+
else checkInt32(rm, 0, 8);
|
|
1467
|
+
y = finalise(new Ctor(x), dp + x.e + 1, rm);
|
|
1468
|
+
str = finiteToString(y, false, dp + y.e + 1);
|
|
1469
|
+
}
|
|
1470
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1471
|
+
};
|
|
1472
|
+
P.toFraction = function(maxD) {
|
|
1473
|
+
var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
|
|
1474
|
+
if (!xd) return new Ctor(x);
|
|
1475
|
+
n1 = d0 = new Ctor(1);
|
|
1476
|
+
d1 = n0 = new Ctor(0);
|
|
1477
|
+
d = new Ctor(d1);
|
|
1478
|
+
e = d.e = getPrecision(xd) - x.e - 1;
|
|
1479
|
+
k = e % LOG_BASE;
|
|
1480
|
+
d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
|
|
1481
|
+
if (maxD == null) maxD = e > 0 ? d : n1;
|
|
1482
|
+
else {
|
|
1483
|
+
n = new Ctor(maxD);
|
|
1484
|
+
if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
|
|
1485
|
+
maxD = n.gt(d) ? e > 0 ? d : n1 : n;
|
|
1486
|
+
}
|
|
1487
|
+
external = false;
|
|
1488
|
+
n = new Ctor(digitsToString(xd));
|
|
1489
|
+
pr = Ctor.precision;
|
|
1490
|
+
Ctor.precision = e = xd.length * LOG_BASE * 2;
|
|
1491
|
+
for (;;) {
|
|
1492
|
+
q = divide(n, d, 0, 1, 1);
|
|
1493
|
+
d2 = d0.plus(q.times(d1));
|
|
1494
|
+
if (d2.cmp(maxD) == 1) break;
|
|
1495
|
+
d0 = d1;
|
|
1496
|
+
d1 = d2;
|
|
1497
|
+
d2 = n1;
|
|
1498
|
+
n1 = n0.plus(q.times(d2));
|
|
1499
|
+
n0 = d2;
|
|
1500
|
+
d2 = d;
|
|
1501
|
+
d = n.minus(q.times(d2));
|
|
1502
|
+
n = d2;
|
|
1503
|
+
}
|
|
1504
|
+
d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
|
|
1505
|
+
n0 = n0.plus(d2.times(n1));
|
|
1506
|
+
d0 = d0.plus(d2.times(d1));
|
|
1507
|
+
n0.s = n1.s = x.s;
|
|
1508
|
+
r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
|
|
1509
|
+
Ctor.precision = pr;
|
|
1510
|
+
external = true;
|
|
1511
|
+
return r;
|
|
1512
|
+
};
|
|
1513
|
+
P.toHexadecimal = P.toHex = function(sd, rm) {
|
|
1514
|
+
return toStringBinary(this, 16, sd, rm);
|
|
1515
|
+
};
|
|
1516
|
+
P.toNearest = function(y, rm) {
|
|
1517
|
+
var x = this, Ctor = x.constructor;
|
|
1518
|
+
x = new Ctor(x);
|
|
1519
|
+
if (y == null) {
|
|
1520
|
+
if (!x.d) return x;
|
|
1521
|
+
y = new Ctor(1);
|
|
1522
|
+
rm = Ctor.rounding;
|
|
1523
|
+
} else {
|
|
1524
|
+
y = new Ctor(y);
|
|
1525
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1526
|
+
else checkInt32(rm, 0, 8);
|
|
1527
|
+
if (!x.d) return y.s ? x : y;
|
|
1528
|
+
if (!y.d) {
|
|
1529
|
+
if (y.s) y.s = x.s;
|
|
1530
|
+
return y;
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
if (y.d[0]) {
|
|
1534
|
+
external = false;
|
|
1535
|
+
x = divide(x, y, 0, rm, 1).times(y);
|
|
1536
|
+
external = true;
|
|
1537
|
+
finalise(x);
|
|
1538
|
+
} else {
|
|
1539
|
+
y.s = x.s;
|
|
1540
|
+
x = y;
|
|
1541
|
+
}
|
|
1542
|
+
return x;
|
|
1543
|
+
};
|
|
1544
|
+
P.toNumber = function() {
|
|
1545
|
+
return +this;
|
|
1546
|
+
};
|
|
1547
|
+
P.toOctal = function(sd, rm) {
|
|
1548
|
+
return toStringBinary(this, 8, sd, rm);
|
|
1549
|
+
};
|
|
1550
|
+
P.toPower = P.pow = function(y) {
|
|
1551
|
+
var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
|
|
1552
|
+
if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
|
|
1553
|
+
x = new Ctor(x);
|
|
1554
|
+
if (x.eq(1)) return x;
|
|
1555
|
+
pr = Ctor.precision;
|
|
1556
|
+
rm = Ctor.rounding;
|
|
1557
|
+
if (y.eq(1)) return finalise(x, pr, rm);
|
|
1558
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1559
|
+
if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
|
|
1560
|
+
r = intPow(Ctor, x, k, pr);
|
|
1561
|
+
return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
|
|
1562
|
+
}
|
|
1563
|
+
s = x.s;
|
|
1564
|
+
if (s < 0) {
|
|
1565
|
+
if (e < y.d.length - 1) return new Ctor(NaN);
|
|
1566
|
+
if ((y.d[e] & 1) == 0) s = 1;
|
|
1567
|
+
if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
|
|
1568
|
+
x.s = s;
|
|
1569
|
+
return x;
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
k = mathpow(+x, yn);
|
|
1573
|
+
e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
|
|
1574
|
+
if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
|
|
1575
|
+
external = false;
|
|
1576
|
+
Ctor.rounding = x.s = 1;
|
|
1577
|
+
k = Math.min(12, (e + "").length);
|
|
1578
|
+
r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
|
|
1579
|
+
if (r.d) {
|
|
1580
|
+
r = finalise(r, pr + 5, 1);
|
|
1581
|
+
if (checkRoundingDigits(r.d, pr, rm)) {
|
|
1582
|
+
e = pr + 10;
|
|
1583
|
+
r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
|
|
1584
|
+
if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) r = finalise(r, pr + 1, 0);
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
r.s = s;
|
|
1588
|
+
external = true;
|
|
1589
|
+
Ctor.rounding = rm;
|
|
1590
|
+
return finalise(r, pr, rm);
|
|
1591
|
+
};
|
|
1592
|
+
P.toPrecision = function(sd, rm) {
|
|
1593
|
+
var str, x = this, Ctor = x.constructor;
|
|
1594
|
+
if (sd === void 0) str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1595
|
+
else {
|
|
1596
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1597
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1598
|
+
else checkInt32(rm, 0, 8);
|
|
1599
|
+
x = finalise(new Ctor(x), sd, rm);
|
|
1600
|
+
str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
|
|
1601
|
+
}
|
|
1602
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1603
|
+
};
|
|
1604
|
+
P.toSignificantDigits = P.toSD = function(sd, rm) {
|
|
1605
|
+
var x = this, Ctor = x.constructor;
|
|
1606
|
+
if (sd === void 0) {
|
|
1607
|
+
sd = Ctor.precision;
|
|
1608
|
+
rm = Ctor.rounding;
|
|
1609
|
+
} else {
|
|
1610
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1611
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1612
|
+
else checkInt32(rm, 0, 8);
|
|
1613
|
+
}
|
|
1614
|
+
return finalise(new Ctor(x), sd, rm);
|
|
1615
|
+
};
|
|
1616
|
+
P.toString = function() {
|
|
1617
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1618
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1619
|
+
};
|
|
1620
|
+
P.truncated = P.trunc = function() {
|
|
1621
|
+
return finalise(new this.constructor(this), this.e + 1, 1);
|
|
1622
|
+
};
|
|
1623
|
+
P.valueOf = P.toJSON = function() {
|
|
1624
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1625
|
+
return x.isNeg() ? "-" + str : str;
|
|
1626
|
+
};
|
|
1627
|
+
function digitsToString(d) {
|
|
1628
|
+
var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
|
|
1629
|
+
if (indexOfLastWord > 0) {
|
|
1630
|
+
str += w;
|
|
1631
|
+
for (i = 1; i < indexOfLastWord; i++) {
|
|
1632
|
+
ws = d[i] + "";
|
|
1633
|
+
k = LOG_BASE - ws.length;
|
|
1634
|
+
if (k) str += getZeroString(k);
|
|
1635
|
+
str += ws;
|
|
1636
|
+
}
|
|
1637
|
+
w = d[i];
|
|
1638
|
+
ws = w + "";
|
|
1639
|
+
k = LOG_BASE - ws.length;
|
|
1640
|
+
if (k) str += getZeroString(k);
|
|
1641
|
+
} else if (w === 0) return "0";
|
|
1642
|
+
for (; w % 10 === 0;) w /= 10;
|
|
1643
|
+
return str + w;
|
|
1644
|
+
}
|
|
1645
|
+
function checkInt32(i, min, max) {
|
|
1646
|
+
if (i !== ~~i || i < min || i > max) throw Error(invalidArgument + i);
|
|
1647
|
+
}
|
|
1648
|
+
function checkRoundingDigits(d, i, rm, repeating) {
|
|
1649
|
+
var di, k = d[0], r, rd;
|
|
1650
|
+
for (; k >= 10; k /= 10) --i;
|
|
1651
|
+
if (--i < 0) {
|
|
1652
|
+
i += LOG_BASE;
|
|
1653
|
+
di = 0;
|
|
1654
|
+
} else {
|
|
1655
|
+
di = Math.ceil((i + 1) / LOG_BASE);
|
|
1656
|
+
i %= LOG_BASE;
|
|
1657
|
+
}
|
|
1658
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1659
|
+
rd = d[di] % k | 0;
|
|
1660
|
+
if (repeating == null) {
|
|
1661
|
+
if (i < 3) {
|
|
1662
|
+
if (i == 0) rd = rd / 100 | 0;
|
|
1663
|
+
else if (i == 1) rd = rd / 10 | 0;
|
|
1664
|
+
r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 5e4 || rd == 0;
|
|
1665
|
+
} else r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
|
|
1666
|
+
} else if (i < 4) {
|
|
1667
|
+
if (i == 0) rd = rd / 1e3 | 0;
|
|
1668
|
+
else if (i == 1) rd = rd / 100 | 0;
|
|
1669
|
+
else if (i == 2) rd = rd / 10 | 0;
|
|
1670
|
+
r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
|
|
1671
|
+
} else r = ((repeating || rm < 4) && rd + 1 == k || !repeating && rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 1e3 | 0) == mathpow(10, i - 3) - 1;
|
|
1672
|
+
return r;
|
|
1673
|
+
}
|
|
1674
|
+
function convertBase(str, baseIn, baseOut) {
|
|
1675
|
+
var j, arr = [0], arrL, i = 0, strL = str.length;
|
|
1676
|
+
for (; i < strL;) {
|
|
1677
|
+
for (arrL = arr.length; arrL--;) arr[arrL] *= baseIn;
|
|
1678
|
+
arr[0] += NUMERALS.indexOf(str.charAt(i++));
|
|
1679
|
+
for (j = 0; j < arr.length; j++) if (arr[j] > baseOut - 1) {
|
|
1680
|
+
if (arr[j + 1] === void 0) arr[j + 1] = 0;
|
|
1681
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
1682
|
+
arr[j] %= baseOut;
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
return arr.reverse();
|
|
1686
|
+
}
|
|
1687
|
+
function cosine(Ctor, x) {
|
|
1688
|
+
var k, len, y;
|
|
1689
|
+
if (x.isZero()) return x;
|
|
1690
|
+
len = x.d.length;
|
|
1691
|
+
if (len < 32) {
|
|
1692
|
+
k = Math.ceil(len / 3);
|
|
1693
|
+
y = (1 / tinyPow(4, k)).toString();
|
|
1694
|
+
} else {
|
|
1695
|
+
k = 16;
|
|
1696
|
+
y = "2.3283064365386962890625e-10";
|
|
1697
|
+
}
|
|
1698
|
+
Ctor.precision += k;
|
|
1699
|
+
x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
|
|
1700
|
+
for (var i = k; i--;) {
|
|
1701
|
+
var cos2x = x.times(x);
|
|
1702
|
+
x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
|
|
1703
|
+
}
|
|
1704
|
+
Ctor.precision -= k;
|
|
1705
|
+
return x;
|
|
1706
|
+
}
|
|
1707
|
+
var divide = (function() {
|
|
1708
|
+
function multiplyInteger(x, k, base) {
|
|
1709
|
+
var temp, carry = 0, i = x.length;
|
|
1710
|
+
for (x = x.slice(); i--;) {
|
|
1711
|
+
temp = x[i] * k + carry;
|
|
1712
|
+
x[i] = temp % base | 0;
|
|
1713
|
+
carry = temp / base | 0;
|
|
1714
|
+
}
|
|
1715
|
+
if (carry) x.unshift(carry);
|
|
1716
|
+
return x;
|
|
1717
|
+
}
|
|
1718
|
+
function compare(a, b, aL, bL) {
|
|
1719
|
+
var i, r;
|
|
1720
|
+
if (aL != bL) r = aL > bL ? 1 : -1;
|
|
1721
|
+
else for (i = r = 0; i < aL; i++) if (a[i] != b[i]) {
|
|
1722
|
+
r = a[i] > b[i] ? 1 : -1;
|
|
1723
|
+
break;
|
|
1724
|
+
}
|
|
1725
|
+
return r;
|
|
1726
|
+
}
|
|
1727
|
+
function subtract(a, b, aL, base) {
|
|
1728
|
+
var i = 0;
|
|
1729
|
+
for (; aL--;) {
|
|
1730
|
+
a[aL] -= i;
|
|
1731
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
|
1732
|
+
a[aL] = i * base + a[aL] - b[aL];
|
|
1733
|
+
}
|
|
1734
|
+
for (; !a[0] && a.length > 1;) a.shift();
|
|
1735
|
+
}
|
|
1736
|
+
return function(x, y, pr, rm, dp, base) {
|
|
1737
|
+
var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz, Ctor = x.constructor, sign = x.s == y.s ? 1 : -1, xd = x.d, yd = y.d;
|
|
1738
|
+
if (!xd || !xd[0] || !yd || !yd[0]) return new Ctor(!x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0);
|
|
1739
|
+
if (base) {
|
|
1740
|
+
logBase = 1;
|
|
1741
|
+
e = x.e - y.e;
|
|
1742
|
+
} else {
|
|
1743
|
+
base = BASE;
|
|
1744
|
+
logBase = LOG_BASE;
|
|
1745
|
+
e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
|
|
1746
|
+
}
|
|
1747
|
+
yL = yd.length;
|
|
1748
|
+
xL = xd.length;
|
|
1749
|
+
q = new Ctor(sign);
|
|
1750
|
+
qd = q.d = [];
|
|
1751
|
+
for (i = 0; yd[i] == (xd[i] || 0); i++);
|
|
1752
|
+
if (yd[i] > (xd[i] || 0)) e--;
|
|
1753
|
+
if (pr == null) {
|
|
1754
|
+
sd = pr = Ctor.precision;
|
|
1755
|
+
rm = Ctor.rounding;
|
|
1756
|
+
} else if (dp) sd = pr + (x.e - y.e) + 1;
|
|
1757
|
+
else sd = pr;
|
|
1758
|
+
if (sd < 0) {
|
|
1759
|
+
qd.push(1);
|
|
1760
|
+
more = true;
|
|
1761
|
+
} else {
|
|
1762
|
+
sd = sd / logBase + 2 | 0;
|
|
1763
|
+
i = 0;
|
|
1764
|
+
if (yL == 1) {
|
|
1765
|
+
k = 0;
|
|
1766
|
+
yd = yd[0];
|
|
1767
|
+
sd++;
|
|
1768
|
+
for (; (i < xL || k) && sd--; i++) {
|
|
1769
|
+
t = k * base + (xd[i] || 0);
|
|
1770
|
+
qd[i] = t / yd | 0;
|
|
1771
|
+
k = t % yd | 0;
|
|
1772
|
+
}
|
|
1773
|
+
more = k || i < xL;
|
|
1774
|
+
} else {
|
|
1775
|
+
k = base / (yd[0] + 1) | 0;
|
|
1776
|
+
if (k > 1) {
|
|
1777
|
+
yd = multiplyInteger(yd, k, base);
|
|
1778
|
+
xd = multiplyInteger(xd, k, base);
|
|
1779
|
+
yL = yd.length;
|
|
1780
|
+
xL = xd.length;
|
|
1781
|
+
}
|
|
1782
|
+
xi = yL;
|
|
1783
|
+
rem = xd.slice(0, yL);
|
|
1784
|
+
remL = rem.length;
|
|
1785
|
+
for (; remL < yL;) rem[remL++] = 0;
|
|
1786
|
+
yz = yd.slice();
|
|
1787
|
+
yz.unshift(0);
|
|
1788
|
+
yd0 = yd[0];
|
|
1789
|
+
if (yd[1] >= base / 2) ++yd0;
|
|
1790
|
+
do {
|
|
1791
|
+
k = 0;
|
|
1792
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1793
|
+
if (cmp < 0) {
|
|
1794
|
+
rem0 = rem[0];
|
|
1795
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
|
1796
|
+
k = rem0 / yd0 | 0;
|
|
1797
|
+
if (k > 1) {
|
|
1798
|
+
if (k >= base) k = base - 1;
|
|
1799
|
+
prod = multiplyInteger(yd, k, base);
|
|
1800
|
+
prodL = prod.length;
|
|
1801
|
+
remL = rem.length;
|
|
1802
|
+
cmp = compare(prod, rem, prodL, remL);
|
|
1803
|
+
if (cmp == 1) {
|
|
1804
|
+
k--;
|
|
1805
|
+
subtract(prod, yL < prodL ? yz : yd, prodL, base);
|
|
1806
|
+
}
|
|
1807
|
+
} else {
|
|
1808
|
+
if (k == 0) cmp = k = 1;
|
|
1809
|
+
prod = yd.slice();
|
|
1810
|
+
}
|
|
1811
|
+
prodL = prod.length;
|
|
1812
|
+
if (prodL < remL) prod.unshift(0);
|
|
1813
|
+
subtract(rem, prod, remL, base);
|
|
1814
|
+
if (cmp == -1) {
|
|
1815
|
+
remL = rem.length;
|
|
1816
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1817
|
+
if (cmp < 1) {
|
|
1818
|
+
k++;
|
|
1819
|
+
subtract(rem, yL < remL ? yz : yd, remL, base);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
remL = rem.length;
|
|
1823
|
+
} else if (cmp === 0) {
|
|
1824
|
+
k++;
|
|
1825
|
+
rem = [0];
|
|
1826
|
+
}
|
|
1827
|
+
qd[i++] = k;
|
|
1828
|
+
if (cmp && rem[0]) rem[remL++] = xd[xi] || 0;
|
|
1829
|
+
else {
|
|
1830
|
+
rem = [xd[xi]];
|
|
1831
|
+
remL = 1;
|
|
1832
|
+
}
|
|
1833
|
+
} while ((xi++ < xL || rem[0] !== void 0) && sd--);
|
|
1834
|
+
more = rem[0] !== void 0;
|
|
1835
|
+
}
|
|
1836
|
+
if (!qd[0]) qd.shift();
|
|
1837
|
+
}
|
|
1838
|
+
if (logBase == 1) {
|
|
1839
|
+
q.e = e;
|
|
1840
|
+
inexact = more;
|
|
1841
|
+
} else {
|
|
1842
|
+
for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
|
|
1843
|
+
q.e = i + e * logBase - 1;
|
|
1844
|
+
finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
|
|
1845
|
+
}
|
|
1846
|
+
return q;
|
|
1847
|
+
};
|
|
1848
|
+
})();
|
|
1849
|
+
function finalise(x, sd, rm, isTruncated) {
|
|
1850
|
+
var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
|
|
1851
|
+
out: if (sd != null) {
|
|
1852
|
+
xd = x.d;
|
|
1853
|
+
if (!xd) return x;
|
|
1854
|
+
for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
|
|
1855
|
+
i = sd - digits;
|
|
1856
|
+
if (i < 0) {
|
|
1857
|
+
i += LOG_BASE;
|
|
1858
|
+
j = sd;
|
|
1859
|
+
w = xd[xdi = 0];
|
|
1860
|
+
rd = w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1861
|
+
} else {
|
|
1862
|
+
xdi = Math.ceil((i + 1) / LOG_BASE);
|
|
1863
|
+
k = xd.length;
|
|
1864
|
+
if (xdi >= k) {
|
|
1865
|
+
if (isTruncated) {
|
|
1866
|
+
for (; k++ <= xdi;) xd.push(0);
|
|
1867
|
+
w = rd = 0;
|
|
1868
|
+
digits = 1;
|
|
1869
|
+
i %= LOG_BASE;
|
|
1870
|
+
j = i - LOG_BASE + 1;
|
|
1871
|
+
} else break out;
|
|
1872
|
+
} else {
|
|
1873
|
+
w = k = xd[xdi];
|
|
1874
|
+
for (digits = 1; k >= 10; k /= 10) digits++;
|
|
1875
|
+
i %= LOG_BASE;
|
|
1876
|
+
j = i - LOG_BASE + digits;
|
|
1877
|
+
rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
|
|
1881
|
+
roundUp = rm < 4 ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && (i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
|
|
1882
|
+
if (sd < 1 || !xd[0]) {
|
|
1883
|
+
xd.length = 0;
|
|
1884
|
+
if (roundUp) {
|
|
1885
|
+
sd -= x.e + 1;
|
|
1886
|
+
xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
|
|
1887
|
+
x.e = -sd || 0;
|
|
1888
|
+
} else xd[0] = x.e = 0;
|
|
1889
|
+
return x;
|
|
1890
|
+
}
|
|
1891
|
+
if (i == 0) {
|
|
1892
|
+
xd.length = xdi;
|
|
1893
|
+
k = 1;
|
|
1894
|
+
xdi--;
|
|
1895
|
+
} else {
|
|
1896
|
+
xd.length = xdi + 1;
|
|
1897
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1898
|
+
xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
|
|
1899
|
+
}
|
|
1900
|
+
if (roundUp) for (;;) if (xdi == 0) {
|
|
1901
|
+
for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
|
|
1902
|
+
j = xd[0] += k;
|
|
1903
|
+
for (k = 1; j >= 10; j /= 10) k++;
|
|
1904
|
+
if (i != k) {
|
|
1905
|
+
x.e++;
|
|
1906
|
+
if (xd[0] == BASE) xd[0] = 1;
|
|
1907
|
+
}
|
|
1908
|
+
break;
|
|
1909
|
+
} else {
|
|
1910
|
+
xd[xdi] += k;
|
|
1911
|
+
if (xd[xdi] != BASE) break;
|
|
1912
|
+
xd[xdi--] = 0;
|
|
1913
|
+
k = 1;
|
|
1914
|
+
}
|
|
1915
|
+
for (i = xd.length; xd[--i] === 0;) xd.pop();
|
|
1916
|
+
}
|
|
1917
|
+
if (external) {
|
|
1918
|
+
if (x.e > Ctor.maxE) {
|
|
1919
|
+
x.d = null;
|
|
1920
|
+
x.e = NaN;
|
|
1921
|
+
} else if (x.e < Ctor.minE) {
|
|
1922
|
+
x.e = 0;
|
|
1923
|
+
x.d = [0];
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
return x;
|
|
1927
|
+
}
|
|
1928
|
+
function finiteToString(x, isExp, sd) {
|
|
1929
|
+
if (!x.isFinite()) return nonFiniteToString(x);
|
|
1930
|
+
var k, e = x.e, str = digitsToString(x.d), len = str.length;
|
|
1931
|
+
if (isExp) {
|
|
1932
|
+
if (sd && (k = sd - len) > 0) str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
|
|
1933
|
+
else if (len > 1) str = str.charAt(0) + "." + str.slice(1);
|
|
1934
|
+
str = str + (x.e < 0 ? "e" : "e+") + x.e;
|
|
1935
|
+
} else if (e < 0) {
|
|
1936
|
+
str = "0." + getZeroString(-e - 1) + str;
|
|
1937
|
+
if (sd && (k = sd - len) > 0) str += getZeroString(k);
|
|
1938
|
+
} else if (e >= len) {
|
|
1939
|
+
str += getZeroString(e + 1 - len);
|
|
1940
|
+
if (sd && (k = sd - e - 1) > 0) str = str + "." + getZeroString(k);
|
|
1941
|
+
} else {
|
|
1942
|
+
if ((k = e + 1) < len) str = str.slice(0, k) + "." + str.slice(k);
|
|
1943
|
+
if (sd && (k = sd - len) > 0) {
|
|
1944
|
+
if (e + 1 === len) str += ".";
|
|
1945
|
+
str += getZeroString(k);
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
return str;
|
|
1949
|
+
}
|
|
1950
|
+
function getBase10Exponent(digits, e) {
|
|
1951
|
+
var w = digits[0];
|
|
1952
|
+
for (e *= LOG_BASE; w >= 10; w /= 10) e++;
|
|
1953
|
+
return e;
|
|
1954
|
+
}
|
|
1955
|
+
function getLn10(Ctor, sd, pr) {
|
|
1956
|
+
if (sd > LN10_PRECISION) {
|
|
1957
|
+
external = true;
|
|
1958
|
+
if (pr) Ctor.precision = pr;
|
|
1959
|
+
throw Error(precisionLimitExceeded);
|
|
1960
|
+
}
|
|
1961
|
+
return finalise(new Ctor(LN10), sd, 1, true);
|
|
1962
|
+
}
|
|
1963
|
+
function getPi(Ctor, sd, rm) {
|
|
1964
|
+
if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
|
|
1965
|
+
return finalise(new Ctor(PI), sd, rm, true);
|
|
1966
|
+
}
|
|
1967
|
+
function getPrecision(digits) {
|
|
1968
|
+
var w = digits.length - 1, len = w * LOG_BASE + 1;
|
|
1969
|
+
w = digits[w];
|
|
1970
|
+
if (w) {
|
|
1971
|
+
for (; w % 10 == 0; w /= 10) len--;
|
|
1972
|
+
for (w = digits[0]; w >= 10; w /= 10) len++;
|
|
1973
|
+
}
|
|
1974
|
+
return len;
|
|
1975
|
+
}
|
|
1976
|
+
function getZeroString(k) {
|
|
1977
|
+
var zs = "";
|
|
1978
|
+
for (; k--;) zs += "0";
|
|
1979
|
+
return zs;
|
|
1980
|
+
}
|
|
1981
|
+
function intPow(Ctor, x, n, pr) {
|
|
1982
|
+
var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
|
|
1983
|
+
external = false;
|
|
1984
|
+
for (;;) {
|
|
1985
|
+
if (n % 2) {
|
|
1986
|
+
r = r.times(x);
|
|
1987
|
+
if (truncate(r.d, k)) isTruncated = true;
|
|
1988
|
+
}
|
|
1989
|
+
n = mathfloor(n / 2);
|
|
1990
|
+
if (n === 0) {
|
|
1991
|
+
n = r.d.length - 1;
|
|
1992
|
+
if (isTruncated && r.d[n] === 0) ++r.d[n];
|
|
1993
|
+
break;
|
|
1994
|
+
}
|
|
1995
|
+
x = x.times(x);
|
|
1996
|
+
truncate(x.d, k);
|
|
1997
|
+
}
|
|
1998
|
+
external = true;
|
|
1999
|
+
return r;
|
|
2000
|
+
}
|
|
2001
|
+
function isOdd(n) {
|
|
2002
|
+
return n.d[n.d.length - 1] & 1;
|
|
2003
|
+
}
|
|
2004
|
+
function maxOrMin(Ctor, args, n) {
|
|
2005
|
+
var k, y, x = new Ctor(args[0]), i = 0;
|
|
2006
|
+
for (; ++i < args.length;) {
|
|
2007
|
+
y = new Ctor(args[i]);
|
|
2008
|
+
if (!y.s) {
|
|
2009
|
+
x = y;
|
|
2010
|
+
break;
|
|
2011
|
+
}
|
|
2012
|
+
k = x.cmp(y);
|
|
2013
|
+
if (k === n || k === 0 && x.s === n) x = y;
|
|
2014
|
+
}
|
|
2015
|
+
return x;
|
|
2016
|
+
}
|
|
2017
|
+
function naturalExponential(x, sd) {
|
|
2018
|
+
var denominator, guard, j, pow, sum, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
2019
|
+
if (!x.d || !x.d[0] || x.e > 17) return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : NaN);
|
|
2020
|
+
if (sd == null) {
|
|
2021
|
+
external = false;
|
|
2022
|
+
wpr = pr;
|
|
2023
|
+
} else wpr = sd;
|
|
2024
|
+
t = new Ctor(.03125);
|
|
2025
|
+
while (x.e > -2) {
|
|
2026
|
+
x = x.times(t);
|
|
2027
|
+
k += 5;
|
|
2028
|
+
}
|
|
2029
|
+
guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
|
|
2030
|
+
wpr += guard;
|
|
2031
|
+
denominator = pow = sum = new Ctor(1);
|
|
2032
|
+
Ctor.precision = wpr;
|
|
2033
|
+
for (;;) {
|
|
2034
|
+
pow = finalise(pow.times(x), wpr, 1);
|
|
2035
|
+
denominator = denominator.times(++i);
|
|
2036
|
+
t = sum.plus(divide(pow, denominator, wpr, 1));
|
|
2037
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|
2038
|
+
j = k;
|
|
2039
|
+
while (j--) sum = finalise(sum.times(sum), wpr, 1);
|
|
2040
|
+
if (sd == null) {
|
|
2041
|
+
if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
|
|
2042
|
+
Ctor.precision = wpr += 10;
|
|
2043
|
+
denominator = pow = t = new Ctor(1);
|
|
2044
|
+
i = 0;
|
|
2045
|
+
rep++;
|
|
2046
|
+
} else return finalise(sum, Ctor.precision = pr, rm, external = true);
|
|
2047
|
+
} else {
|
|
2048
|
+
Ctor.precision = pr;
|
|
2049
|
+
return sum;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
sum = t;
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
function naturalLogarithm(y, sd) {
|
|
2056
|
+
var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2, n = 1, guard = 10, x = y, xd = x.d, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
2057
|
+
if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
|
|
2058
|
+
if (sd == null) {
|
|
2059
|
+
external = false;
|
|
2060
|
+
wpr = pr;
|
|
2061
|
+
} else wpr = sd;
|
|
2062
|
+
Ctor.precision = wpr += guard;
|
|
2063
|
+
c = digitsToString(xd);
|
|
2064
|
+
c0 = c.charAt(0);
|
|
2065
|
+
if (Math.abs(e = x.e) < 15e14) {
|
|
2066
|
+
while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
|
|
2067
|
+
x = x.times(y);
|
|
2068
|
+
c = digitsToString(x.d);
|
|
2069
|
+
c0 = c.charAt(0);
|
|
2070
|
+
n++;
|
|
2071
|
+
}
|
|
2072
|
+
e = x.e;
|
|
2073
|
+
if (c0 > 1) {
|
|
2074
|
+
x = new Ctor("0." + c);
|
|
2075
|
+
e++;
|
|
2076
|
+
} else x = new Ctor(c0 + "." + c.slice(1));
|
|
2077
|
+
} else {
|
|
2078
|
+
t = getLn10(Ctor, wpr + 2, pr).times(e + "");
|
|
2079
|
+
x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
|
|
2080
|
+
Ctor.precision = pr;
|
|
2081
|
+
return sd == null ? finalise(x, pr, rm, external = true) : x;
|
|
2082
|
+
}
|
|
2083
|
+
x1 = x;
|
|
2084
|
+
sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
|
|
2085
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
2086
|
+
denominator = 3;
|
|
2087
|
+
for (;;) {
|
|
2088
|
+
numerator = finalise(numerator.times(x2), wpr, 1);
|
|
2089
|
+
t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1));
|
|
2090
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|
2091
|
+
sum = sum.times(2);
|
|
2092
|
+
if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
|
|
2093
|
+
sum = divide(sum, new Ctor(n), wpr, 1);
|
|
2094
|
+
if (sd == null) {
|
|
2095
|
+
if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
|
|
2096
|
+
Ctor.precision = wpr += guard;
|
|
2097
|
+
t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
|
|
2098
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
2099
|
+
denominator = rep = 1;
|
|
2100
|
+
} else return finalise(sum, Ctor.precision = pr, rm, external = true);
|
|
2101
|
+
} else {
|
|
2102
|
+
Ctor.precision = pr;
|
|
2103
|
+
return sum;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
sum = t;
|
|
2107
|
+
denominator += 2;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
function nonFiniteToString(x) {
|
|
2111
|
+
return String(x.s * x.s / 0);
|
|
2112
|
+
}
|
|
2113
|
+
function parseDecimal(x, str) {
|
|
2114
|
+
var e, i, len;
|
|
2115
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
2116
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
2117
|
+
if (e < 0) e = i;
|
|
2118
|
+
e += +str.slice(i + 1);
|
|
2119
|
+
str = str.substring(0, i);
|
|
2120
|
+
} else if (e < 0) e = str.length;
|
|
2121
|
+
for (i = 0; str.charCodeAt(i) === 48; i++);
|
|
2122
|
+
for (len = str.length; str.charCodeAt(len - 1) === 48; --len);
|
|
2123
|
+
str = str.slice(i, len);
|
|
2124
|
+
if (str) {
|
|
2125
|
+
len -= i;
|
|
2126
|
+
x.e = e = e - i - 1;
|
|
2127
|
+
x.d = [];
|
|
2128
|
+
i = (e + 1) % LOG_BASE;
|
|
2129
|
+
if (e < 0) i += LOG_BASE;
|
|
2130
|
+
if (i < len) {
|
|
2131
|
+
if (i) x.d.push(+str.slice(0, i));
|
|
2132
|
+
for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE));
|
|
2133
|
+
str = str.slice(i);
|
|
2134
|
+
i = LOG_BASE - str.length;
|
|
2135
|
+
} else i -= len;
|
|
2136
|
+
for (; i--;) str += "0";
|
|
2137
|
+
x.d.push(+str);
|
|
2138
|
+
if (external) {
|
|
2139
|
+
if (x.e > x.constructor.maxE) {
|
|
2140
|
+
x.d = null;
|
|
2141
|
+
x.e = NaN;
|
|
2142
|
+
} else if (x.e < x.constructor.minE) {
|
|
2143
|
+
x.e = 0;
|
|
2144
|
+
x.d = [0];
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
} else {
|
|
2148
|
+
x.e = 0;
|
|
2149
|
+
x.d = [0];
|
|
2150
|
+
}
|
|
2151
|
+
return x;
|
|
2152
|
+
}
|
|
2153
|
+
function parseOther(x, str) {
|
|
2154
|
+
var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
|
|
2155
|
+
if (str.indexOf("_") > -1) {
|
|
2156
|
+
str = str.replace(/(\d)_(?=\d)/g, "$1");
|
|
2157
|
+
if (isDecimal.test(str)) return parseDecimal(x, str);
|
|
2158
|
+
} else if (str === "Infinity" || str === "NaN") {
|
|
2159
|
+
if (!+str) x.s = NaN;
|
|
2160
|
+
x.e = NaN;
|
|
2161
|
+
x.d = null;
|
|
2162
|
+
return x;
|
|
2163
|
+
}
|
|
2164
|
+
if (isHex.test(str)) {
|
|
2165
|
+
base = 16;
|
|
2166
|
+
str = str.toLowerCase();
|
|
2167
|
+
} else if (isBinary.test(str)) base = 2;
|
|
2168
|
+
else if (isOctal.test(str)) base = 8;
|
|
2169
|
+
else throw Error(invalidArgument + str);
|
|
2170
|
+
i = str.search(/p/i);
|
|
2171
|
+
if (i > 0) {
|
|
2172
|
+
p = +str.slice(i + 1);
|
|
2173
|
+
str = str.substring(2, i);
|
|
2174
|
+
} else str = str.slice(2);
|
|
2175
|
+
i = str.indexOf(".");
|
|
2176
|
+
isFloat = i >= 0;
|
|
2177
|
+
Ctor = x.constructor;
|
|
2178
|
+
if (isFloat) {
|
|
2179
|
+
str = str.replace(".", "");
|
|
2180
|
+
len = str.length;
|
|
2181
|
+
i = len - i;
|
|
2182
|
+
divisor = intPow(Ctor, new Ctor(base), i, i * 2);
|
|
2183
|
+
}
|
|
2184
|
+
xd = convertBase(str, base, BASE);
|
|
2185
|
+
xe = xd.length - 1;
|
|
2186
|
+
for (i = xe; xd[i] === 0; --i) xd.pop();
|
|
2187
|
+
if (i < 0) return new Ctor(x.s * 0);
|
|
2188
|
+
x.e = getBase10Exponent(xd, xe);
|
|
2189
|
+
x.d = xd;
|
|
2190
|
+
external = false;
|
|
2191
|
+
if (isFloat) x = divide(x, divisor, len * 4);
|
|
2192
|
+
if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
|
|
2193
|
+
external = true;
|
|
2194
|
+
return x;
|
|
2195
|
+
}
|
|
2196
|
+
function sine(Ctor, x) {
|
|
2197
|
+
var k, len = x.d.length;
|
|
2198
|
+
if (len < 3) return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
|
|
2199
|
+
k = 1.4 * Math.sqrt(len);
|
|
2200
|
+
k = k > 16 ? 16 : k | 0;
|
|
2201
|
+
x = x.times(1 / tinyPow(5, k));
|
|
2202
|
+
x = taylorSeries(Ctor, 2, x, x);
|
|
2203
|
+
var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
2204
|
+
for (; k--;) {
|
|
2205
|
+
sin2_x = x.times(x);
|
|
2206
|
+
x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
|
|
2207
|
+
}
|
|
2208
|
+
return x;
|
|
2209
|
+
}
|
|
2210
|
+
function taylorSeries(Ctor, n, x, y, isHyperbolic) {
|
|
2211
|
+
var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
|
|
2212
|
+
external = false;
|
|
2213
|
+
x2 = x.times(x);
|
|
2214
|
+
u = new Ctor(y);
|
|
2215
|
+
for (;;) {
|
|
2216
|
+
t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
2217
|
+
u = isHyperbolic ? y.plus(t) : y.minus(t);
|
|
2218
|
+
y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
2219
|
+
t = u.plus(y);
|
|
2220
|
+
if (t.d[k] !== void 0) {
|
|
2221
|
+
for (j = k; t.d[j] === u.d[j] && j--;);
|
|
2222
|
+
if (j == -1) break;
|
|
2223
|
+
}
|
|
2224
|
+
j = u;
|
|
2225
|
+
u = y;
|
|
2226
|
+
y = t;
|
|
2227
|
+
t = j;
|
|
2228
|
+
i++;
|
|
2229
|
+
}
|
|
2230
|
+
external = true;
|
|
2231
|
+
t.d.length = k + 1;
|
|
2232
|
+
return t;
|
|
2233
|
+
}
|
|
2234
|
+
function tinyPow(b, e) {
|
|
2235
|
+
var n = b;
|
|
2236
|
+
while (--e) n *= b;
|
|
2237
|
+
return n;
|
|
2238
|
+
}
|
|
2239
|
+
function toLessThanHalfPi(Ctor, x) {
|
|
2240
|
+
var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(.5);
|
|
2241
|
+
x = x.abs();
|
|
2242
|
+
if (x.lte(halfPi)) {
|
|
2243
|
+
quadrant = isNeg ? 4 : 1;
|
|
2244
|
+
return x;
|
|
2245
|
+
}
|
|
2246
|
+
t = x.divToInt(pi);
|
|
2247
|
+
if (t.isZero()) quadrant = isNeg ? 3 : 2;
|
|
2248
|
+
else {
|
|
2249
|
+
x = x.minus(t.times(pi));
|
|
2250
|
+
if (x.lte(halfPi)) {
|
|
2251
|
+
quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
|
|
2252
|
+
return x;
|
|
2253
|
+
}
|
|
2254
|
+
quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
|
|
2255
|
+
}
|
|
2256
|
+
return x.minus(pi).abs();
|
|
2257
|
+
}
|
|
2258
|
+
function toStringBinary(x, baseOut, sd, rm) {
|
|
2259
|
+
var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0;
|
|
2260
|
+
if (isExp) {
|
|
2261
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
2262
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
2263
|
+
else checkInt32(rm, 0, 8);
|
|
2264
|
+
} else {
|
|
2265
|
+
sd = Ctor.precision;
|
|
2266
|
+
rm = Ctor.rounding;
|
|
2267
|
+
}
|
|
2268
|
+
if (!x.isFinite()) str = nonFiniteToString(x);
|
|
2269
|
+
else {
|
|
2270
|
+
str = finiteToString(x);
|
|
2271
|
+
i = str.indexOf(".");
|
|
2272
|
+
if (isExp) {
|
|
2273
|
+
base = 2;
|
|
2274
|
+
if (baseOut == 16) sd = sd * 4 - 3;
|
|
2275
|
+
else if (baseOut == 8) sd = sd * 3 - 2;
|
|
2276
|
+
} else base = baseOut;
|
|
2277
|
+
if (i >= 0) {
|
|
2278
|
+
str = str.replace(".", "");
|
|
2279
|
+
y = new Ctor(1);
|
|
2280
|
+
y.e = str.length - i;
|
|
2281
|
+
y.d = convertBase(finiteToString(y), 10, base);
|
|
2282
|
+
y.e = y.d.length;
|
|
2283
|
+
}
|
|
2284
|
+
xd = convertBase(str, 10, base);
|
|
2285
|
+
e = len = xd.length;
|
|
2286
|
+
for (; xd[--len] == 0;) xd.pop();
|
|
2287
|
+
if (!xd[0]) str = isExp ? "0p+0" : "0";
|
|
2288
|
+
else {
|
|
2289
|
+
if (i < 0) e--;
|
|
2290
|
+
else {
|
|
2291
|
+
x = new Ctor(x);
|
|
2292
|
+
x.d = xd;
|
|
2293
|
+
x.e = e;
|
|
2294
|
+
x = divide(x, y, sd, rm, 0, base);
|
|
2295
|
+
xd = x.d;
|
|
2296
|
+
e = x.e;
|
|
2297
|
+
roundUp = inexact;
|
|
2298
|
+
}
|
|
2299
|
+
i = xd[sd];
|
|
2300
|
+
k = base / 2;
|
|
2301
|
+
roundUp = roundUp || xd[sd + 1] !== void 0;
|
|
2302
|
+
roundUp = rm < 4 ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || rm === (x.s < 0 ? 8 : 7));
|
|
2303
|
+
xd.length = sd;
|
|
2304
|
+
if (roundUp) for (; ++xd[--sd] > base - 1;) {
|
|
2305
|
+
xd[sd] = 0;
|
|
2306
|
+
if (!sd) {
|
|
2307
|
+
++e;
|
|
2308
|
+
xd.unshift(1);
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
for (len = xd.length; !xd[len - 1]; --len);
|
|
2312
|
+
for (i = 0, str = ""; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
2313
|
+
if (isExp) {
|
|
2314
|
+
if (len > 1) {
|
|
2315
|
+
if (baseOut == 16 || baseOut == 8) {
|
|
2316
|
+
i = baseOut == 16 ? 4 : 3;
|
|
2317
|
+
for (--len; len % i; len++) str += "0";
|
|
2318
|
+
xd = convertBase(str, base, baseOut);
|
|
2319
|
+
for (len = xd.length; !xd[len - 1]; --len);
|
|
2320
|
+
for (i = 1, str = "1."; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
2321
|
+
} else str = str.charAt(0) + "." + str.slice(1);
|
|
2322
|
+
}
|
|
2323
|
+
str = str + (e < 0 ? "p" : "p+") + e;
|
|
2324
|
+
} else if (e < 0) {
|
|
2325
|
+
for (; ++e;) str = "0" + str;
|
|
2326
|
+
str = "0." + str;
|
|
2327
|
+
} else if (++e > len) for (e -= len; e--;) str += "0";
|
|
2328
|
+
else if (e < len) str = str.slice(0, e) + "." + str.slice(e);
|
|
2329
|
+
}
|
|
2330
|
+
str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
|
|
2331
|
+
}
|
|
2332
|
+
return x.s < 0 ? "-" + str : str;
|
|
2333
|
+
}
|
|
2334
|
+
function truncate(arr, len) {
|
|
2335
|
+
if (arr.length > len) {
|
|
2336
|
+
arr.length = len;
|
|
2337
|
+
return true;
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
function abs(x) {
|
|
2341
|
+
return new this(x).abs();
|
|
2342
|
+
}
|
|
2343
|
+
function acos(x) {
|
|
2344
|
+
return new this(x).acos();
|
|
2345
|
+
}
|
|
2346
|
+
function acosh(x) {
|
|
2347
|
+
return new this(x).acosh();
|
|
2348
|
+
}
|
|
2349
|
+
function add(x, y) {
|
|
2350
|
+
return new this(x).plus(y);
|
|
2351
|
+
}
|
|
2352
|
+
function asin(x) {
|
|
2353
|
+
return new this(x).asin();
|
|
2354
|
+
}
|
|
2355
|
+
function asinh(x) {
|
|
2356
|
+
return new this(x).asinh();
|
|
2357
|
+
}
|
|
2358
|
+
function atan(x) {
|
|
2359
|
+
return new this(x).atan();
|
|
2360
|
+
}
|
|
2361
|
+
function atanh(x) {
|
|
2362
|
+
return new this(x).atanh();
|
|
2363
|
+
}
|
|
2364
|
+
function atan2(y, x) {
|
|
2365
|
+
y = new this(y);
|
|
2366
|
+
x = new this(x);
|
|
2367
|
+
var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
|
|
2368
|
+
if (!y.s || !x.s) r = new this(NaN);
|
|
2369
|
+
else if (!y.d && !x.d) {
|
|
2370
|
+
r = getPi(this, wpr, 1).times(x.s > 0 ? .25 : .75);
|
|
2371
|
+
r.s = y.s;
|
|
2372
|
+
} else if (!x.d || y.isZero()) {
|
|
2373
|
+
r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
|
|
2374
|
+
r.s = y.s;
|
|
2375
|
+
} else if (!y.d || x.isZero()) {
|
|
2376
|
+
r = getPi(this, wpr, 1).times(.5);
|
|
2377
|
+
r.s = y.s;
|
|
2378
|
+
} else if (x.s < 0) {
|
|
2379
|
+
this.precision = wpr;
|
|
2380
|
+
this.rounding = 1;
|
|
2381
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
2382
|
+
x = getPi(this, wpr, 1);
|
|
2383
|
+
this.precision = pr;
|
|
2384
|
+
this.rounding = rm;
|
|
2385
|
+
r = y.s < 0 ? r.minus(x) : r.plus(x);
|
|
2386
|
+
} else r = this.atan(divide(y, x, wpr, 1));
|
|
2387
|
+
return r;
|
|
2388
|
+
}
|
|
2389
|
+
function cbrt(x) {
|
|
2390
|
+
return new this(x).cbrt();
|
|
2391
|
+
}
|
|
2392
|
+
function ceil(x) {
|
|
2393
|
+
return finalise(x = new this(x), x.e + 1, 2);
|
|
2394
|
+
}
|
|
2395
|
+
function clamp(x, min, max) {
|
|
2396
|
+
return new this(x).clamp(min, max);
|
|
2397
|
+
}
|
|
2398
|
+
function config(obj) {
|
|
2399
|
+
if (!obj || typeof obj !== "object") throw Error(decimalError + "Object expected");
|
|
2400
|
+
var i, p, v, useDefaults = obj.defaults === true, ps = [
|
|
2401
|
+
"precision",
|
|
2402
|
+
1,
|
|
2403
|
+
MAX_DIGITS,
|
|
2404
|
+
"rounding",
|
|
2405
|
+
0,
|
|
2406
|
+
8,
|
|
2407
|
+
"toExpNeg",
|
|
2408
|
+
-EXP_LIMIT,
|
|
2409
|
+
0,
|
|
2410
|
+
"toExpPos",
|
|
2411
|
+
0,
|
|
2412
|
+
EXP_LIMIT,
|
|
2413
|
+
"maxE",
|
|
2414
|
+
0,
|
|
2415
|
+
EXP_LIMIT,
|
|
2416
|
+
"minE",
|
|
2417
|
+
-EXP_LIMIT,
|
|
2418
|
+
0,
|
|
2419
|
+
"modulo",
|
|
2420
|
+
0,
|
|
2421
|
+
9
|
|
2422
|
+
];
|
|
2423
|
+
for (i = 0; i < ps.length; i += 3) {
|
|
2424
|
+
if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
|
|
2425
|
+
if ((v = obj[p]) !== void 0) {
|
|
2426
|
+
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
|
|
2427
|
+
else throw Error(invalidArgument + p + ": " + v);
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
if (p = "crypto", useDefaults) this[p] = DEFAULTS[p];
|
|
2431
|
+
if ((v = obj[p]) !== void 0) {
|
|
2432
|
+
if (v === true || v === false || v === 0 || v === 1) {
|
|
2433
|
+
if (v) {
|
|
2434
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) this[p] = true;
|
|
2435
|
+
else throw Error(cryptoUnavailable);
|
|
2436
|
+
} else this[p] = false;
|
|
2437
|
+
} else throw Error(invalidArgument + p + ": " + v);
|
|
2438
|
+
}
|
|
2439
|
+
return this;
|
|
2440
|
+
}
|
|
2441
|
+
function cos(x) {
|
|
2442
|
+
return new this(x).cos();
|
|
2443
|
+
}
|
|
2444
|
+
function cosh(x) {
|
|
2445
|
+
return new this(x).cosh();
|
|
2446
|
+
}
|
|
2447
|
+
function clone(obj) {
|
|
2448
|
+
var i, p, ps;
|
|
2449
|
+
function Decimal(v) {
|
|
2450
|
+
var e, i, t, x = this;
|
|
2451
|
+
if (!(x instanceof Decimal)) return new Decimal(v);
|
|
2452
|
+
x.constructor = Decimal;
|
|
2453
|
+
if (isDecimalInstance(v)) {
|
|
2454
|
+
x.s = v.s;
|
|
2455
|
+
if (external) {
|
|
2456
|
+
if (!v.d || v.e > Decimal.maxE) {
|
|
2457
|
+
x.e = NaN;
|
|
2458
|
+
x.d = null;
|
|
2459
|
+
} else if (v.e < Decimal.minE) {
|
|
2460
|
+
x.e = 0;
|
|
2461
|
+
x.d = [0];
|
|
2462
|
+
} else {
|
|
2463
|
+
x.e = v.e;
|
|
2464
|
+
x.d = v.d.slice();
|
|
2465
|
+
}
|
|
2466
|
+
} else {
|
|
2467
|
+
x.e = v.e;
|
|
2468
|
+
x.d = v.d ? v.d.slice() : v.d;
|
|
2469
|
+
}
|
|
2470
|
+
return;
|
|
2471
|
+
}
|
|
2472
|
+
t = typeof v;
|
|
2473
|
+
if (t === "number") {
|
|
2474
|
+
if (v === 0) {
|
|
2475
|
+
x.s = 1 / v < 0 ? -1 : 1;
|
|
2476
|
+
x.e = 0;
|
|
2477
|
+
x.d = [0];
|
|
2478
|
+
return;
|
|
2479
|
+
}
|
|
2480
|
+
if (v < 0) {
|
|
2481
|
+
v = -v;
|
|
2482
|
+
x.s = -1;
|
|
2483
|
+
} else x.s = 1;
|
|
2484
|
+
if (v === ~~v && v < 1e7) {
|
|
2485
|
+
for (e = 0, i = v; i >= 10; i /= 10) e++;
|
|
2486
|
+
if (external) {
|
|
2487
|
+
if (e > Decimal.maxE) {
|
|
2488
|
+
x.e = NaN;
|
|
2489
|
+
x.d = null;
|
|
2490
|
+
} else if (e < Decimal.minE) {
|
|
2491
|
+
x.e = 0;
|
|
2492
|
+
x.d = [0];
|
|
2493
|
+
} else {
|
|
2494
|
+
x.e = e;
|
|
2495
|
+
x.d = [v];
|
|
2496
|
+
}
|
|
2497
|
+
} else {
|
|
2498
|
+
x.e = e;
|
|
2499
|
+
x.d = [v];
|
|
2500
|
+
}
|
|
2501
|
+
return;
|
|
2502
|
+
}
|
|
2503
|
+
if (v * 0 !== 0) {
|
|
2504
|
+
if (!v) x.s = NaN;
|
|
2505
|
+
x.e = NaN;
|
|
2506
|
+
x.d = null;
|
|
2507
|
+
return;
|
|
2508
|
+
}
|
|
2509
|
+
return parseDecimal(x, v.toString());
|
|
2510
|
+
}
|
|
2511
|
+
if (t === "string") {
|
|
2512
|
+
if ((i = v.charCodeAt(0)) === 45) {
|
|
2513
|
+
v = v.slice(1);
|
|
2514
|
+
x.s = -1;
|
|
2515
|
+
} else {
|
|
2516
|
+
if (i === 43) v = v.slice(1);
|
|
2517
|
+
x.s = 1;
|
|
2518
|
+
}
|
|
2519
|
+
return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
|
|
2520
|
+
}
|
|
2521
|
+
if (t === "bigint") {
|
|
2522
|
+
if (v < 0) {
|
|
2523
|
+
v = -v;
|
|
2524
|
+
x.s = -1;
|
|
2525
|
+
} else x.s = 1;
|
|
2526
|
+
return parseDecimal(x, v.toString());
|
|
2527
|
+
}
|
|
2528
|
+
throw Error(invalidArgument + v);
|
|
2529
|
+
}
|
|
2530
|
+
Decimal.prototype = P;
|
|
2531
|
+
Decimal.ROUND_UP = 0;
|
|
2532
|
+
Decimal.ROUND_DOWN = 1;
|
|
2533
|
+
Decimal.ROUND_CEIL = 2;
|
|
2534
|
+
Decimal.ROUND_FLOOR = 3;
|
|
2535
|
+
Decimal.ROUND_HALF_UP = 4;
|
|
2536
|
+
Decimal.ROUND_HALF_DOWN = 5;
|
|
2537
|
+
Decimal.ROUND_HALF_EVEN = 6;
|
|
2538
|
+
Decimal.ROUND_HALF_CEIL = 7;
|
|
2539
|
+
Decimal.ROUND_HALF_FLOOR = 8;
|
|
2540
|
+
Decimal.EUCLID = 9;
|
|
2541
|
+
Decimal.config = Decimal.set = config;
|
|
2542
|
+
Decimal.clone = clone;
|
|
2543
|
+
Decimal.isDecimal = isDecimalInstance;
|
|
2544
|
+
Decimal.abs = abs;
|
|
2545
|
+
Decimal.acos = acos;
|
|
2546
|
+
Decimal.acosh = acosh;
|
|
2547
|
+
Decimal.add = add;
|
|
2548
|
+
Decimal.asin = asin;
|
|
2549
|
+
Decimal.asinh = asinh;
|
|
2550
|
+
Decimal.atan = atan;
|
|
2551
|
+
Decimal.atanh = atanh;
|
|
2552
|
+
Decimal.atan2 = atan2;
|
|
2553
|
+
Decimal.cbrt = cbrt;
|
|
2554
|
+
Decimal.ceil = ceil;
|
|
2555
|
+
Decimal.clamp = clamp;
|
|
2556
|
+
Decimal.cos = cos;
|
|
2557
|
+
Decimal.cosh = cosh;
|
|
2558
|
+
Decimal.div = div;
|
|
2559
|
+
Decimal.exp = exp;
|
|
2560
|
+
Decimal.floor = floor;
|
|
2561
|
+
Decimal.hypot = hypot;
|
|
2562
|
+
Decimal.ln = ln;
|
|
2563
|
+
Decimal.log = log;
|
|
2564
|
+
Decimal.log10 = log10;
|
|
2565
|
+
Decimal.log2 = log2;
|
|
2566
|
+
Decimal.max = max;
|
|
2567
|
+
Decimal.min = min;
|
|
2568
|
+
Decimal.mod = mod;
|
|
2569
|
+
Decimal.mul = mul;
|
|
2570
|
+
Decimal.pow = pow;
|
|
2571
|
+
Decimal.random = random;
|
|
2572
|
+
Decimal.round = round;
|
|
2573
|
+
Decimal.sign = sign;
|
|
2574
|
+
Decimal.sin = sin;
|
|
2575
|
+
Decimal.sinh = sinh;
|
|
2576
|
+
Decimal.sqrt = sqrt;
|
|
2577
|
+
Decimal.sub = sub;
|
|
2578
|
+
Decimal.sum = sum;
|
|
2579
|
+
Decimal.tan = tan;
|
|
2580
|
+
Decimal.tanh = tanh;
|
|
2581
|
+
Decimal.trunc = trunc;
|
|
2582
|
+
if (obj === void 0) obj = {};
|
|
2583
|
+
if (obj) {
|
|
2584
|
+
if (obj.defaults !== true) {
|
|
2585
|
+
ps = [
|
|
2586
|
+
"precision",
|
|
2587
|
+
"rounding",
|
|
2588
|
+
"toExpNeg",
|
|
2589
|
+
"toExpPos",
|
|
2590
|
+
"maxE",
|
|
2591
|
+
"minE",
|
|
2592
|
+
"modulo",
|
|
2593
|
+
"crypto"
|
|
2594
|
+
];
|
|
2595
|
+
for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
Decimal.config(obj);
|
|
2599
|
+
return Decimal;
|
|
2600
|
+
}
|
|
2601
|
+
function div(x, y) {
|
|
2602
|
+
return new this(x).div(y);
|
|
2603
|
+
}
|
|
2604
|
+
function exp(x) {
|
|
2605
|
+
return new this(x).exp();
|
|
2606
|
+
}
|
|
2607
|
+
function floor(x) {
|
|
2608
|
+
return finalise(x = new this(x), x.e + 1, 3);
|
|
2609
|
+
}
|
|
2610
|
+
function hypot() {
|
|
2611
|
+
var i, n, t = new this(0);
|
|
2612
|
+
external = false;
|
|
2613
|
+
for (i = 0; i < arguments.length;) {
|
|
2614
|
+
n = new this(arguments[i++]);
|
|
2615
|
+
if (!n.d) {
|
|
2616
|
+
if (n.s) {
|
|
2617
|
+
external = true;
|
|
2618
|
+
return new this(1 / 0);
|
|
2619
|
+
}
|
|
2620
|
+
t = n;
|
|
2621
|
+
} else if (t.d) t = t.plus(n.times(n));
|
|
2622
|
+
}
|
|
2623
|
+
external = true;
|
|
2624
|
+
return t.sqrt();
|
|
2625
|
+
}
|
|
2626
|
+
function isDecimalInstance(obj) {
|
|
2627
|
+
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
|
|
2628
|
+
}
|
|
2629
|
+
function ln(x) {
|
|
2630
|
+
return new this(x).ln();
|
|
2631
|
+
}
|
|
2632
|
+
function log(x, y) {
|
|
2633
|
+
return new this(x).log(y);
|
|
2634
|
+
}
|
|
2635
|
+
function log2(x) {
|
|
2636
|
+
return new this(x).log(2);
|
|
2637
|
+
}
|
|
2638
|
+
function log10(x) {
|
|
2639
|
+
return new this(x).log(10);
|
|
2640
|
+
}
|
|
2641
|
+
function max() {
|
|
2642
|
+
return maxOrMin(this, arguments, -1);
|
|
2643
|
+
}
|
|
2644
|
+
function min() {
|
|
2645
|
+
return maxOrMin(this, arguments, 1);
|
|
2646
|
+
}
|
|
2647
|
+
function mod(x, y) {
|
|
2648
|
+
return new this(x).mod(y);
|
|
2649
|
+
}
|
|
2650
|
+
function mul(x, y) {
|
|
2651
|
+
return new this(x).mul(y);
|
|
2652
|
+
}
|
|
2653
|
+
function pow(x, y) {
|
|
2654
|
+
return new this(x).pow(y);
|
|
2655
|
+
}
|
|
2656
|
+
function random(sd) {
|
|
2657
|
+
var d, e, k, n, i = 0, r = new this(1), rd = [];
|
|
2658
|
+
if (sd === void 0) sd = this.precision;
|
|
2659
|
+
else checkInt32(sd, 1, MAX_DIGITS);
|
|
2660
|
+
k = Math.ceil(sd / LOG_BASE);
|
|
2661
|
+
if (!this.crypto) for (; i < k;) rd[i++] = Math.random() * 1e7 | 0;
|
|
2662
|
+
else if (crypto.getRandomValues) {
|
|
2663
|
+
d = crypto.getRandomValues(new Uint32Array(k));
|
|
2664
|
+
for (; i < k;) {
|
|
2665
|
+
n = d[i];
|
|
2666
|
+
if (n >= 429e7) d[i] = crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0];
|
|
2667
|
+
else rd[i++] = n % 1e7;
|
|
2668
|
+
}
|
|
2669
|
+
} else if (crypto.randomBytes) {
|
|
2670
|
+
d = crypto.randomBytes(k *= 4);
|
|
2671
|
+
for (; i < k;) {
|
|
2672
|
+
n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
|
|
2673
|
+
if (n >= 214e7) crypto.randomBytes(4).copy(d, i);
|
|
2674
|
+
else {
|
|
2675
|
+
rd.push(n % 1e7);
|
|
2676
|
+
i += 4;
|
|
2677
|
+
}
|
|
2678
|
+
}
|
|
2679
|
+
i = k / 4;
|
|
2680
|
+
} else throw Error(cryptoUnavailable);
|
|
2681
|
+
k = rd[--i];
|
|
2682
|
+
sd %= LOG_BASE;
|
|
2683
|
+
if (k && sd) {
|
|
2684
|
+
n = mathpow(10, LOG_BASE - sd);
|
|
2685
|
+
rd[i] = (k / n | 0) * n;
|
|
2686
|
+
}
|
|
2687
|
+
for (; rd[i] === 0; i--) rd.pop();
|
|
2688
|
+
if (i < 0) {
|
|
2689
|
+
e = 0;
|
|
2690
|
+
rd = [0];
|
|
2691
|
+
} else {
|
|
2692
|
+
e = -1;
|
|
2693
|
+
for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
|
|
2694
|
+
for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
|
|
2695
|
+
if (k < LOG_BASE) e -= LOG_BASE - k;
|
|
2696
|
+
}
|
|
2697
|
+
r.e = e;
|
|
2698
|
+
r.d = rd;
|
|
2699
|
+
return r;
|
|
2700
|
+
}
|
|
2701
|
+
function round(x) {
|
|
2702
|
+
return finalise(x = new this(x), x.e + 1, this.rounding);
|
|
2703
|
+
}
|
|
2704
|
+
function sign(x) {
|
|
2705
|
+
x = new this(x);
|
|
2706
|
+
return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
|
|
2707
|
+
}
|
|
2708
|
+
function sin(x) {
|
|
2709
|
+
return new this(x).sin();
|
|
2710
|
+
}
|
|
2711
|
+
function sinh(x) {
|
|
2712
|
+
return new this(x).sinh();
|
|
2713
|
+
}
|
|
2714
|
+
function sqrt(x) {
|
|
2715
|
+
return new this(x).sqrt();
|
|
2716
|
+
}
|
|
2717
|
+
function sub(x, y) {
|
|
2718
|
+
return new this(x).sub(y);
|
|
2719
|
+
}
|
|
2720
|
+
function sum() {
|
|
2721
|
+
var i = 0, args = arguments, x = new this(args[i]);
|
|
2722
|
+
external = false;
|
|
2723
|
+
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
|
|
2724
|
+
external = true;
|
|
2725
|
+
return finalise(x, this.precision, this.rounding);
|
|
2726
|
+
}
|
|
2727
|
+
function tan(x) {
|
|
2728
|
+
return new this(x).tan();
|
|
2729
|
+
}
|
|
2730
|
+
function tanh(x) {
|
|
2731
|
+
return new this(x).tanh();
|
|
2732
|
+
}
|
|
2733
|
+
function trunc(x) {
|
|
2734
|
+
return finalise(x = new this(x), x.e + 1, 1);
|
|
2735
|
+
}
|
|
2736
|
+
P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
|
|
2737
|
+
P[Symbol.toStringTag] = "Decimal";
|
|
2738
|
+
var Decimal = P.constructor = clone(DEFAULTS);
|
|
2739
|
+
LN10 = new Decimal(LN10);
|
|
2740
|
+
PI = new Decimal(PI);
|
|
2741
|
+
// src/math/HVec3.ts
|
|
2742
|
+
/** Mutable decimal vector. Pass strings to preserve more precision than a JS number. */
|
|
2743
|
+
var HVec3 = class HVec3 {
|
|
2744
|
+
x;
|
|
2745
|
+
y;
|
|
2746
|
+
z;
|
|
2747
|
+
constructor(x = 0, y = 0, z = 0) {
|
|
2748
|
+
this.x = new Decimal(x);
|
|
2749
|
+
this.y = new Decimal(y);
|
|
2750
|
+
this.z = new Decimal(z);
|
|
2751
|
+
if (![
|
|
2752
|
+
this.x,
|
|
2753
|
+
this.y,
|
|
2754
|
+
this.z
|
|
2755
|
+
].every((v) => v.isFinite())) throw new RangeError("Vector components must be finite");
|
|
2756
|
+
}
|
|
2757
|
+
static isVec3(v) {
|
|
2758
|
+
return typeof v === "object" && v !== null && [
|
|
2759
|
+
"x",
|
|
2760
|
+
"y",
|
|
2761
|
+
"z"
|
|
2762
|
+
].every((key) => Decimal.isDecimal(v[key]));
|
|
2763
|
+
}
|
|
2764
|
+
get isVec3() {
|
|
2765
|
+
return true;
|
|
2766
|
+
}
|
|
2767
|
+
static get Up() {
|
|
2768
|
+
return new HVec3(0, 1, 0);
|
|
2769
|
+
}
|
|
2770
|
+
static get Down() {
|
|
2771
|
+
return new HVec3(0, -1, 0);
|
|
2772
|
+
}
|
|
2773
|
+
static get UnitX() {
|
|
2774
|
+
return new HVec3(1, 0, 0);
|
|
2775
|
+
}
|
|
2776
|
+
static get UnitY() {
|
|
2777
|
+
return new HVec3(0, 1, 0);
|
|
2778
|
+
}
|
|
2779
|
+
static get UnitZ() {
|
|
2780
|
+
return new HVec3(0, 0, 1);
|
|
2781
|
+
}
|
|
2782
|
+
set(x, y, z) {
|
|
2783
|
+
const v = new HVec3(x, y, z);
|
|
2784
|
+
this.x = v.x;
|
|
2785
|
+
this.y = v.y;
|
|
2786
|
+
this.z = v.z;
|
|
2787
|
+
return this;
|
|
2788
|
+
}
|
|
2789
|
+
setScalar(s) {
|
|
2790
|
+
return this.set(s, s, s);
|
|
2791
|
+
}
|
|
2792
|
+
setComponent(index, value) {
|
|
2793
|
+
if (!Number.isInteger(index) || index < 0 || index > 2) throw new RangeError("Component index must be 0, 1 or 2");
|
|
2794
|
+
const component = new Decimal(value);
|
|
2795
|
+
if (!component.isFinite()) throw new RangeError("Component must be finite");
|
|
2796
|
+
this[[
|
|
2797
|
+
"x",
|
|
2798
|
+
"y",
|
|
2799
|
+
"z"
|
|
2800
|
+
][index]] = component;
|
|
2801
|
+
return this;
|
|
2802
|
+
}
|
|
2803
|
+
getComponent(index) {
|
|
2804
|
+
if (!Number.isInteger(index) || index < 0 || index > 2) throw new RangeError("Component index must be 0, 1 or 2");
|
|
2805
|
+
return this[[
|
|
2806
|
+
"x",
|
|
2807
|
+
"y",
|
|
2808
|
+
"z"
|
|
2809
|
+
][index]];
|
|
2810
|
+
}
|
|
2811
|
+
copy(v) {
|
|
2812
|
+
return this.set(v.x, v.y, v.z);
|
|
2813
|
+
}
|
|
2814
|
+
clone() {
|
|
2815
|
+
return new HVec3(this.x, this.y, this.z);
|
|
2816
|
+
}
|
|
2817
|
+
add(v, w) {
|
|
2818
|
+
return w ? this.addVecs(v, w) : this.addVecs(this, v);
|
|
2819
|
+
}
|
|
2820
|
+
addVecs(a, b) {
|
|
2821
|
+
return this.set(a.x.add(b.x), a.y.add(b.y), a.z.add(b.z));
|
|
2822
|
+
}
|
|
2823
|
+
sub(v, w) {
|
|
2824
|
+
return w ? this.subVecs(v, w) : this.subVecs(this, v);
|
|
2825
|
+
}
|
|
2826
|
+
subVecs(a, b) {
|
|
2827
|
+
return this.set(a.x.sub(b.x), a.y.sub(b.y), a.z.sub(b.z));
|
|
2828
|
+
}
|
|
2829
|
+
multiplyScalar(s) {
|
|
2830
|
+
return this.set(this.x.mul(s), this.y.mul(s), this.z.mul(s));
|
|
2831
|
+
}
|
|
2832
|
+
divideScalar(s) {
|
|
2833
|
+
if (new Decimal(s).isZero()) throw new RangeError("Division by zero");
|
|
2834
|
+
return this.set(this.x.div(s), this.y.div(s), this.z.div(s));
|
|
2835
|
+
}
|
|
2836
|
+
cross(v, w) {
|
|
2837
|
+
return w ? this.crossVecs(v, w) : this.crossVecs(this, v);
|
|
2838
|
+
}
|
|
2839
|
+
crossVecs(a, b) {
|
|
2840
|
+
return this.set(a.y.mul(b.z).sub(a.z.mul(b.y)), a.z.mul(b.x).sub(a.x.mul(b.z)), a.x.mul(b.y).sub(a.y.mul(b.x)));
|
|
2841
|
+
}
|
|
2842
|
+
dot(v) {
|
|
2843
|
+
return this.x.mul(v.x).add(this.y.mul(v.y)).add(this.z.mul(v.z));
|
|
2844
|
+
}
|
|
2845
|
+
lengthSq() {
|
|
2846
|
+
return this.dot(this);
|
|
2847
|
+
}
|
|
2848
|
+
length() {
|
|
2849
|
+
return this.lengthSq().sqrt();
|
|
2850
|
+
}
|
|
2851
|
+
normalize() {
|
|
2852
|
+
const length = this.length();
|
|
2853
|
+
return length.isZero() ? this : this.divideScalar(length);
|
|
2854
|
+
}
|
|
2855
|
+
lerp(v, alpha) {
|
|
2856
|
+
return this.set(this.x.add(v.x.sub(this.x).mul(alpha)), this.y.add(v.y.sub(this.y).mul(alpha)), this.z.add(v.z.sub(this.z).mul(alpha)));
|
|
2857
|
+
}
|
|
2858
|
+
distanceTo(v) {
|
|
2859
|
+
return this.clone().sub(v).length();
|
|
2860
|
+
}
|
|
2861
|
+
equals(v) {
|
|
2862
|
+
return this.x.eq(v.x) && this.y.eq(v.y) && this.z.eq(v.z);
|
|
2863
|
+
}
|
|
2864
|
+
toArray() {
|
|
2865
|
+
return [
|
|
2866
|
+
this.x,
|
|
2867
|
+
this.y,
|
|
2868
|
+
this.z
|
|
2869
|
+
];
|
|
2870
|
+
}
|
|
2871
|
+
toJSON() {
|
|
2872
|
+
return this.toArray().map((v) => v.toString());
|
|
2873
|
+
}
|
|
2874
|
+
};
|
|
2875
|
+
// src/csg/polygon.ts
|
|
2876
|
+
var Polygon = class Polygon {
|
|
2877
|
+
w = new Decimal(0);
|
|
2878
|
+
normal = new HVec3();
|
|
2879
|
+
vertices;
|
|
2880
|
+
epsilon;
|
|
2881
|
+
constructor(vertices = [], epsilon = "1e-15") {
|
|
2882
|
+
this.epsilon = new Decimal(epsilon);
|
|
2883
|
+
if (!this.epsilon.isFinite() || this.epsilon.isNegative()) throw new RangeError("Invalid CSG tolerance");
|
|
2884
|
+
this.vertices = vertices.map((v) => v.clone());
|
|
2885
|
+
if (vertices.length) {
|
|
2886
|
+
this.calcPlane();
|
|
2887
|
+
if (this.vertices.some((v) => this.normal.dot(v).sub(this.w).abs().gt(this.epsilon))) throw new RangeError("CSG polygon must be planar");
|
|
2888
|
+
for (let i = 0; i < this.vertices.length; i++) {
|
|
2889
|
+
const a = this.vertices[i], b = this.vertices[(i + 1) % this.vertices.length], c = this.vertices[(i + 2) % this.vertices.length];
|
|
2890
|
+
if (b.clone().sub(a).cross(c.clone().sub(b)).dot(this.normal).lt(this.epsilon.negated())) throw new RangeError("CSG polygon must be convex and consistently wound");
|
|
2891
|
+
}
|
|
2892
|
+
}
|
|
2893
|
+
}
|
|
2894
|
+
calcPlane() {
|
|
2895
|
+
if (this.vertices.length < 3) throw new RangeError("A polygon needs at least three vertices");
|
|
2896
|
+
const a = this.vertices[0];
|
|
2897
|
+
for (let i = 1; i + 1 < this.vertices.length; i++) {
|
|
2898
|
+
const n = this.vertices[i].clone().sub(a).cross(this.vertices[i + 1].clone().sub(a));
|
|
2899
|
+
if (!n.lengthSq().isZero()) {
|
|
2900
|
+
this.normal = n.normalize();
|
|
2901
|
+
this.w = this.normal.dot(a);
|
|
2902
|
+
return this;
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2905
|
+
throw new RangeError("Degenerate polygon");
|
|
2906
|
+
}
|
|
2907
|
+
flip() {
|
|
2908
|
+
this.normal.multiplyScalar(-1);
|
|
2909
|
+
this.w = this.w.negated();
|
|
2910
|
+
this.vertices.reverse();
|
|
2911
|
+
return this;
|
|
2912
|
+
}
|
|
2913
|
+
classifyVertex(v) {
|
|
2914
|
+
const d = this.normal.dot(v).sub(this.w);
|
|
2915
|
+
return d.lt(this.epsilon.negated()) ? 2 : d.gt(this.epsilon) ? 1 : 0;
|
|
2916
|
+
}
|
|
2917
|
+
classifySide(p) {
|
|
2918
|
+
return p.vertices.reduce((side, v) => side | this.classifyVertex(v), 0);
|
|
2919
|
+
}
|
|
2920
|
+
splitPolygon(p, coplanarFront = [], coplanarBack = [], front = [], back = []) {
|
|
2921
|
+
const side = this.classifySide(p);
|
|
2922
|
+
if (side === 0) (this.normal.dot(p.normal).gt(0) ? coplanarFront : coplanarBack).push(p.clone());
|
|
2923
|
+
else if (side === 1) front.push(p.clone());
|
|
2924
|
+
else if (side === 2) back.push(p.clone());
|
|
2925
|
+
else {
|
|
2926
|
+
const f = [], b = [];
|
|
2927
|
+
for (let i = 0; i < p.vertices.length; i++) {
|
|
2928
|
+
const vi = p.vertices[i], vj = p.vertices[(i + 1) % p.vertices.length];
|
|
2929
|
+
const ti = this.classifyVertex(vi), tj = this.classifyVertex(vj);
|
|
2930
|
+
if (ti !== 2) f.push(vi.clone());
|
|
2931
|
+
if (ti !== 1) b.push(vi.clone());
|
|
2932
|
+
if ((ti | tj) === 3) {
|
|
2933
|
+
const t = this.w.sub(this.normal.dot(vi)).div(this.normal.dot(vj.clone().sub(vi)));
|
|
2934
|
+
const v = vi.clone().lerp(vj, t);
|
|
2935
|
+
f.push(v);
|
|
2936
|
+
b.push(v.clone());
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
if (f.length >= 3) front.push(new Polygon(f, p.epsilon));
|
|
2940
|
+
if (b.length >= 3) back.push(new Polygon(b, p.epsilon));
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
clone() {
|
|
2944
|
+
return new Polygon(this.vertices, this.epsilon);
|
|
2945
|
+
}
|
|
2946
|
+
};
|
|
2947
|
+
// src/csg/three.ts
|
|
2948
|
+
/** BufferGeometry adapter. Mesh input is baked into world coordinates. */
|
|
2949
|
+
var ThreeBSP = class ThreeBSP {
|
|
2950
|
+
solid;
|
|
2951
|
+
constructor(source) {
|
|
2952
|
+
if (source instanceof BSP) {
|
|
2953
|
+
this.solid = source.clone();
|
|
2954
|
+
return;
|
|
2955
|
+
}
|
|
2956
|
+
const geometry = source instanceof Mesh ? (source.updateWorldMatrix(true, false), source.geometry.clone().applyMatrix4(source.matrixWorld)) : source;
|
|
2957
|
+
const position = geometry.getAttribute("position"), index = geometry.getIndex();
|
|
2958
|
+
if (!position || position.itemSize !== 3) throw new RangeError("CSG requires position triangles");
|
|
2959
|
+
const count = index?.count ?? position.count;
|
|
2960
|
+
if (count % 3) throw new RangeError("CSG requires triangle geometry");
|
|
2961
|
+
const faces = [];
|
|
2962
|
+
for (let i = 0; i < count; i += 3) {
|
|
2963
|
+
const vertices = Array.from({ length: 3 }, (_, j) => {
|
|
2964
|
+
const k = index ? index.getX(i + j) : i + j;
|
|
2965
|
+
return new HVec3(position.getX(k), position.getY(k), position.getZ(k));
|
|
2966
|
+
});
|
|
2967
|
+
try {
|
|
2968
|
+
faces.push(new Polygon(vertices));
|
|
2969
|
+
} catch (error) {
|
|
2970
|
+
if (!(error instanceof RangeError)) throw error;
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
this.solid = new BSP(faces);
|
|
2974
|
+
if (source instanceof Mesh) geometry.dispose();
|
|
2975
|
+
}
|
|
2976
|
+
subtract(other) {
|
|
2977
|
+
return new ThreeBSP(this.solid.sub(other.solid));
|
|
2978
|
+
}
|
|
2979
|
+
union(other) {
|
|
2980
|
+
return new ThreeBSP(this.solid.union(other.solid));
|
|
2981
|
+
}
|
|
2982
|
+
intersect(other) {
|
|
2983
|
+
return new ThreeBSP(this.solid.intersect(other.solid));
|
|
2984
|
+
}
|
|
2985
|
+
toGeometry() {
|
|
2986
|
+
const position = [], normal = [];
|
|
2987
|
+
for (const face of this.solid.toPolygons()) for (let i = 1; i + 1 < face.vertices.length; i++) for (const point of [
|
|
2988
|
+
face.vertices[0],
|
|
2989
|
+
face.vertices[i],
|
|
2990
|
+
face.vertices[i + 1]
|
|
2991
|
+
]) {
|
|
2992
|
+
position.push(...point.toArray().map((v) => v.toNumber()));
|
|
2993
|
+
normal.push(...face.normal.toArray().map((v) => v.toNumber()));
|
|
2994
|
+
}
|
|
2995
|
+
const geometry = new BufferGeometry();
|
|
2996
|
+
geometry.setAttribute("position", new Float32BufferAttribute(position, 3));
|
|
2997
|
+
geometry.setAttribute("normal", new Float32BufferAttribute(normal, 3));
|
|
2998
|
+
geometry.computeBoundingSphere();
|
|
2999
|
+
return geometry;
|
|
3000
|
+
}
|
|
3001
|
+
toMesh(material = new MeshNormalMaterial()) {
|
|
3002
|
+
return new Mesh(this.toGeometry(), material);
|
|
3003
|
+
}
|
|
3004
|
+
};
|
|
3005
|
+
export { BreathLight, DiamondShader, GLView, ThreeBSP };
|
|
3006
|
+
|
|
3007
|
+
//# sourceMappingURL=three.js.map
|