Difference between revisions of "System Function DR"

From NARS2000
Jump to navigationJump to search
(New page: ==Monadic Function== <table border="1" cellpadding="5" cellspacing="0" rules="none" summary=""> <tr> <td> <table border="0" cellpadding="5" cellspacing="0" summary=""> <tr> ...)
 
Line 27: Line 27:
  
 
<ul>
 
<ul>
   <li>Boolean:  <apll>100</apll>, one bit per element</li>
+
   <li><apll>&nbsp;100</apll>:  Boolean, one bit per element</li>
   <li>Character:  <apll>1601</apll>, 16 bits per element</li>
+
   <li><apll>1601</apll>:  Character, 16 bits per element</li>
   <li>Integer:  <apll>6402</apll>, 64 bits per element</li>
+
   <li><apll>6402</apll>:  Integer, 64 bits per element</li>
   <li>Floating Point:  <apll>6403</apll>, 64 bits per element (double precision)</li>
+
   <li><apll>6403</apll>:  Floating Point, 64 bits per element (double precision)</li>
   <li>Arithmetic Progression Array:  <apll>6404</apll> 64 bits each for the offset and multiplier</li>
+
   <li><apll>6404</apll>:  Arithmetic Progression Array, 64 bits each for the offset and multiplier</li>
   <li>Heterogeneous:  <apll>3208</apll>, 32 bits per element (each is a pointer)</li>
+
   <li><apll>3208</apll>:  Heterogeneous, 32 bits per element (each is a pointer)</li>
   <li>Nested:  <apll>3210</apll>, 32 bits per element (each is a pointer)</li>
+
   <li><apll>3210</apll>:  Nested, 32 bits per element (each is a pointer)</li>
 
</ul>
 
</ul>
  
Line 39: Line 39:
  
 
<apll>
 
<apll>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕DR 2 100{rho}1<br />
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕DR 1 0 1<br />
 
100<br />
 
100<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr ⌈/⍬<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr ⌈/⍬<br />
 
6403<br />
 
6403<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕dr 2 64⍴1<br />
 +
6404<br />
 
</apll>
 
</apll>
 
<br />
 
<br />
Line 66: Line 68:
 
</tr>
 
</tr>
 
<tr>
 
<tr>
   <td><apll>L</apll> is an integer scalar datatype (see the table above).</td>
+
   <td><apll>L</apll> is an integer scalar datatype (see the table above), or a special value (see below).</td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
Line 74: Line 76:
 
</table>
 
</table>
 
<br />
 
<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>
 +
 +
<apll>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6402 ⎕dr 'NARS2000'<br />
 +
23362775258562638 13511005043687474<br />
 +
</apll>
 +
 +
<p>If the conversion is from a wider datatype to a narrower datatype, each value in the result
 +
  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,</p>
  
 
<apll>
 
<apll>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1601 ⎕dr 65 49<br />
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍴⎕←1601 ⎕dr 23362775258562638 13511005043687474<br />
A1<br />
+
NARS2000<br />
 +
8<br />
 +
</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>
 +
 
 +
<apll>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6402 ⎕dr 2 64⍴1<br />
 +
1 0 2 64<br />
 +
</apll>
  
 +
<p>However, this doesn't produce the expected result because this particular right argument is actually an APA, not a Boolean vector.  What actually happened is the APA was represented as 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>On the other hand, this expression</p>
 +
 +
<apll>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6402 ⎕dr 2 64⍴1 1<br />
 +
&nbsp;¯1<br />
 +
&nbsp;¯1<br />
 +
</apll>
 +
 +
<p>produces the expected result because the right argument is now Boolean.</p>
 +
 +
===Special Values===
 +
 +
<p>There are three special values you may use as a left argument to <apll>⎕dr</apll>.</p>
 +
 +
<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>
 +
 +
<p>For example,</p>
 +
 +
<apll>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 ⎕dr 'a'<br />
 +
