I don't understand this issue in javascript:
- how many digits after decimal point it can hold?
- the value of digits' number after decimal point, that it can hold, is constant or it depends on the number of digits before point? i.e. the total number of digits that can be stored is fixed regardless of whether that is after or before point?
I use mathjs number type for the variable that stores the function's value at certain point. and in the Docs of mathjs it's said that if the value is bigger than 1.7976931348623157e+308 then it'll be converted into Infinity. But I exactly know that my function's value is never like that, it is evaluated as much-much lower value. Although the result that I receive after processing is Infinity. So maybe it's because the number of digits after decimal point is bigger that it can hold? If so, will be this alright if I round the returned value to the number of digits after point that it can hold? P.S. used library for number is http://mathjs.org/
function calc(f, a, b, tol, k_max) {
var starttime = performance.now();
iter = 0;
a = math.number(a);
b = math.number(b);
tol = math.number(tol);
k_max = math.number(k_max);
var x1, x2, f1, f2, iter = 0;
var parser = math.parser();
x1 = a + (3 - math.sqrt(5)) * (b - a) / 2;
x2 = a + b - x1;
while (math.abs(b - a) > tol && iter < k_max) {
iter++;
parser.set('x', x1);
f1 = parser.eval(f);
parser.set('x', x2);
f2 = parser.eval(f);
if (f1 < f2) {
b = x2;
x2 = x1;
x1 = a + b - x1;
} else {
a = x1;
x1 = x2;
x2 = a + b - x2;
}
}
var x = (a + b) / 2;
parser.set('x', x);
var f = parser.eval(f);
var elapsedtime = math.string(math.format(performance.now() - starttime, { notation: 'fixed', precision: 2 })) + " ms";
result = {
x: x,
f_x: f,
iter: iter,
b_a: math.abs(b - a),
elapsed: elapsedtime,
loading: false
};
return result;
};