File Exchange

image thumbnail

num2strexact (exact version of num2str)

version 1.1 (8.66 KB) by

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



View License

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.

Comments and Ratings (6)

Anya Getman

Niraj Sanghvi


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

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

John D'Errico (view profile)



Added self-building feature, and BSD license.

MATLAB Release
MATLAB 7.4 (R2007a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video