Be the first to rate this file! 131 downloads (last 30 days) File Size: 8.66 KB File ID: #22239

num2strexact (exact version of num2str)

by James Tursa

 

25 Nov 2008 (Updated 05 Aug 2009)

Code covered by BSD License  

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

Download Now | Watch this File

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 submission has inspired the following:
num2vpi - Converts input exactly into vpi
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.
Zip File Content  
Other Files license.txt,
num2strexact.c,
num2strexact.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
05 Aug 2009

Added self-building feature, and BSD license.

Tag Activity for this File
Tag Applied By Date/Time
num2strexact James Tursa 25 Nov 2008 14:52:55
num2str James Tursa 25 Nov 2008 14:52:55
str2num James Tursa 25 Nov 2008 14:52:55
precision James Tursa 25 Nov 2008 14:52:55
sprintf James Tursa 25 Nov 2008 14:52:55
printf James Tursa 25 Nov 2008 14:52:55
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com