Code covered by the BSD License  

Highlights from
num2strexact (exact version of num2str)

5.0 | 4 ratings Rate this file 21 Downloads (last 30 days) File Size: 8.66 KB File ID: #22239 Version: 1.1
image thumbnail

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

| Watch this File

File Information

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 =
>> num2strexact(1.2)
ans =

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

>> num2strexact(eps(1.2))
ans =

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.


  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:

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


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.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
28 Sep 2015 Niraj Sanghvi  
16 Apr 2013 legendbb

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

29 Jul 2011 Derek O'Connor

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  
20 Dec 2010 John D'Errico

John D'Errico (view profile)

05 Aug 2009 1.1

Added self-building feature, and BSD license.

Contact us