Skip to main content

why is the Double.parseDouble making 9999999999999999 to 10000000000000000?


why is the Double.parseDouble making 9999999999999999 to 10000000000000000 ? For Example :




Double d =Double.parseDouble("9999999999999999");
String b= new DecimalFormat("#.##").format(d);
System.out.println(b);



IS Printing




10000000000000000



instead it has to show 9999999999999999 or 9999999999999999.00



Any sort of help is greatly appreciated.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. double only has 15/16 digits of accuracy and when you give it a number it can't represent (which is most of the time, even 0.1 is not accurate) it takes the closest representable number.

    If you want to represent 9999999999999999 exactly, you need to use BigDecimal.

    BigDecimal bd = new BigDecimal("9999999999999999");
    System.out.println(new DecimalFormat("#.##").format(bd));


    prints

    9999999999999999


    Very few real world problems need this accuracy because you can't measure anything this accurately anyway. i.e. to an error of 1 part per quintillion.



    You can find the largest representable integer with

    // search all the powers of 2 until (x + 1) - x != 1
    for (long l = 1; l > 0; l <<= 1) {
    double d0 = l;
    double d1 = l + 1;
    if (d1 - d0 != 1) {
    System.out.println("Cannot represent " + (l + 1) + " was " + d1);
    break;
    }
    }


    prints

    Cannot represent 9007199254740993 was 9.007199254740992E15


    The largest representable integer is 9007199254740992 as it needs one less bit (as its even)

    ReplyDelete
  2. The number 9999999999999999 is just above the precision limit of double-precision floating-point. In other words, the 53-bit mantissa is not able to hold 9999999999999999.

    So the result is that it is rounded to the nearest double-precision value - which is 10000000000000000.

    9999999999999999 = 0x2386f26fc0ffff // 54 significant bits needed
    10000000000000000 = 0x2386f26fc10000 // 38 significant bits needed

    ReplyDelete
  3. 9999999999999999 requires 54 bits of mantissa in order to be represented exactly, and double only has 52. The number is therefore rounded to the nearest number that can be represented using a 52-bit mantissa. This number happens to be 10000000000000000.

    The reason 10000000000000000 requires fewer bits is that its binary representation ends in a lot of zeroes, and those zeroes can get represented by increasing the (binary) exponent.

    For detailed explanation of a similar problem, see Why is (long)9223372036854665200d giving me 9223372036854665216?

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex