System Function DR: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
 
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This function is available in both ''monadic'' and ''dyadic'' forms.
==Monadic Function==
==Monadic Function==
 
{{BoxStart|<apll>Z←⎕DR R</apll>
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
|returns the internal representation of <apll>R</apll>.}}
<tr>
{{BoxLine |<apll>R</apll> is an arbitrary array.}}
  <td>
{{BoxEnd  |<apll>Z</apll> is a numeric scalar which represents the datatype of <apll>R</apll>.}}
    <table border="0" cellpadding="5" cellspacing="0" summary="">
    <tr>
      <td valign="top"><apll>Z←⎕DR R</apll></td>
      <td></td>
      <td></td>
      <td>returns the data representation of <apll>R</apll>.</td>
    </tr>
    </table>
  </td>
</tr>
<tr>
  <td><apll>R</apll> is an arbitrary array.</td>
</tr>
<tr>
  <td><apll>Z</apll> is a numeric scalar which represents the datatype of <apll>R</apll>.</td>
</tr>
 
</table>
<br />
<br />


<p>The datatypes are encoded with a unique index in the low-order two digits, and the bits per element in the remaining digits:</p>
<p>The low-order two digits of the result encode the internal representation as a unique index for each category.  The high-order two digits of the result encode either the number of bits per element (where appropriate), or for Hypercomplex numbers, the internal representation of the coefficients:</p>


<ul>
*<apll>&nbsp;110</apll>:  Boolean, one bit per element
  <li><apll>&nbsp;110</apll>:  Boolean, one bit per element</li>
*<apll>1611</apll>:  Character, 16 bits per element
  <li><apll>1611</apll>:  Character, 16 bits per element</li>
*<apll>6412</apll>:  Integer, 64 bits per element
  <li><apll>6412</apll>:  Integer, 64 bits per element</li>
*<apll>6413</apll>:  Floating Point, 64 bits per element (double precision)
  <li><apll>6413</apll>:  Floating Point, 64 bits per element (double precision)</li>
*<apll>&nbsp;&nbsp;14</apll>:  Rational, 16 bytes plus two 4- or 8-byte pointers plus size of limbs per element
  <li><apll>6414</apll>:  Arithmetic Progression Array, 64 bits each for the offset and multiplier</li>
*<apll>&nbsp;&nbsp;15</apll>:  Variable-precision Floating Point, 12 bytes plus one 4- or 8-byte pointer plus size of limbs per element
  <li><apll>3218</apll>:  Heterogeneous, 32 bits per element (each is a pointer)</li>
*<apll>1216</apll>:  64-bit Integer Complex (two-dimensional) number
  <li><apll>6418</apll>:  Heterogeneous, 64 bits per element (each is a pointer)</li>
*<apll>1316</apll>:  64-bit Floating Point Complex (two-dimensional) number
  <li><apll>3219</apll>:  Nested, 32 bits per element (each is a pointer)</li>
*<apll>1416</apll>:  Multiple-precision Integer/Rational Complex (two-dimensional) number
  <li><apll>6419</apll>:  Nested, 64 bits per element (each is a pointer)</li>
*<apll>1516</apll>:  Multiple-precision Floating Point Complex (two-dimensional) number
  <li><apll>3220</apll>:  Rational, 16 bytes plus two 4-byte pointers plus size of limbs per element</li>
*<apll>1217</apll>:  64-bit Integer Quaternion (four-dimensional) number
  <li><apll>6420</apll>:  Rational, 16 bytes plus two 8-byte pointers plus size of limbs per element</li>
*<apll>1317</apll>:  64-bit Floating Point Quaternion (four-dimensional) number
  <li><apll>3221</apll>:  Variable-precision Floating Point, 12 bytes plus one 4-byte pointer plus size of limbs per element</li>
*<apll>1417</apll>:  Multiple-precision Integer/Rational Quaternion (four-dimensional) number
  <li><apll>6421</apll>:  Variable-precision Floating Point, 12 bytes plus one 8-byte pointer plus size of limbs per element</li>
