moatze
moatze

Reputation: 148

precision num2string matlab

I got some question relating the precision of the num2str() fctn in MatLab.

a=0.11111111111111;
b=a;

Linux/OSX: num2str(a+b,25): ans=0.2222222222222221655465116
Windows: num2str(a+b,25): ans= 0.222222222222222

Can anyone explain where the numbers after the 15th decimal place is coming from when using a Linux/OSX system? What is the maximum precision num2str() can obtain?

There is some hint at the num2str documentation that I don't understand completly.

Note:   If you specify precision to exceed the precision of the input floating-point data type, the results might not match the input values to the precision you specified. The result depends on your computer hardware and operating system.

Upvotes: 2

Views: 410

Answers (1)

tim
tim

Reputation: 10176

Have a look at eps (https://de.mathworks.com/help/matlab/ref/eps.html) which gives you the floating-point relative accuracy, which depends on your system architecture.

Have a further look here: https://de.mathworks.com/matlabcentral/answers/26458-machine-epsilon:

It roughly means that numbers are stored with about 15-16 digits of precision. If a number is approximately 1, then that means it can be stored with an error of around 10^(-16)

[...]

d = eps(X) is the positive distance from abs(X) to the next larger in magnitude floating point number of the same precision as X"

That says that d = eps(1) is the smallest positive value such that (1+d) is exactly representable and is different than 1.

1+eps(1) is the smallest representable number greater than 1, a single bit difference in the least significant (smallest change) bit.

Also have a look here to get to know more about it in general (as it is not really a MATLAB specific topic): https://en.wikipedia.org/wiki/Machine_epsilon

Upvotes: 3

Related Questions