Skip to content

Commit 21366ae

Browse files
committed
⚡️ 三角関数の精度を380桁まで拡大 (以降謎の誤差発生)
1 parent 3237e7c commit 21366ae

File tree

7 files changed

+72
-55
lines changed

7 files changed

+72
-55
lines changed

dist/JavaLibraryScript.js

Lines changed: 33 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javalibraryscript",
3-
"version": "v1.0.7.1",
3+
"version": "v1.0.7.2",
44
"description": "Javaの機能をJavaScriptに適当に再現",
55
"main": "index.js",
66
"scripts": {

src/math/BigFloat.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ class BigFloat extends JavaLibraryScriptCore {
911911
/**
912912
* 正弦[Maclaurin展開]
913913
* @returns {this}
914+
* @throws {Error}
914915
*/
915916
sin() {
916917
/** @type {typeof BigFloat} */
@@ -1018,7 +1019,6 @@ class BigFloat extends JavaLibraryScriptCore {
10181019
*/
10191020
static _trigFuncsNewton(f, df, initial, precision, maxSteps = 50) {
10201021
const scale = 10n ** precision;
1021-
const EPSILON = 10n ** (precision / 2n);
10221022
let x = initial;
10231023

10241024
for (let i = 0; i < maxSteps; i++) {
@@ -1029,10 +1029,10 @@ class BigFloat extends JavaLibraryScriptCore {
10291029

10301030
// dx = fx / dfx (整数で割り算)
10311031
// dx は分母あるから SCALEかけて割る
1032-
const dx = fx / dfx;
1032+
const dx = (fx * scale) / dfx;
10331033
x = x - dx;
10341034

1035-
if ((dx < 0n ? -dx : dx) < EPSILON) break; // 収束判定
1035+
if (dx === 0n) break; // 収束判定
10361036
}
10371037

10381038
return x;
@@ -1157,7 +1157,8 @@ class BigFloat extends JavaLibraryScriptCore {
11571157
const df = (theta) => {
11581158
const cosTheta = this._cos(theta, precision, maxSteps);
11591159
if (cosTheta === 0n) throw new Error("Derivative undefined");
1160-
return (scale * scale) / (cosTheta * cosTheta);
1160+
1161+
return (scale * scale * scale) / (cosTheta * cosTheta);
11611162
};
11621163
return this._trigFuncsNewton(f, df, x, precision, BigInt(maxSteps));
11631164
}
@@ -1193,30 +1194,37 @@ class BigFloat extends JavaLibraryScriptCore {
11931194
if (x === 0n) {
11941195
if (y > 0n) return this._pi(precision) / 2n;
11951196
if (y < 0n) return -this._pi(precision) / 2n;
1196-
throw new Error("atan2(0, 0) is undefined");
1197+
return 0n;
11971198
}
11981199

1199-
// y == 0
1200-
if (y === 0n) {
1201-
if (x > 0n) return 0n;
1202-
if (x < 0n) return this._pi(precision);
1203-
}
12041200
const scale = 10n ** precision;
1205-
1206-
const ratio = (y * scale) / x;
1207-
1208-
const angle = this._atan(ratio, precision, maxSteps);
1209-
1210-
if (x > 0n) {
1211-
// 第1,4象限: そのまま
1212-
return angle;
1213-
}
1214-
const pi = this._pi(precision);
1215-
// 第2,3象限: πを足す
1216-
if (y >= 0n) {
1217-
return angle + pi;
1218-
}
1219-
return angle - pi;
1201+
const angle = this._atan((y * scale) / x, precision, maxSteps);
1202+
1203+
if (x > 0n) return angle;
1204+
if (y >= 0n) return angle + this._pi(precision);
1205+
return angle - this._pi(precision);
1206+
1207+
// // y == 0
1208+
// if (y === 0n) {
1209+
// if (x > 0n) return 0n;
1210+
// if (x < 0n) return this._pi(precision);
1211+
// }
1212+
// const scale = 10n ** precision;
1213+
1214+
// const ratio = (y * scale) / x;
1215+
1216+
// const angle = this._atan(ratio, precision, maxSteps);
1217+
1218+
// if (x > 0n) {
1219+
// // 第1,4象限: そのまま
1220+
// return angle;
1221+
// }
1222+
// const pi = this._pi(precision);
1223+
// // 第2,3象限: πを足す
1224+
// if (y >= 0n) {
1225+
// return angle + pi;
1226+
// }
1227+
// return angle - pi;
12201228
}
12211229

12221230
/**

types/JavaLibraryScript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@ declare class BigFloat extends JavaLibraryScriptCore {
14081408
/**
14091409
* 正弦[Maclaurin展開]
14101410
* @returns {this}
1411+
* @throws {Error}
14111412
*/
14121413
sin(): this;
14131414
/**

0 commit comments

Comments
 (0)