java - How to add a very small number and a very large number -
i pretty new java. learning numerical computation @ moment. how 1 add , multiply small number , large number, of order $10^{-20}$ , of order $10^{20}$ arbitrary precision.
take @ bigdecimal
class. javadoc:
immutable, arbitrary-precision signed decimal numbers.
and:
the bigdecimal class gives user complete control on rounding behavior.
for example:
import java.math.bigdecimal; public class main { public static void main(string[] args) { bigdecimal big = new bigdecimal("10e20"); bigdecimal small = new bigdecimal("10e-20"); bigdecimal ans = big.add(small); system.err.println("answer: " + ans); } }
running gives following:
$ java main answer: 1000000000000000000000.00000000000000000010
Comments
Post a Comment