-1

i cant find an solution for my code. i dont know where could be the fail. i dont earn an 1/10^n result of an value of

0.100000000000000000000

no i earn an value of

0.1000000000000000055511151231257827021181583404541015625000000000000000

with javascrript. wtf. could someone explain me where is my fail in the code? thx and br

..

    function berechnungen(htmlTAG_partial, htmlTAG_n, htmlTAG_an, n, nks, sb, formel, cName, sName ) {
            let sum = [];
                // sum: Summe aller Partialwerte, um es auch im Graphen darstellen zu lassen
            let array_1 = [];
                // array_1: Gesamtarray aller in array_2 gesammelten Werte
            let array_2 = [];
                // array_2: Einzelwerte aus der bearbeiteten Nachkommerversetzung 
            let i = j = y = 1;
                // i, j: loop variables
                // y: versetzt die nachkommerstelle 
            let sum_2 = x = a = h = b = 0;
                // sum_2: sum of partial sum of N(a(n))
                // x: the math function to earn the partial N's of each a(n)
                // a: relation sum to substract the N(a(n+1)) - N(a(n))
                // h: helps to show the values in BERICH a(n) with "correct" decimals
            for (i; i < n; i++) {
                //if (formel === 'fx1' || formel === 'fx2') {
                    for (j = 1; j < n; j++) {
                        /////////////
                        // FORMELN //
                        /////////////
                        if (formel === 'fx1') {
                            fx = (1 / Math.pow(2, j));
                            h = (1 / Math.pow(2, i));
                        }
                        else if (formel === 'fx2') {
                            fx = (1 / Math.pow(j,2));
                            h = (1 / Math.pow(i,2));
                        }
                        else if (formel === 'fx3') {
                            fx = (1 / j);
                            h = (1 / i);
                        }
                        else if (formel === 'fx4') {
                            fx = (1 / Math.pow(10, j));
                            h = (1 / Math.pow(10, i));
                        }
                        ////////////////////////////////////
                        // VERSCHIEBUNGS-/SCHNITTFUNKTION //
                        ////////////////////////////////////
                        if (i == 1) {
                            b = Math.floor(fx,0);
                            if (b > 0) {
                                x = 0;
                                array_2.push(x);
                                sum_2 = sum_2 + x;
                                console.log("1:x: ", x, " 1:sum_2:", sum_2);
                            }
                            else {
                                x = Math.floor(fx * Math.pow(10, i),0);
                                array_2.push(x);
                                sum_2 = sum_2 + x;
                                console.log("x ", x, " sum_2", sum_2);
                            }   
                        }
                        else {
                            y = Math.floor((fx * Math.pow(10, i-1)),0)*10;
                            a = (fx * Math.pow(10, i));
                            x =  Math.floor(a,0) - Math.floor(y,0);
                            array_2.push(x);
                            sum_2 = sum_2 + x;
                        }
                    } // 2nd for loop
                //} // if 

                //////////////////////////
                // BEREICH Partialsumme //
                //////////////////////////
                if (i < sb) {
                    document.getElementById(htmlTAG_partial).innerHTML 
                        += "<ccc style='color:red-';>Partialsumme N(" + i + ")=" + sum_2 + "</ccc><br>";
                }
                else {
                    document.getElementById(htmlTAG_partial).innerHTML 
                        += "<ccc style='color:red;'>Partialsumme N(" + i + ")=" + sum_2 + "</ccc><br>";
                }
                ///////////////
                // BEREICH n //
                ///////////////
                document.getElementById(htmlTAG_n).innerHTML += i + "<BR>";
                /////////////////
                // BERICH a(n) //
                /////////////////
                document.getElementById(htmlTAG_an).innerHTML += h.toFixed(nks) + '<br>';
                ///////////////////////////////////////////////////////////
                // SET SOMETHNIG TO zero, clear something, push to Array //
                ///////////////////////////////////////////////////////////
                array_1.push(array_2);
                array_2 = [];
                sum.push(sum_2);
                sum_2 = 0;
            }
...
.. // some other stuff to print the message out
Eduard Tester
  • 151
  • 11
  • Where would we put all those extra binary decimal points (an infinity of them) required to exactly represent the decimal number 0.1 in binary, considering we only have 64 bits to store the value? What you're getting is the closest possible value that fits. – James Apr 12 '19 at 12:15
  • @James i thought also about that space, because i use a pi3 for that and i have just 560MB using memory of ram, because i don't know if javascript stores the rest on the disk. but it's still around 2^29 left and in my prog i can get just correct values if i try to calculate 1/2^n for only n(24) what it looks like that javascript use just RAM for the calculation. but that with 1/10^n i dont understand. any idea how could i solve it for around n=40 or 2^40? – Eduard Tester Apr 12 '19 at 13:17
  • @James because clusters of computers who makes also calculations with M*2^n, where M = Sum of computers in an clusterfunction and n as 300+ would be calculated – Eduard Tester Apr 12 '19 at 13:23
  • 1
    OK but they are not using doubles (64-bit floats) to do those calculations. They use some kind of "big math" libraries. With doubles you get seventeen significant digits of precision. These are not continuous numbers, they are an approximation of continuous numbers provided by a discrete system. – James Apr 12 '19 at 16:49
  • @James ah ok, but is it possible to tune this? do you have any experiences with how to do that, if it is possible? i thought about to analyse 5 or 7 digits of the first 5 or 7 terms and if i find musters like 999999 or 111111 or 09090909 or something like that to overwrite information in the array, because i dont know any function which has an 0.09090909090909090909091234564689078908..... value which jumps after the 09 values up to the different kind of numbers. or did i miss the right way of thinking atm? not sure if there are not functions which could do hat. any idea would be cool.thx – Eduard Tester Apr 19 '19 at 13:09
  • If it's a question of displaying the values, use a formatter that lets you specify the precision, eg [Number.toPrecision](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Number/toPrecision) - that generates a string that "looks" like the "right" number. If it's a question of mathematical operations failing because of imprecision, consider a library like [bigjs](https://mikemcl.github.io/big.js/). I don't know of a library that can represent a decimal using sum of fractions which is what I think you're saying with 0909 etc. – James Apr 19 '19 at 13:27

1 Answers1

1

Floating point numbers' representation is imprecise. Infuriating example:

console.log(0.1 + 0.2);
mbojko
  • 13,503
  • 1
  • 16
  • 26