*<apll>1517</apll>:  Multiple-precision Floating Point Quaternion (four-dimensional) number
</ul>
*<apll>1218</apll>:  64-bit Integer Octonion (eight-dimensional) number
*<apll>1318</apll>:  64-bit Floating Point Octonion (eight-dimensional) number
*<apll>1418</apll>:  Multiple-precision Integer/Rational Octonion (eight-dimensional) number
*<apll>1518</apll>:  Multiple-precision Floating Point Octonion (eight-dimensional) number
*<apll>&nbsp;&nbsp;19</apll>:  Arithmetic Progression Array, 64 bits each for the offset and multiplier
*<apll>&nbsp;&nbsp;20</apll>:  Heterogeneous, one 4- or 8-byte pointer per element
*<apll>&nbsp;&nbsp;21</apll>:  Nested, one 4- or 8-byte pointer per element
*<apll>&nbsp;&nbsp;22</apll>:  Real (one-dimensional) Ball Arithmetic
*<apll>2216</apll>:  Complex (two-dimensional) Ball Arithmetic
*<apll>2217</apll>:  Quaternion (four-dimensional) Ball Arithmetic
*<apll>2218</apll>:  Octonion (eight-dimensional) Ball Arithmetic


<p>For example,</p>
<p>For example,</p>


<apll>
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕DR 1 0 1<br />
      ⎕DR 1 0 1
110<br />
110  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr ⌈/⍬<br />
      ⎕DR ⌈/
6413<br />
6413
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr 2 64⍴1</apll> &mdash; Note this is really an APA, not Boolean<apll><br />
      ⎕DR 2 64⍴1  &mdash; Note [1] 
6414<br />
19
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr 2 64⍴1 1</apll> &mdash; This one is Boolean because Reshape produces APAs for integer singleton right arguments only<apll><br />
      ⎕DR 2 64⍴1 1 &mdash; Note [2] 
110<br />
110
</apll>
</pre>
<br />
<sup>[1]</sup> This is really an APA, not Boolean <br />
<br />
<sup>[2]</sup> This one is Boolean because Reshape produces APAs for integer singleton right arguments only


==Dyadic Function==
==Dyadic Function==
{{BoxStart|<apll>Z←L ⎕DR R</apll>
|converts <apll>R</apll> to the datatype indicated by <apll>L</apll>.}}
{{BoxLine|<apll>R</apll> is an arbitrary array.}}
{{BoxLine|<apll>L</apll> is an integer scalar or one-element vector datatype (see the table above), or a special value (see below).}}
{{BoxEnd|<apll>Z</apll> is <apll>R</apll> where each of the values are converted to the datatype indicated by <apll>L</apll>.}}


<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<p>Returned value <apll>Z</apll> may alternatively be: numeric scalar, vector of numbers, or a character string - depending on what the value of left argument <apll>L</apll> is.</p>
<tr>
  <td>
    <table border="0" cellpadding="5" cellspacing="0" summary="">
    <tr>
      <td valign="top"><apll>Z←L ⎕DR R</apll></td>
      <td></td>
      <td></td>
      <td>converts <apll>R</apll> to the datatype indicated by <apll>L</apll>.</td>
    </tr>
    </table>
  </td>
</tr>
<tr>
  <td><apll>R</apll> is an arbitrary array.</td>
</tr>
<tr>
  <td><apll>L</apll> is an integer scalar or one-element vector datatype (see the table above), or a special value (see below).</td>
</tr>
<tr>
  <td><apll>Z</apll> is <apll>R</apll> where each of the values are converted to the datatype indicated by <apll>L</apll>.</td>
</tr>


</table>
<p>If the conversion is from a narrower datatype to a wider datatype, there must be exactly enough columns in the right argument to match a multiple of the size of the wider datatype.  For example, when converting from character (16-bit) to integer (64-bit), the last column of the right argument must be a multiple of 4 (= 64/16); otherwise, a <apll>LENGTH ERROR</apll> is signalled.</p>
<br />


<p>If the conversion is from a narrower datatype to a wider datatype, there must be exactly enough columns in the right argument to match a multiple of the size of the wider datatype.  For example, when converting from character (16-bit) to integer (64-bit), the last column of the right argument must be a multiple of 4 (= 64/16); otherwise, a <apll>LENGTH ERROR</apll> is signalled.</p>
<p>In the following example, an 8-element character vector (=128 bits) is converted to two 64-bit integers (in both NARS2000 32- and 64-bit versions integers are stored in 64-bits):</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6412 ⎕dr 'NARS2000'</apll> &mdash; Eight 16-bit characters (=128 bits) convert to two 64-bit integers<apll><br />
      6412 ⎕DR 'NARS2000'
23362775258562638 13511005043687474<br />
23362775258562638 13511005043687474
</apll>
</pre></apll>


