Be the first to rate this file! 109 downloads (last 30 days) File Size: 4.77 KB File ID: #25643

num2vpi - Converts input exactly into vpi

by James Tursa

 

23 Oct 2009

Code covered by BSD License  

Converts double inputs larger than 2^53-1, or char inputs with exponents, exactly into vpi.

Download Now | Watch this File

File Information
Description

num2vpi converts numeric and char string integral inputs exactly into Variable Precision Integers (vpi).

  Syntax:

  B = num2vpi(A)

      A = a logical or numeric class variable (real), or
        = a char array, each row giving a single integer value (real)
      B = a vpi class variable

  Description:

num2vpi converts A into a vpi class variable using the exact bit representation of the A elements. For single and double floating point inputs, num2vpi uses the FEX submission num2strexact to do the basic conversion. For uint64 and int64, num2vpi does the conversion in two uint32 pieces since vpi handles uint32 inputs but not uint64 or int64 inputs. The int64 conversion uses the typecast function. For logical inputs, num2vpi does a simple conversion to uint8 first, since vpi handles uint8 inputs but not logical inputs. For char inputs, each row is interpreted as a single number. Decimal points and exponents are allowed as long as the result is an integer. For all other input types, the input is simply passed on through to vpi to do the conversion.

  num2vpi requires the following submissions from the FEX:

  num2strexact:
  http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

  vpi:
  http://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic

  For older versions of MATLAB, you will also need this submission:

  typecast:
  http://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-c-mex-function

  Examples:

  >> num2vpi(1e30)
  ans =
     1000000000000000019884624838656

  >> num2vpi('1e30')
  ans =
     1000000000000000000000000000000

  >> num2vpi(1e100)
  ans =
     100000000000000001590289110975991804683608085639452813897813275577478
  38772170381060813469985856815104

  >> num2vpi('1e100')
  ans =
     100000000000000000000000000000000000000000000000000000000000000000000
  00000000000000000000000000000000

  >> num2vpi(['1.234e5';...
              '2000e-3';...
              '4.57e10'])
  ans =
     123400
     2
     45700000000

  >> num2vpi(uint64(realmax))
  ans =
     18446744073709551615

The main point to gather from the examples is that for a double or single input, num2vpi uses the utility function num2strexact to get the exact floating point bit pattern of the number and turn that into an integer string. The resulting integer is whatever the exact bit pattern converts into using exact power of 2 conversions. The char string input, on the other hand, simply takes the base number and adds or removes 0's based on the value of the exponent to get the integer string.

The convention for missing digits is that they are 0. For example, the following are all equivalent:

  num2vpi('e')
  num2vpi('0e')
  num2vpi('e0')
  num2vpi('0e0')

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
typecast C-mex function, num2strexact (exact version of num2str), Variable Precision Integer Arithmetic

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Any C compiler, such as the built in lcc compiler, will be needed to build the num2strexact and typecast functions. (Non-C programmers don't worry ... they are self-building ... you don't have to know anything about C or mex to use them).
Zip File Content  
Other Files license.txt,
num2vpi.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.
Tag Activity for this File
Tag Applied By Date/Time
vpi James Tursa 26 Oct 2009 10:20:59
variable James Tursa 26 Oct 2009 10:20:59
precision James Tursa 26 Oct 2009 10:20:59
integer James Tursa 26 Oct 2009 10:20:59
 

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