-1

I don't understand this issue in javascript:

  1. how many digits after decimal point it can hold?
  2. 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;
};
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • It's called rounding error in floating point arithmetic. Read more: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Aragorn Nov 26 '15 at 19:36

1 Answers1

0

Oh that's a tricky question. There is no fix number of digits you will store. In fact they are not even precise!

JavaScript is using float wich is only an approximation of a number!

More insight on float here: Dealing with float precision in Javascript

More info on your Infinity-Problem by Aragorn:

It's called rounding error in floating point arithmetic. Read more: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Community
  • 1
  • 1
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • Have looked that link. But it doesn't clarify my questions. The link is just about formatting float numbers whereas it's not my goal. I just wanna get the solution that prevents the function's value from being transformed as Infinity. And why that's happening if it's value doesn't cross the border of 1.7976931348623157e+308 after which itshould be Infinity. – Merim Aliaskarova Nov 26 '15 at 19:43
  • Sorry, your question was "how many digits after decimal point can be stored in memory in JavaScript" – CoderPi Nov 26 '15 at 19:44
  • But the link that you gave doesn't answer that question. The link is about using toFixed(), round(). – Merim Aliaskarova Nov 26 '15 at 19:51
  • second link better? as i said, i first answered your question in the title.. please consider to make the question correct – CoderPi Nov 26 '15 at 19:51