i have this simple problem.
Sample input:
input one
95.123
input two 12
Raising the first input to second input must give the exact value of the power.
like this : 548815620517731830194541.8990253434157159735359672218698
I can’t get it right.
here is what i have done so far. I think i’m missing something
System.out.println("Input One");
Scanner scanner = new Scanner(System.in);
Double inputOne = scanner.nextDouble();
System.out.println("Input Two");
Double inputTwo = scanner.nextDouble();
Double result = Math.pow(inputOne, inputTwo);
BigDecimal big = new BigDecimal(result);
System.out.println(big);
what am i missing out?
You’re using
doubleto start with, anddoublecan’t represent 95.123 exactly. You’re then usingMath.powwith the double values, which will further lose precision. There’s no point in converting an already-lossy value to a high-precision value… it can’t magically recover the data.Just pass a string to the
BigDecimalconstructor for the first argument, andBigDecimal.powto do the computation.Sample code:
Output:
(It looks like your expected value is actually truncated… the exact value should definitely end in a 1, as 312 ends in a 1.)