问题描述
我当前的项目需要一个曲线方程,但它不会为不同的输入返回不同的值,而是为多个连续的输入返回相同的值。
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**67**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**68**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**69**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**70**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**71**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**72**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**73**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**74**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**75**/8.436912)^1.09424));
console.log(-21.6 + (594.6724 - -21.6)/(1 + (**76**/8.436912)^1.09424));
46.87471111111112
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
34.42476363636364
如您所见,在68-75的输入值之间,输出不变。 我已经尝试了几种IDE,以确保这不是本地问题。有人可以告诉我发生了什么事吗?
链接到jsfiddle:https://jsfiddle.net/ay4twrm5/
谢谢。
解决方法
我猜测^
的意思是求幂,只是JavaScript中的^
并非以这种方式使用(它是Bitwise XOR运算符)来执行JavaScript中的指数您需要使用**
(在实际代码中不是为了强调):
function f(x) {
return -21.6 + (594.6724 - -21.6)/(1 + (x/8.436912)**1.09424);
}
console.log(f(67));
console.log(f(68));
console.log(f(69));
console.log(f(70));
console.log(f(71));
console.log(f(72));
console.log(f(73));
console.log(f(74));
console.log(f(75));
console.log(f(76));
或者,您可以使用Math.pow
:
function f(x) {
return -21.6 + (594.6724 - -21.6)/(1 + Math.pow(x/8.436912,1.09424));
}
console.log(f(67));
console.log(f(68));
console.log(f(69));
console.log(f(70));
console.log(f(71));
console.log(f(72));
console.log(f(73));
console.log(f(74));
console.log(f(75));
console.log(f(76));