Search code examples
javanumber-formatting

Java Understanding Math.getExponent(Double)


Double dble = new Double("2.2737367544323201e-13");
int exponent = Math.getExponent(dble);

I have the above code and exponent has value of '-43'. I'm not sure how the exponent is '-43', when the passed double value contains '-13'. Could someone shed some light into this API?


Solution

  • Math.getExponent() returns the exponent of the binary representation of the number. In your example -13 is the exponent of the decimal representation, and -43 the exponent of the binary representation.

    For example,

    System.out.println (Math.getExponent (1024));
    

    prints

    10
    

    since

    1024 = 2 ^ 10
    

    so the exponent is 10.

    System.out.println (Math.getExponent (1.0/8192));
    

    will print

    -13
    

    since

    1.0/8192 = 2 ^ (-13)