How to stop MATLAB from rounding extremely small values to 0?

266 views (last 30 days)
I have a code in MATLAB which works with very small numbers, for example, I have values that are on the order of 10^{-25}, however when MATLAB does the calculations, the values themselves are rounded to 0. Note, I am not referring to format to display these extra decimals, but rather the number itself is changed to 0. I think the reason is because MATLAB, by default, retains up to 15 digits for its calculations. How can I change this so that numbers that are very very small are retained as they are in the calculations?

Answers (2)

Walter Roberson
Walter Roberson on 21 Feb 2014
MATLAB will not change the value to 0. However, it is possible that the result if using the value in an operation is indistinguishable from using 0. For example,
1 + 1E-25
is going to be indistinguishable from
1 + 0
Retaining the value would require a minimum of 84 bits of precision, but MATLAB only calculates to 53 bits.
If you need higher precision then you should use the Symbolic Toolbox. See also today's discussion http://www.mathworks.co.uk/matlabcentral/answers/116949-big-integer-speed-vpi-and-symbolic

Roger Stafford
Roger Stafford on 22 Feb 2014
The very purpose of floating point numbers is to allow quantities to be represented which are very small or very large. Such numbers can range from somewhere in the neighborhood of 10^-308 up to 10^308 before overflow or underflow occurs. Such small or large numbers can be multiplied without loss of relative accuracy provided the result does not go beyond these limits. However, as Walter has pointed out, when addition or subtraction occurs if both large numbers and small numbers are combined, the small numbers can become overshadowed by the large numbers and their relative accuracy lost. For example the number 1.234e7 is represented with an accuracy of one part in about 10^16 which means the error is around 10^-9. The number 2.345e-7 has an error around 10^-23. If we add them, the answer will only be accurate to around 10^-9, so all but two decimals of that small number have been lost in the process of addition to the large number. This is a limitation imposed by the number of bits used by the double precision floating point numbers which matlab uses, namely 53 binary bits. To do better than this requires the symbolic toolbox or its equivalents, allowing much larger numbers of digits to be used but at the expense of longer computation times.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!