Cody

Problem 1833. Usage of java.math : Add, Multiply, Pow

Solution 2911819

Submitted on 4 Sep 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
x=2; y=64; % power zstr=java_math(x,y,3); assert(strcmp(zstr,'18446744073709551616'),sprintf('zstr=%s\n',zstr))

2   Fail
xstr='18446744073709551615'; y=3; %Add zstr=java_math(xstr,y,1); assert(strcmp(zstr,'18446744073709551618'),sprintf('zstr=%s\n',zstr))

No method 'add' with matching signature found for class 'java.math.BigDecimal'. Error in java_math (line 6) zBD=xBD.add(y); Error in Test2 (line 4) zstr=java_math(xstr,y,1);

3   Fail
x=2^53; % largest eps==1 double y=2^11; %Multiply zstr=java_math(x,y,2); assert(strcmp(zstr,'18446744073709551616'),sprintf('zstr=%s\n',zstr))

No method 'multiply' with matching signature found for class 'java.math.BigDecimal'. Error in java_math (line 8) zBD=xBD.multiply(y); Error in Test3 (line 4) zstr=java_math(x,y,2);

4   Fail
x=2^53; % largest valid double y=2^12; % Multiply zstr=java_math(x,y,2); assert(strcmp(zstr,'36893488147419103232'),sprintf('zstr=%s\n',zstr))

No method 'multiply' with matching signature found for class 'java.math.BigDecimal'. Error in java_math (line 8) zBD=xBD.multiply(y); Error in Test4 (line 4) zstr=java_math(x,y,2);

5   Fail
x=randi(10); y=randi(100); zstr=java_math(x,y,1); assert(strcmp(zstr,num2str(x+y)),sprintf('x=%2i y=%3i x+y=%5i zstr=%s\n',x,y,x+y,zstr))

No method 'add' with matching signature found for class 'java.math.BigDecimal'. Error in java_math (line 6) zBD=xBD.add(y); Error in Test5 (line 3) zstr=java_math(x,y,1);

6   Fail
x=randi(10); y=randi(100); zstr=java_math(x,y,2); assert(strcmp(zstr,num2str(x*y)),sprintf('x=%2i y=%3i x*y=%5i zstr=%s\n',x,y,x*y,zstr))

No method 'multiply' with matching signature found for class 'java.math.BigDecimal'. Error in java_math (line 8) zBD=xBD.multiply(y); Error in Test6 (line 3) zstr=java_math(x,y,2);

7   Pass
x=randi(20); y=randi(5); zstr=java_math(x,y,3); assert(strcmp(zstr,num2str(x^y)),sprintf('x=%2i y=%3i x^y=%8i zstr=%s\n',x,y,x^y,zstr))

Suggested Problems

More from this Author241

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!