<p>If the conversion is from a wider datatype to a narrower datatype, the number of values in the result is a multiple of the ratio of the wider datatype to the narrower datatype.  For example, when converting from integer (64-bit) to character (16-bit), the last column in the result is the product of last column of the right argument and 4 (= 64/16).</p>
<p>If the conversion is from a wider datatype to a narrower datatype, the number of values in the result is a multiple of the ratio of the wider datatype to the narrower datatype.  For example, when converting from integer (64-bit) to character (16-bit), the last column in the result is the product of last column of the right argument and 4 (= 64/16).</p>


<p>For example,</p>
<p>For example ('''inverse''' of the example just above),</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍴⎕←1611 ⎕dr 23362775258562638 13511005043687474<br />
      ⍴⎕←1611 ⎕DR 23362775258562638 13511005043687474
NARS2000<br />
NARS2000
8<br />
8
</apll>
</pre></apll>


<p>Keep in mind how [[APA|Arithmetic Progression Arrays]] are created and represented as they can fool you.  For example, you might try the following to convert from Boolean to integer:</p>
<p>Keep in mind how [[APA|Arithmetic Progression Arrays]]''' are created and represented; they can fool both the casual programmer as well as an experienced coder.  For example, one might try the following to convert from Boolean to integer:</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6412 ⎕dr 2 64⍴1<br />
      6412 ⎕DR 2 64⍴1 ⍝ Conversion of an APA to an integer
1 0 2 64<br />
1 0 2 64
</apll>
</pre></apll>


<p>However, this doesn't produce the expected result because this particular right argument is an APA, not a Boolean vector.  That is, under certain circumstances, the reshape primitive creates an APA.  In this case, the APA is an array with an offset of <apll>1</apll>, a multiplier of <apll>0</apll> and a shape of <apll>2 64</apll>.</p>
<p>However, this doesn't produce the expected result because this particular right argument is an <apll>APA</apll>, and <b>not</b> a simple Boolean vector of 1s.  That is, under certain circumstances, the reshape primitive creates an APA.  In this case, the APA is an array with an offset of <apll>1</apll>, a multiplier of <apll>0</apll> and a shape of <apll>2</apll> rows and <apll>64</apll> cols.  The usefulness of APA's in reducing data storage and processing times outweighs their sometimes added code complexity or additional insight, which may be required.</p>


<p>On the other hand, this expression</p>
<p>On the other hand, this expression:</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6412 ⎕dr 2 64⍴1 1<br />
      6412 ⎕DR 2 64⍴1 1 ⍝ Contrast this with the prior example - Boolean vector created here, NOT an APA
&nbsp;¯1<br />
¯1
&nbsp;¯1<br />
¯1
</apll>
</pre></apll>


<p>produces the expected result because the right argument is now Boolean.</p>
<p>produces the expected result because the right argument is not an integer-valued singleton (in fact, it is a two-element vector which is not eligible for conversion to an APA).</p>


===Special Values===
===Special Values===


<p>There are several special values you may use as a left argument to <apll>⎕dr</apll>:</p>
<p>There are several special values you may use as a left argument to <apll>⎕DR</apll>:</p>


<ul>
<ul>
   <li><p>The value <apll>0</apll> displays the datatype of the right argument as a text string so you don't need to remember the datatype numbers.</p>
   <li><p>The value <apll>0</apll> displays the datatype of the right argument as a text '''string''' so you don't need to remember the datatype numbers.</p>


<p>For example,</p>
<p>For example,</p>
<apll><pre>
      ⎕DR 0
110
      0 ⎕DR 0
Boolean (110):  1 bit per element
      ⎕DR 23
6412
      0 ⎕DR 23 24
Integer (6412):  64 bits per element
      ⎕DR 1.1
6413
      0 ⎕DR 1.1
Floating Point (6413):  64 bits per element
      ⎕DR 'a'
1611
      0 ⎕DR 'a'
Character (1611):  16 bits per element
      ⎕DR ⍳12
19
      0 ⎕DR ⍳12
Arithmetic Progression Array (19):  64 bit offset + 64 bit multiplier -- PV1
      0 ⎕dr 1 2 3<_x/>
Rational (14):  arbitrary precision numerator and denominator
      0 ⎕dr 1 2 3<_v/>
VFP (15):  variable precision mantissa, 32-bit exponent -- FPC128
      0 ⎕DR 1v64 2<_v/>64
VFP (15):  variable precision mantissa, 32-bit exponent -- FPC64
      0 ⎕DR 1 2<_v/>64
VFP (15):  variable precision mantissa, 32-bit exponent -- FPC-Mixed
      0 ⎕DR ⍳¨⍳9
Nested Array (21):  PTR bits per element
      0 ⎕DR 'a' 1
Heterogeneous Array (20):  PTR bits per element
</pre></apll>
<p>Note that the storage format for integers (<apll>23 24</apll> in the above examples) is 64-bits per element and APAs (<apll>⍳12</apll>) is 64-bits for both the offset and multiplier regardless of whether you are running the 32- or 64-bit version of NARS2000.</p>
<p>In the last two examples, the reference to <apll>PTR</apll> means that storage format consists of pointers and is either 32- or 64-bits per element in the 32- or 64-bit version of NARS2000, respectively.</p>


<apll>
<p>Many times in programming - for debugging, data scrutiny or other purposes - it is very important to know <b>exactly</b> how the data is represented by the program. <apll>⎕DR</apll> or Data Representation is a feature designed to allow close scrutiny of how data is stored in memory and operated on in NARS2000.</p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr 'a'<br />
1611<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 ⎕dr 'a'<br />
Character (1611):  16 bits per element<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 ⎕dr ⍳12<br />
Arithmetic Progression Array (6414):  64 bit offset + 64 bit multiplier -- PV1<br />
</apll>


<p>Some arrays have properties that may be displayed by this function.  The list of such properties is</p>
<p>Some arrays have properties that may be displayed by this function:</p>


    <table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
      <tr>
<tr><td><apll>PV0</apll></td>
        <td><apll>PV0</apll></td>
<td>This array is a Permutation Vector, origin-0</td></tr>
        <td>This array is a Permutation Vector, origin-0</td>
      </tr>
       <tr>
       <tr>
         <td><apll>PV1</apll></td>
         <td><apll>PV1</apll></td>
Line 162: Line 166:
         <td>This array is a VFP whose entries are represented in two or more different precisions</td>
         <td>This array is a VFP whose entries are represented in two or more different precisions</td>
       </tr>
       </tr>
    </table>
</table>


   </li>
   </li>


   <li><p>The value <apll>1</apll> converts between character and floating point.  This argument makes it easy to see the representation of floating point numbers in order to help understand precision and other floating point issues.</p>
   <li><p>The value <apll>1</apll> converts between hex-character and floating point computer formats.  This argument makes it very useful to view representations of floating point numbers - in order to better understand numeric precisions, program storage formats, debug multi-precision floating-point math routines (by NARS developers) or examine perceived floating point issues.  Because this function converts between floating point and character format, it is self-inverse.</p>


<p>For example,</p>
<p>For example,</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕pp←99<br />
      ⎕PP←99
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr 1.1<br />
      1 ⎕DR 1.1
3FF199999999999A<br />
3FF199999999999A
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr '3fd',13⍴'5'<br />
      ⍴1 ⎕DR 1.1
0.3333333333333333<br />
16
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr ¯∞ ∞<br />
      1 ⎕DR 1
FFF0000000000000<br />
3FF0000000000000
7FF0000000000000<br />
      1 ⎕DR ÷3
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr '7fe',13⍴'f'</apll> &mdash; The largest positive number<apll><br />
3FD5555555555555
1.7976931348623157E308<br />
      1 ⎕DR '3fd',13⍴'5'
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr '001',13⍴'0'</apll> &mdash; The smallest positive number<apll><br />
0.3333333333333333
2.2250738585072014E¯308<br />
      1 ⎕DR ¯∞ ∞
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr '801',13⍴'0'</apll> &mdash; The largest negative number<apll><br />
FFF0000000000000
¯2.2250738585072014E¯308<br />
7FF0000000000000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr 'ffe',13⍴'f'</apll> &mdash; The smallest negative number<apll><br />
      1 ⎕DR '7fe',13⍴'f' The largest positive number
¯1.7976931348623157E308<br />
1.7976931348623157E308
</apll>
      1 ⎕DR '001',13⍴'0' The smallest positive number
2.2250738585072014E¯308
      1 ⎕DR '801',13⍴'0' The largest negative number
¯2.2250738585072014E¯308
      1 ⎕DR 'ffe',13⍴'f' The smallest negative number
¯1.7976931348623157E308
</pre></apll>
   </li>
   </li>


   <li><p>The value <apll>2</apll> converts between character and 64-bit integer.  This argument makes it easy to see the representation of integers.</p>
   <li><p>The value <apll>2</apll> converts between character and 64-bit integer.  This argument makes it easy to see the representation of integers.  Because this function converts between integer and character format, it is self-inverse.</p>


<p>For example,</p>
<p>For example,</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 ⎕dr ¯1<br />
      2 ⎕DR ¯1
FFFFFFFFFFFFFFFF<br />
FFFFFFFFFFFFFFFF
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 ⎕dr '7',15⍴'f'</apll> &mdash; The largest positive integer<apll><br />
      2 ⎕DR 16⍴'f'
9223372036854775807<br />
¯1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 ⎕dr '8',15⍴'0'</apll> &mdash; The smallest negative integer<apll><br />
      2 ⎕DR '7',15⍴'f' The largest positive integer
¯9223372036854775808<br />
9223372036854775807
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 ⎕dr 9223372036854775807 ¯9223372036854775808<br />
      2 ⎕DR '8',15⍴'0' The smallest negative integer
7FFFFFFFFFFFFFFF<br />
¯9223372036854775808
8000000000000000<br />
      2 ⎕DR 9223372036854775807 ¯9223372036854775808
</apll>
7FFFFFFFFFFFFFFF
8000000000000000
</pre></apll>
   </li>
   </li>
  <li><p>The value <apll>3</apll> returns the precision in bits of the right argument array as a simple scalar.</p>
<p>For example,</p>
<apll><pre>
      3 ⎕DR 0 1
1
      3 ⎕DR ⍳3
64
      3 ⎕DR ÷2 3
64
      3 ⎕DR ÷2 3<_x/>
      3 ⎕DR 2.3<_v/> ⍝ Returns the value of ⎕FPC when the constant was tokenized
128
      3 ⎕DR 1<_v/>64
64
      3 ⎕DR 'a'
0
      3 ⎕DR 'a' 1
0
      3 ⎕DR ⊂1 2
0
</pre></apll>
<li><p>The value <apll>4</apll> returns the numerator and denominator of a right argument Rational array as a Rational integer array whose shape is <apll>2,{rho}R</apll>.</p>
<p>For example,</p>
<apll><pre>      ⍴⎕←4 ⎕dr 1<_r/>3
1 3
2
      ⍴⎕←4 ⎕dr ,1<_r/>3
1
3
2 1
      ⍴⎕←4 ⎕dr 1<_r/>3 3<_r/>7
1 3
3 7
2 2
</pre></apll>
  </li>
</ul>
</ul>


===A Word of Caution===
===A Word of Caution===


<p>This system function allows you to create special numbers we don't support in that no other primitive generates these numbers and the behavior of all other primitives on these numbers is undefined.  Examples of such special numbers include '''Quiet NaNs''', '''Signaling NaNs''', '''Negative Zero''', and '''Denormals'''.  If the system doesn't behave as you expect when using these special numbers, don't be surprised.</p>
<p>This system function allows you to create special numbers we don't support in that no other primitive generates these numbers and the behavior of all other primitives on these numbers is undefined.  Examples of such special numbers include [https://en.wikipedia.org/wiki/NaN#Quiet_NaN Quiet NaNs], [https://en.wikipedia.org/wiki/NaN#Signaling_NaN Signaling NaNs], [https://en.wikipedia.org/wiki/Signed_zero Negative Zero], and [https://en.wikipedia.org/wiki/Denormal_number Denormals].  If the system doesn't behave as you expect when using these special numbers, don't be surprised.</p>


<p>For example,</p>
<p>For example,</p>


<apll>
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕ct←0 ⎕pp←99<br />
      ⎕CT←0 ⎕PP←99
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6413 ⎕dr ¯64↑1<br />
      6413 ⎕DR ¯64↑1
¯0<br />
¯0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QNaN←6413 ⎕dr ¯64↑13⍴1</apll> &mdash; A Quiet NaN (Not a Number)<apll><br />
      QNaN←6413 ⎕DR ¯64↑13⍴1 ⍝ The Real Indefinite (Quiet NaN)  where NaN = Not a Number
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr QNaN<br />
      1 ⎕DR QNaN
FFF8000000000000<br />
FFF8000000000000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QNaN=QNaN<br />
      QNaN=QNaN
0<br />
0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr '000fffffffffffff'</apll> &mdash; The largest positive denormal<apll><br />
      1 ⎕DR '000fffffffffffff' ⍝ The largest positive denormal
2.225073858507201E¯308<br />
2.225073858507201E¯308
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr '0000000000000001'</apll> &mdash; The smallest positive denormal<apll><br />
      1 ⎕DR '0000000000000001' ⍝ The smallest positive denormal
5E¯324<br />
5E¯324
</apll>
</pre></apll>
 
<p>There is incomplete support for negative zero (<apll>¯0</apll>) in NARS2000. It may be enabled by setting the second element of <apll>⎕FEATURE</apll> to <apll>1</apll>, i.e. <apll>⎕FEATURE[2]←1</apll>.  If you dare to try this concept, please report any problems in the usual manner via the [http://forum.nars2000.org/forum12.html?sid=18df73b72fccdedc847f3990d8b07410 forum], but please understand this feature is not fully supported.</p>
 
<br/>
See also '''[[System_Variable_CT|System Variable ⎕CT]]''' and '''[[System_Variable_PP|System Variable ⎕PP]]'''.<br>
{{SFDM|DR}}

Latest revision as of 15:07, 18 February 2024

This function is available in both monadic and dyadic forms.

Monadic Function

Z←⎕DR R returns the internal representation of R.
R is an arbitrary array.
Z is a numeric scalar which represents the datatype of R.


The low-order two digits of the result encode the internal representation as a unique index for each category. The high-order two digits of the result encode either the number of bits per element (where appropriate), or for Hypercomplex numbers, the internal representation of the coefficients:

  •  110: Boolean, one bit per element
  • 1611: Character, 16 bits per element
  • 6412: Integer, 64 bits per element
  • 6413: Floating Point, 64 bits per element (double precision)
  •   14: Rational, 16 bytes plus two 4- or 8-byte pointers plus size of limbs per element
  •   15: Variable-precision Floating Point, 12 bytes plus one 4- or 8-byte pointer plus size of limbs per element
  • 1216: 64-bit Integer Complex (two-dimensional) number
  • 1316: 64-bit Floating Point Complex (two-dimensional) number
  • 1416: Multiple-precision Integer/Rational Complex (two-dimensional) number
  • 1516: Multiple-precision Floating Point Complex (two-dimensional) number
  • 1217: 64-bit Integer Quaternion (four-dimensional) number
  • 1317: 64-bit Floating Point Quaternion (four-dimensional) number
  • 1417: Multiple-precision Integer/Rational Quaternion (four-dimensional) number
  • 1517: Multiple-precision Floating Point Quaternion (four-dimensional) number
  • 1218: 64-bit Integer Octonion (eight-dimensional) number
  • 1318: 64-bit Floating Point Octonion (eight-dimensional) number
  • 1418: Multiple-precision Integer/Rational Octonion (eight-dimensional) number
  • 1518: Multiple-precision Floating Point Octonion (eight-dimensional) number
  •   19: Arithmetic Progression Array, 64 bits each for the offset and multiplier
  •   20: Heterogeneous, one 4- or 8-byte pointer per element
  •   21: Nested, one 4- or 8-byte pointer per element
  •   22: Real (one-dimensional) Ball Arithmetic
  • 2216: Complex (two-dimensional) Ball Arithmetic
  • 2217: Quaternion (four-dimensional) Ball Arithmetic
  • 2218: Octonion (eight-dimensional) Ball Arithmetic

For example,

      ⎕DR 1 0 1
110 
      ⎕DR ⌈/⍬
6413 
      ⎕DR 2 64⍴1   — Note [1]  
19 
      ⎕DR 2 64⍴1 1 — Note [2]  
110

[1] This is really an APA, not Boolean
[2] This one is Boolean because Reshape produces APAs for integer singleton right arguments only

Dyadic Function

Z←L ⎕DR R converts R to the datatype indicated by L.
R is an arbitrary array.
L is an integer scalar or one-element vector datatype (see the table above), or a special value (see below).
Z is R where each of the values are converted to the datatype indicated by L.

Returned value Z may alternatively be: numeric scalar, vector of numbers, or a character string - depending on what the value of left argument L is.

If the conversion is from a narrower datatype to a wider datatype, there must be exactly enough columns in the right argument to match a multiple of the size of the wider datatype. For example, when converting from character (16-bit) to integer (64-bit), the last column of the right argument must be a multiple of 4 (= 64/16); otherwise, a LENGTH ERROR is signalled.

In the following example, an 8-element character vector (=128 bits) is converted to two 64-bit integers (in both NARS2000 32- and 64-bit versions integers are stored in 64-bits):

      6412 ⎕DR 'NARS2000'
23362775258562638 13511005043687474

If the conversion is from a wider datatype to a narrower datatype, the number of values in the result is a multiple of the ratio of the wider datatype to the narrower datatype. For example, when converting from integer (64-bit) to character (16-bit), the last column in the result is the product of last column of the right argument and 4 (= 64/16).

For example (inverse of the example just above),

      ⍴⎕←1611 ⎕DR 23362775258562638 13511005043687474
NARS2000
8

Keep in mind how Arithmetic Progression Arrays are created and represented; they can fool both the casual programmer as well as an experienced coder. For example, one might try the following to convert from Boolean to integer:

      6412 ⎕DR 2 64⍴1 ⍝ Conversion of an APA to an integer
1 0 2 64

However, this doesn't produce the expected result because this particular right argument is an APA, and not a simple Boolean vector of 1s. That is, under certain circumstances, the reshape primitive creates an APA. In this case, the APA is an array with an offset of 1, a multiplier of 0 and a shape of 2 rows and 64 cols. The usefulness of APA's in reducing data storage and processing times outweighs their sometimes added code complexity or additional insight, which may be required.

On the other hand, this expression:

      6412 ⎕DR 2 64⍴1 1 ⍝ Contrast this with the prior example - Boolean vector created here, NOT an APA
 ¯1
 ¯1

produces the expected result because the right argument is not an integer-valued singleton (in fact, it is a two-element vector which is not eligible for conversion to an APA).

Special Values

There are several special values you may use as a left argument to ⎕DR:

  • The value 0 displays the datatype of the right argument as a text string so you don't need to remember the datatype numbers.

    For example,

          ⎕DR 0
    110
          0 ⎕DR 0
    Boolean (110):  1 bit per element
          ⎕DR 23
    6412
          0 ⎕DR 23 24
    Integer (6412):  64 bits per element
          ⎕DR 1.1
    6413
          0 ⎕DR 1.1
    Floating Point (6413):  64 bits per element
          ⎕DR 'a'
    1611
          0 ⎕DR 'a'
    Character (1611):  16 bits per element
          ⎕DR ⍳12
    19
          0 ⎕DR ⍳12
    Arithmetic Progression Array (19):  64 bit offset + 64 bit multiplier -- PV1
          0 ⎕dr 1 2 3x
    Rational (14):  arbitrary precision numerator and denominator
          0 ⎕dr 1 2 3v
    VFP (15):  variable precision mantissa, 32-bit exponent -- FPC128
          0 ⎕DR 1v64 2v64
    VFP (15):  variable precision mantissa, 32-bit exponent -- FPC64 
          0 ⎕DR 1 2v64
    VFP (15):  variable precision mantissa, 32-bit exponent -- FPC-Mixed
          0 ⎕DR ⍳¨⍳9
    Nested Array (21):  PTR bits per element
          0 ⎕DR 'a' 1
    Heterogeneous Array (20):  PTR bits per element
    

    Note that the storage format for integers (23 24 in the above examples) is 64-bits per element and APAs (⍳12) is 64-bits for both the offset and multiplier regardless of whether you are running the 32- or 64-bit version of NARS2000.

    In the last two examples, the reference to PTR means that storage format consists of pointers and is either 32- or 64-bits per element in the 32- or 64-bit version of NARS2000, respectively.

    Many times in programming - for debugging, data scrutiny or other purposes - it is very important to know exactly how the data is represented by the program. ⎕DR or Data Representation is a feature designed to allow close scrutiny of how data is stored in memory and operated on in NARS2000.

    Some arrays have properties that may be displayed by this function:

    PV0 This array is a Permutation Vector, origin-0
    PV1 This array is a Permutation Vector, origin-1
    All2s This array consists of all 2s
    FPCnnn This array is a VFP and all entries are represented in Precision nnn
    FPC-Mixed This array is a VFP whose entries are represented in two or more different precisions
  • The value 1 converts between hex-character and floating point computer formats. This argument makes it very useful to view representations of floating point numbers - in order to better understand numeric precisions, program storage formats, debug multi-precision floating-point math routines (by NARS developers) or examine perceived floating point issues. Because this function converts between floating point and character format, it is self-inverse.

    For example,

          ⎕PP←99
          1 ⎕DR 1.1
    3FF199999999999A
          ⍴1 ⎕DR 1.1
    16
          1 ⎕DR 1
    3FF0000000000000
          1 ⎕DR ÷3
    3FD5555555555555
          1 ⎕DR '3fd',13⍴'5'
    0.3333333333333333
          1 ⎕DR ¯∞ ∞
    FFF0000000000000
    7FF0000000000000
          1 ⎕DR '7fe',13⍴'f' ⍝ The largest positive number
    1.7976931348623157E308
          1 ⎕DR '001',13⍴'0' ⍝ The smallest positive number
    2.2250738585072014E¯308
          1 ⎕DR '801',13⍴'0' ⍝ The largest negative number
    ¯2.2250738585072014E¯308
          1 ⎕DR 'ffe',13⍴'f' ⍝ The smallest negative number
    ¯1.7976931348623157E308
    
  • The value 2 converts between character and 64-bit integer. This argument makes it easy to see the representation of integers. Because this function converts between integer and character format, it is self-inverse.

    For example,

          2 ⎕DR ¯1
    FFFFFFFFFFFFFFFF
          2 ⎕DR 16⍴'f'
    ¯1
          2 ⎕DR '7',15⍴'f' ⍝ The largest positive integer
    9223372036854775807
          2 ⎕DR '8',15⍴'0' ⍝ The smallest negative integer
    ¯9223372036854775808
          2 ⎕DR 9223372036854775807 ¯9223372036854775808
    7FFFFFFFFFFFFFFF
    8000000000000000
    
  • The value 3 returns the precision in bits of the right argument array as a simple scalar.

    For example,

          3 ⎕DR 0 1
    1
          3 ⎕DR ⍳3
    64
          3 ⎕DR ÷2 3
    64
          3 ⎕DR ÷2 3x
    ∞
          3 ⎕DR 2.3v ⍝ Returns the value of ⎕FPC when the constant was tokenized
    128
          3 ⎕DR 1v64
    64
          3 ⎕DR 'a'
    0
          3 ⎕DR 'a' 1
    0
          3 ⎕DR ⊂1 2
    0
    
  • The value 4 returns the numerator and denominator of a right argument Rational array as a Rational integer array whose shape is 2,⍴R.

    For example,

          ⍴⎕←4 ⎕dr 1r3
    1 3 
    2
          ⍴⎕←4 ⎕dr ,1r3
    1
    3
    2 1 
          ⍴⎕←4 ⎕dr 1r3 3r7
    1 3
    3 7
    2 2
    

A Word of Caution

This system function allows you to create special numbers we don't support in that no other primitive generates these numbers and the behavior of all other primitives on these numbers is undefined. Examples of such special numbers include Quiet NaNs, Signaling NaNs, Negative Zero, and Denormals. If the system doesn't behave as you expect when using these special numbers, don't be surprised.

For example,

      ⎕CT←0 ⋄ ⎕PP←99
      6413 ⎕DR ¯64↑1
¯0
      QNaN←6413 ⎕DR ¯64↑13⍴1 ⍝ The Real Indefinite (Quiet NaN)  where NaN = Not a Number
      1 ⎕DR QNaN
FFF8000000000000
      QNaN=QNaN
0
      1 ⎕DR '000fffffffffffff' ⍝ The largest positive denormal
2.225073858507201E¯308
      1 ⎕DR '0000000000000001' ⍝ The smallest positive denormal
5E¯324

There is incomplete support for negative zero (¯0) in NARS2000. It may be enabled by setting the second element of ⎕FEATURE to 1, i.e. ⎕FEATURE[2]←1. If you dare to try this concept, please report any problems in the usual manner via the forum, but please understand this feature is not fully supported.


See also System Variable ⎕CT and System Variable ⎕PP.

Note: ⎕DR is available as both a monadic and dyadic form system function, you cannot assign a value to it.

System Variables (A value may be assigned to these except for ⎕DM)
ALX CT DM DT ELX FC FEATURE FPC IC IO
LR LX PP PR PW RL SA WSID
Niladic System Functions (a value cannot be assigned to these)
A AV EM ET LC NNAMES NNUMS SI SYSID SYSVER
T TC TCBEL TCBS TCESC TCFF TCHT TCLF TCNL TCNUL
TS WA
Monadic or dyadic system functions (a value cannot be assigned to these)
AT CR DC DFT DL DR EA EC ERROR ES
EX FMT FX MF NAPPEND NC NCREATE NERASE NINFO NL
NLOCK NREAD NRENAME NREPLACE NRESIZE NSIZE NTIE NUNTIE STOP TF
TRACE UCS VR
Note that quad functions and variables (except for the ⎕A family of functions) are case insensitive


See Also
System Commands System Variables and Functions Operators


Keyboard
Alt+Shift
Alt ¨ ¯ < > × ÷
Shift ~ ! @ # $ % ^ & * ( ) _ +
Key ` 1 2 3 4 5 6 7 8 9 0 - =
Alt+Shift
Alt ? § π
Shift Q W E R T Y U I O P { } |
Key q w e r t y u i o p [ ] \
Alt+Shift
Alt
Shift A S D F G H J K L : "
Key a s d f g h j k l ; '
Alt+Shift χ
Alt
Shift Z X C V B N M < > ?
Key z x c v b n m , . /
NARS 2000 Lang
Tool
Bar
+ - × ÷ * ! ? |
< = >
~ § π .. ,
/ \ ¨ .
_ ¯
Second Row i j k i j k l g p r v x