0

I have MATLAB code:

clear;
clc;
syms x;
f=log(x)*sin(x^2);
a=vpa(subs(f,x,2),100)
fprintf('a=%.100f\n',a);
doublea=double(a);
fprintf('a=%.100f\n',doublea);

and the result is

a =
 
-0.5245755158634217064842071630254785076113576311088295152384038229263081153172372089356742060202648499
 
a=-0.5245755158634216600000000000000000000000000000000000000000000000000000000000000000000000000000000000

doublea =

   -0.5246

a=-0.5245755158634216600000000000000000000000000000000000000000000000000000000000000000000000000000000000
>> 

Why if I use fprintf the decimal precision is only up to 16 digits, even though I use 100 digits precision? Also why if I convert a to double then a is only up to 16 digits?

Can it cause an error in the calculation if I want use more than 16 digits precision? How to fix it?

  • 2
    this should explain :https://stackoverflow.com/a/4227530/3878321 – Hadi Aug 29 '21 at 13:59
  • 3
    `fprintf` converts the `vpa` number to `double` to print it. A `double` has about 16 decimal digits of precision, it cannot store more digits. If you need more, you need to use `vpa`. Use `disp` to display its value (or use `char` to convert to string, and display that). – Cris Luengo Aug 29 '21 at 14:13

1 Answers1

0

Have you tried using num2str or vpa coupled with disp?

a1 = vpa(a,100);
disp(a1);

Or

disp(['a = ' num2str(a,100)])

Edit: Hadi's link in the comments has a great explanation.

Morc
  • 381
  • 2
  • 9