Problem 1833. Usage of java.math : Add, Multiply, Pow
This challenge is an introduction to the wonderful word of java.math that allows unlimited precision calculations. The primary reference sites are Java Math, Java BigDecimal, and Java BigInteger.
The usage of BigDecimal functions add, multiply, and pow will be tested.
Java Math tutorial: (Simplified summary that is believed correct)
vd-decimal value, vstr-string, vi-integer value xBD=java.math.BigDecimal(vd); % valid vd,vstr,vi creates xBD a BigDecimal variable import java.math.*; % simplifies statements xBD=BigDecimal(vstr); x2pwrBD=xBD.pow(vi); % Invalid: vd,vstr,BD xplusyBD=xBD.add(BigDecimal(y)); % add input requires BD type xmultiplyzBD=xBD.multiply(BigDecimal(z)); % multiply input requires BD type
To convert java to string of unlimited length can be achieved via java toString or Matlab char
xstr=toString(xBD) or xstr=char(xBD)
Input: X,Y, function_case [X,Y double or str, function 1:X+Y, 2:X*Y, 3: X^Y(double)
Output: z (char variable)
Future Challenges:
1. nchoosek_large (full precision) 2. Next Prime 3. factor_large
4. Factorial
Solution Stats
Problem Comments
-
3 Comments
Nice problem to learn the use of java inside Matlab.
BigInteger of doubles above 128 does odd things. BigInteger(num2str(v_dbl)) will work up to 2^53. I did not go into BigInteger limitations.
Usage of BigInteger will cause headaches. The Challenge is BigDecimal.
Solution Comments
Show commentsProblem Recent Solvers33
Suggested Problems
-
3372 Solvers
-
1008 Solvers
-
Project Euler: Problem 4, Palindromic numbers
1041 Solvers
-
Change the sign of even index entries of the reversed vector
553 Solvers
-
Switch matrix to a column vector
349 Solvers
More from this Author308
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!