Tes*_*ijn 3 javascript math nan ecmascript-6
我正在用JavaScript构建自己的数学课.我将默认精度声明为8.当我将其传递给函数时,将for-loop中的结果更改为NaN.
class Mathematics {
constructor(angleType, accuracy) {
this.PI = 3.1415926535897932384626433832795;
this.at = angleType || "rad";
this.acc = accuracy || 8;
}
Ang(angle, at) {
let angdeg, angrad;
switch (at) {
case "deg" || "d" || "Deg":
angdeg = angle;
angrad = angdeg * this.PI / 180;
break;
case "rad" || "r" || "Rad":
angrad = angle;
break;
}
return angrad;
}
Sin(angle, at, acc) {
let _at = at || this.at,
angrad = this.Ang(angle, _at),
ans = 1,
_acc = acc || this.acc,
temp = 1;
for (let i = 2; i <= 2 * _acc; i += 2) {
temp = temp * (-1) * angrad * angrad / (i * (i + 1));
ans = ans + temp;
console.log("sin " + i / 2 + ": " + ans);
}
return ans;
}
sin(angle, at, acc) { return this.Sin(angle, at, acc) }
Cos(angle, at, acc) {
let _at = at || this.at,
angrad = this.Ang(angle, _at),
ans = angrad,
_acc = acc || this.acc,
temp = 1;
for (let i = 2; i <= 2 * _acc; i += 2) {
temp = temp * (-1) * angrad * angrad / (i * (i - 1));
ans = ans + temp;
console.log("cos " + i / 2 + ": " + ans);
}
return ans;
}
cos(angle, at, acc) { return this.Cos(angle, at, acc) }
Tan(angle, at, acc) { return this.Sin(angle, at, acc) / this.Cos(angle, at, acc) }
tan(angle, at, acc) { return this.Tan(angle, at, acc) }
}
var MathClass = new Mathematics();
console.log("Class sin: " + MathClass.sin(1));
console.log("Math sin: " + Math.sin(1));
console.log("Class cos: " + MathClass.cos(1));
console.log("Math cos: " + Math.cos(1));
console.log("Class tan: " + MathClass.tan(1) + " an accuration of 8");
console.log("Class tan: " + MathClass.tan(1, "r", 9) + " an accuration of 9");
console.log("Math tan: " + Math.tan(1));Run Code Online (Sandbox Code Playgroud)
如果您运行该代码段,您将看到for-loop长度良好,但答案转向NaN.
您可以将构造函数中的精度编辑为9,它可以正常工作,直到您更改最后一个参数: MathClass.tan(1, "r", 5)
您的问题是此语法不能用作在JavaScript中捕获多个案例的简写:
case "rad" || "r" || "Rad":
Run Code Online (Sandbox Code Playgroud)
您需要使用多个case语句,如下所示:
case "rad":
case "r":
case "Rad":
Run Code Online (Sandbox Code Playgroud)
编辑:或使用if/ else和正则表达式:
Ang(angle, at) {
at = at || this.at
if (/^r(ad)?$/i.test(at)) {
return angle
} else if (/^d(eg)?$/i.test(at)) {
return angle * this.PI / 180
} else {
throw new Error("Invalid angle type: " + at)
}
}
Run Code Online (Sandbox Code Playgroud)
class Mathematics {
constructor(angleType, accuracy) {
this.PI = 3.1415926535897932384626433832795;
this.at = angleType || "rad";
this.acc = accuracy || 8;
}
Ang(angle, at) {
let angdeg, angrad;
switch (at) {
case "deg":
case "d":
case "Deg":
angdeg = angle;
angrad = angdeg * this.PI / 180;
break;
case "rad":
case "r":
case "Rad":
angrad = angle;
break;
}
return angrad;
}
Sin(angle, at, acc) {
let _at = at || this.at,
angrad = this.Ang(angle, _at),
ans = 1,
_acc = acc || this.acc,
temp = 1;
for (let i = 2; i <= 2 * _acc; i += 2) {
temp = temp * (-1) * angrad * angrad / (i * (i + 1));
ans = ans + temp;
console.log("sin " + i / 2 + ": " + ans);
}
return ans;
}
sin(angle, at, acc) { return this.Sin(angle, at, acc) }
Cos(angle, at, acc) {
let _at = at || this.at,
angrad = this.Ang(angle, _at),
ans = angrad,
_acc = acc || this.acc,
temp = 1;
for (let i = 2; i <= 2 * _acc; i += 2) {
temp = temp * (-1) * angrad * angrad / (i * (i - 1));
ans = ans + temp;
console.log("cos " + i / 2 + ": " + ans);
}
return ans;
}
cos(angle, at, acc) { return this.Cos(angle, at, acc) }
Tan(angle, at, acc) { return this.Sin(angle, at, acc) / this.Cos(angle, at, acc) }
tan(angle, at, acc) { return this.Tan(angle, at, acc) }
}
var MathClass = new Mathematics();
console.log("Class sin: " + MathClass.sin(1));
console.log("Math sin: " + Math.sin(1));
console.log("Class cos: " + MathClass.cos(1));
console.log("Math cos: " + Math.cos(1));
console.log("Class tan: " + MathClass.tan(1) + " an accuration of 8");
console.log("Class tan: " + MathClass.tan(1, "r", 9) + " an accuration of 9");
console.log("Math tan: " + Math.tan(1));Run Code Online (Sandbox Code Playgroud)