File Exchange

## num2strexact (exact version of num2str)

version 1.1.0.0 (8.66 KB) by James Tursa

### James Tursa (view profile)

num2strexact does exact conversion of number to string based on IEEE floating point bit pattern

Updated 05 Aug 2009

num2strexact is a self-building C-mex function that converts a double or single input to the exact decimal string. The conversion is done with hundreds of digits of precision to maintain the exact conversion. The conversion uses the exact decimal value of each bit of the IEEE double precision floating point format along with the exact application of 2^exponent. Inf and NaN bit patterns are recognized, and denormalized numbers are handled also.

Don't confuse the exact conversion with significance! Double numbers will only be significant to about 15 decimal digits, and single numbers will only be significant to about 7 decimal digits. For example,

>> format hex
>> 1.2
ans =
3ff3333333333333
>> num2strexact(1.2)
ans =
1.1999999999999999555910790149937383830547332763671875

>> 1.2 + eps(1.2)
ans =
3ff3333333333334 <-- one bit different from 1.2
num2strexact(1.2 + eps(1.2))
ans =
1.20000000000000017763568394002504646778106689453125

>> num2strexact(eps(1.2))
ans =
2.220446049250313080847263336181640625e-16

You can see that 1.2 is not represented exactly in IEEE double format. The difference shows up in the 18th digit for this example. Then note that the very next number in the IEEE double format model is about 2e-16 bigger. The exact conversions are shown for each number, but they are not significant beyond the 16th digit shown. There are no numbers in between these two numbers that can be represented in IEEE double format.

Syntax:

Y = num2strexact(X)
[Y1 Y2] = num2strexact(X1,X2)
[Y1 Y2 Y3] = num2strexact(X1,X2,X3)
: :
etc etc

The number of inputs must match the number of outputs, except in the special case of 1 input and 0 outputs where the result will simply be put into ans. If the input is a scalar, the output will be a char string. If the input is any other size array, the result will be a cell array of the same dimensions as the input, with each cell containing the char string conversion of the corresponding element. The input must be double or single.

num2strexact is an extremely important function for ... well ... it is an important function for ... hmmmm ... well maybe it is a somewhat useful function for ... sigh ... actually it is not a very useful function at all. It is simply a toy function that I wrote in response to a newsgroup thread. Some versions of MATLAB sprintf exhibit the behavior of num2strexact, while other versions of MATLAB sprintf print only the significant digits (like the example above). My version of MATLAB did not do the full conversion, so I wrote num2strexact to get this functionality and decided to share it. Other than as a curiosity, I suppose it could be a useful teaching aid in some cases (I am really stretching it here ...)

num2strexact gives similar results to the following NUM2IEEE function by Steven Lord:

http://www.mathworks.com/matlabcentral/fileexchange/2996

except that num2strexact does not require the Symbolic Math Toolbox to display the potentially hundreds of exact digits, and num2strexact runs much faster.

James Tursa

### James Tursa (view profile)

Note that as of R2017b MATLAB appears to have changed their underlying Windows library code so that decimal conversions are capable of doing the full decimal digit conversions on these platforms.

Joseph Benzaken

### Joseph Benzaken (view profile)

This function is incredibly helpful for learning floating point arithmetic and the inner workings of how numbers are stored according to IEEE standards. I would highly recommend it to anyone who is learning.

Emanuela Delfino

Anya Getman

Niraj Sanghvi

legendbb

### legendbb (view profile)

This is indeed the most helpful function for me to unpuzzle my floating precision confusion.

Derek O'Connor

Derek O'Connor

### Derek O'Connor (view profile)

I forgot to rate this when I first used it a couple of years ago.

This is a top class function which is very useful for digging into the innards of the IEEE floating point number system.

Students who are studying numerical algorithms and are serious about understanding floating point arithmetic should get this function and experiment with it.

Derek O'Connor

John D'Errico