Please use Matlab for mathematics Build a set of routines in Matlab to do arithmetic in arbitrary precision with natural

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Please use Matlab for mathematics Build a set of routines in Matlab to do arithmetic in arbitrary precision with natural

Post by answerhappygod »

Please use Matlab for mathematics
Build a set of routines in Matlab to do arithmetic in arbitrary
precision with natural numbers. The first requirement is a
representation of a high precision natural number. I would suggest
a vector, where the first element holds the units digit, the second
element holds the tens digit, and so on. Storing digits in reverse
order will make arithmetic much easier.
You should write functions that display high precision natural
numbers, as well as perform subtraction of a smaller natural number
from a larger. The solution should be exact, so the number of
digits output may vary. You should include in your solution several
examples showing that your code is working properly. Here is an
example of a function to add natural numbers that is a useful
starting point. You will have to think a bit about subtraction, and
one easy approach would be to just subtract digits, and later if a
digit is negative do a carry in reverse.
m=length(a); n=length(b);
if m>n
c=a; o=m;
for i=1:n, c(i)=c(i)+b(i); end
else
c=b; o=n;
for i=1:m, c(i)=c(i)+a(i); end
end
for i=1:o-1
if c(i)>9, c(i)=c(i)-10; c(i+1)=c(i+1)+1; end
end
if c(o)>9, c(o)=c(o)-10; c(o+1)=1; end
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply