java - Predicting the number of digits of a multiplication -
i need find number of digits of large multiplications (about 300 digits each). wondering if there trick predict number of digits product without performing calculation.
the number of digits can calculated exactly rounded (down) sum of base 10 log of 2 multiplicands plus 1, follows:
public static void main(string[] args) { decimalformat f = new decimalformat("#"); double num1 = 12345678901234567890d; double num2 = 314159265358979d; // here's line work: int numberofdigits = (int) (math.log10(num1) + math.log10(num2)) + 1; system.out.println(f.format(num1) + " * " + f.format(num2) + " = " + f.format((num1 * num2)) + ", has " + numberofdigits + " digits"); } output:
12345678901234567000 * 314159265358979 = 3878509413969699000000000000000000, has 34 digits this work arbitrarily large numbers.
Comments
Post a Comment