Search code examples
javadoublemultiplication

The result of multiplying two double varibles is zero, why?


I have a problem when I multiply two 'double' variables in my program, the result I get is zero. why?

for example:

1.0E-321 * 5.321777473676208E-4 = 0.0

how to prevent the result is not zero?.


Solution

  • double has a minimum value. If you try and store a number smaller than that, you get underflow.

    You could use the BigDecimal class.

    BigDecimal a = new BigDecimal("1.0E-321");
    BigDecimal b = new BigDecimal("5.321777473676208E-4");
    System.out.println(a.multiply(b));