javascript - Same Function Gives Different Values -
i have function calling find derivative @ point, if hard code number script, right answer, if use variable same number, wrong answer.
html
point<br> <input type="text" name="point" id="point" class="form"><br> <p id="submit" onclick="submit()" name="solve">click</p><br>
script
<script type="text/javascript"> function diff(f) { return function(x) { return (f(x+.0000000001)-f(x))/.0000000001 }; } function f(x) { return x*x+2*x+1; } fprime = diff(f); function submit() { var point = document.getelementbyid("point").value; console.log(point); //-0.5 var q = fprime(-0.5); //will output 1 var w = fprime(point); //will output 749999998.98 }
any idea on why or how fix?
the problem point string, not number, convert value number using parsefloat() or use unary operator below
var point = +document.getelementbyid("point").value;
demo: fiddle
Comments
Post a Comment