Search code examples
javascriptreturnequation

Equation returns same value with consecutive inputs


My current project requires a curve equation but instead of returning different values for different inputs, it returns the same value for several consecutive inputs.

Code (asterisks for emphasis, not present in the actual code)

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));

Output

46.87471111111112
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
55.434050000000006
34.42476363636364

As you can see, between the input values of 68-75 the output doesn't change. I've tried several IDEs to make sure it wasn't a local issue. Can someone clue me in on what's going on?

Link to jsfiddle: https://jsfiddle.net/ay4twrm5/

Thanks.


Solution

  • I am guessing by ^ you mean to do exponentiation, except that ^ in JavaScript is not used in that way (it is the Bitwise XOR operator), to do exponents in JavaScript you need use ** (not for emphasis, in actual code):

    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));

    Or, you could use 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));