View License

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

» Watch video

Highlights from
num2vpi - Converts input exactly into vpi

5.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 4.77 KB File ID: #25643 Version: 1.0
image thumbnail

num2vpi - Converts input exactly into vpi


James Tursa (view profile)


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

| Watch this File

File Information

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


  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


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:



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



  >> num2vpi(1e30)
  ans =

  >> num2vpi('1e30')
  ans =

  >> num2vpi(1e100)
  ans =

  >> num2vpi('1e100')
  ans =

  >> num2vpi(['1.234e5';...
  ans =

  >> num2vpi(uint64(realmax))
  ans =

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:



Typecast And Typecastx C Mex Functions, Num2strexact (Exact Version Of Num2str), and Variable Precision Integer Arithmetic inspired this file.

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).
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
15 Dec 2009 John D'Errico

John D'Errico (view profile)


Contact us