Code covered by the BSD License

### Highlights from num2strexact (exact version of num2str)

5.0
5.0 | 5 ratings Rate this file 19 Downloads (last 30 days) File Size: 8.66 KB File ID: #22239 Version: 1.1

# num2strexact (exact version of num2str)

### James Tursa (view profile)

25 Nov 2008 (Updated )

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

File Information
Description

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.

Acknowledgements

This file inspired Num2vpi Converts Input Exactly Into Vpi and Hpf A Big Decimal Class.

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Any C compiler, such as the built-in lcc compiler. num2strexact is self-building ... you don't have to know anything about C to build or use it.
29 Jun 2016 Anya Getman

### Anya Getman (view profile)

28 Sep 2015 Niraj Sanghvi

### Niraj Sanghvi (view profile)

16 Apr 2013 legendbb

### legendbb (view profile)

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

29 Jul 2011 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

Comment only
29 Jul 2011 Derek O'Connor

### Derek O'Connor (view profile)

20 Dec 2010 John D'Errico