Character (1601):  16 bits per element<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 ⎕dr ⍳12<br />
 +
Arithmetic Progression Array (6404):  64 bit offset + 64 bit multiplier<br />
 +
</apll>
 +
  </li>
 +
 +
  <li><p>The values <apll>¯1</apll> and <apll>1</apll> convert between character and floating point.  These two arguments make it easy to see the representation of floating point numbers so as to understand better precision and other floating point issues.</p>
 +
 +
<p>For example,</p>
 +
 +
<apll>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr 1.1<br />
 +
3FF199999999999A<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;¯1 ⎕dr '3fd5555555555555'<br />
 +
0.3333333333<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr ¯∞ ∞<br />
 +
FFF0000000000000<br />
 +
7FF0000000000000<br />
 +
</apll>
 +
  </li>
 +
</ul>
 +
 +
===A Word of Caution===
 +
 +
<p>This feature allows you to create special numbers which we don't currently support such as '''Quiet NaNs''', '''Signalling 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>For example,</p>
 +
 +
<apll>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6403 ⎕dr ¯64↑1<br />
 +
¯0<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QNaN←6403 ⎕dr ¯64↑13⍴1<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 ⎕dr QNaN<br />
 +
FFF8000000000000<br />
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QNaN=¯∞ ∞<br />
 +
1 1<br />
 
</apll>
 
</apll>

Revision as of 23:14, 8 May 2008

Monadic Function

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


The datatypes are encoded with a unique index in the low-order two digits, and the bits per element in the remaining digits:

  •  100: Boolean, one bit per element
  • 1601: Character, 16 bits per element
  • 6402: Integer, 64 bits per element
  • 6403: Floating Point, 64 bits per element (double precision)
  • 6404: Arithmetic Progression Array, 64 bits each for the offset and multiplier
  • 3208: Heterogeneous, 32 bits per element (each is a pointer)
  • 3210: Nested, 32 bits per element (each is a pointer)

For example,

      ⎕DR 1 0 1
100
      ⎕dr ⌈/⍬
6403
      ⎕dr 2 64⍴1
6404


Dyadic Function

Z←L ⎕DR R converts R to the datatype indicated by L.
R is an arbitrary array.
L is an integer scalar 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.


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.

      6402 ⎕dr 'NARS2000'
23362775258562638 13511005043687474

If the conversion is from a wider datatype to a narrower datatype, each value in the result 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,

      ⍴⎕←1601 ⎕dr 23362775258562638 13511005043687474
NARS2000
8

Keep in mind how 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:

      6402 ⎕dr 2 64⍴1
1 0 2 64

However, this doesn't produce the expected result because this particular right argument is actually an APA, not a Boolean vector. What actually happened is the APA was represented as an array with an offset of 1, a multiplier of 0 and a shape of 2 64.

On the other hand, this expression

      6402 ⎕dr 2 64⍴1 1
 ¯1
 ¯1

produces the expected result because the right argument is now Boolean.

Special Values

There are three 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,

          0 ⎕dr 'a'
    Character (1601): 16 bits per element
          0 ⎕dr ⍳12
    Arithmetic Progression Array (6404): 64 bit offset + 64 bit multiplier

  • The values ¯1 and 1 convert between character and floating point. These two arguments make it easy to see the representation of floating point numbers so as to understand better precision and other floating point issues.

    For example,

          1 ⎕dr 1.1
    3FF199999999999A
          ¯1 ⎕dr '3fd5555555555555'
    0.3333333333
          1 ⎕dr ¯∞ ∞
    FFF0000000000000
    7FF0000000000000

A Word of Caution

This feature allows you to create special numbers which we don't currently support such as Quiet NaNs, Signalling 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,

      6403 ⎕dr ¯64↑1
¯0
      QNaN←6403 ⎕dr ¯64↑13⍴1
      1 ⎕dr QNaN
FFF8000000000000
      QNaN=¯∞ ∞
1 1