www.webdeveloper.com
Results 1 to 2 of 2

Thread: Rounding number without impacting result accuracy

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Rounding number without impacting result accuracy

    Rounding number without impacting result accuracy

    The problem: If you do log1000 the result you will get is 2.9999999999999996 rather than 3 or
    0.1 + 0.2 = 0.30000000000000004.

    I tried to remove this rounding error in the JavaScript eval() function without impacting result accuracy.
    In the format numbering function FormatNumber(strnum) I put CheckEpsilon(strnum) which tests if the "right tail" of number is greather than epsilon (assuming that the value of epsilon is 1e-9 as in C)

    function FormatNumber(strnum, asf, decimal) {
    // asf - number format: automatic(0), scientific(1) or fixed(2) notation
    // decimal - number of decimal places(0-15)

    // First we must check if the right tail is bigger than epsilon
    strnum = CheckEpsilon(strnum);
    // And then we format the number
    var x = parseFloat(strnum);

    switch(asf) {
    case 0: // auto
    strnum = x.toPrecision();
    break;
    case 1: // sci
    strnum = x.toExponential(decimal);
    break;
    case 2: // fix
    strnum = x.toFixed(decimal);
    break;
    }

    return strnum;
    }

    function CheckEpsilon(strnum) {
    // EPSILON - Difference between 1 and the least value greater than 1 that is representable.

    var epsilon = 1e-8;
    var x = parseFloat(strnum);

    var expnum = x.toExponential(17);
    // Last 10 numbers before the exponent (9 if the number is negative)
    // we turn in to a new decimal number ...
    var y = parseFloat("0." + expnum.slice(9,19));

    // and then we compare it to epsilon (1e-8)
    // If y (or 1-y) is smaller than epsilon we round strnum
    if (y<epsilon || (1-y)<epsilon) {
    strnum = x.toExponential(10);
    }

    //and if it isn't, strnum is returned as normal
    return strnum;
    }

    If you're interested in a practical showcase of the function you can take a look at a calculator I made (it's made in javascript so you can check the code easily). The link is: http://www.periodni.com/calculator.html.

    This is the way I've done it, however my actual question is: Does anyone know of any better way of doing this?

  2. #2
    Join Date
    Sep 2007
    Posts
    301
    Code:
      
    <script type="text/javascript">
    // http://www.w3schools.com/jsref/jsref_obj_array.asp
    // http://www.w3schools.com/jsref/jsref_obj_string.asp
    
    alert(  0.1 + 0.2 );  //  0.30000000000000004
    
    // 0.1 + 0.2 = 0.3 olmalı
    // sayıları bir dizinin içine atıp toplayalım
    
    var n = 0.1;
    var m = 0.2;
    
    var N = String(n).split(''); 
    //alert( N );
    var M = String(m).split(''); 
    //alert( M );
    
    var toplam = [];
    var elde = 0;
    var t, s;
    for(var i = N.length-1; i >= 0; i--) {
    if( N[i] == '.' ){ toplam[i] = '.';}
    if(N[i] != '.'){
    t= Number( N[i])  + Number( M[i]) + elde;  //alert('t= '+t);
    if(t<10) {toplam[i]= t ; elde = 0;  }
    if(t>9) { s= String(t).split(''); elde = Number(s[0]); toplam[i]= s[1]; }
    }
    
    }
    
    alert( n +  ' + ' + m + ' = ' +toplam.join('') );
    
    </script>
    Code:
    <script type="text/javascript">
    // http://www.w3schools.com/jsref/jsref_obj_regexp.asp
    // http://www.w3schools.com/jsref/jsref_regexp_test.asp
    
    // 1000 yazdığı zaman 3 sayısını elde etmek için sıfırların sayısını alabiliriz.
    
    var  s = '1000';
    var u;
    var re = /^10+$/;
    
    // alert( re.test(s) );
    
    if(re.test(s)) { u = s.length-1; }
    
    alert( u ); // 3
    
    </script>
    Last edited by Ayşe; 10-10-2013 at 02:14 PM.
    İyiliği emret. Kötülükten alıkoy. (31/16)

    hasbunallahuVeNimelVekil++

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles