System Function DR: Difference between revisions
Sudleyplace (talk | contribs) |
Sudleyplace (talk | contribs) |
||
Line 134: | Line 134: | ||
</li> | </li> | ||
<li><p>The | <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> | ||
<p>For example,</p> | <p>For example,</p> | ||
Line 141: | Line 141: | ||
1 ⎕dr 1.1<br /> | 1 ⎕dr 1.1<br /> | ||
3FF199999999999A<br /> | 3FF199999999999A<br /> | ||
| 1 ⎕dr '3fd5555555555555'<br /> | ||
0.3333333333<br /> | 0.3333333333<br /> | ||
1 ⎕dr ¯∞ ∞<br /> | 1 ⎕dr ¯∞ ∞<br /> | ||
Line 149: | Line 149: | ||
</li> | </li> | ||
<li><p>The | <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> | ||
<p>For example,</p> | <p>For example,</p> | ||
Line 156: | Line 156: | ||
2 ⎕dr ¯1<br /> | 2 ⎕dr ¯1<br /> | ||
FFFFFFFFFFFFFFFF<br /> | FFFFFFFFFFFFFFFF<br /> | ||
| 2 ⎕dr '7',15⍴'f'</apll> — The largest (positive) integer<apll><br /> | ||
9223372036854775807<br /> | 9223372036854775807<br /> | ||
| 2 ⎕dr '8',15⍴'0'</apll> — The smallest (negative) integer<apll><br /> | ||
¯9223372036854775808<br /> | ¯9223372036854775808<br /> | ||
2 ⎕dr 9223372036854775807 ¯9223372036854775808<br /> | 2 ⎕dr 9223372036854775807 ¯9223372036854775808<br /> |
Revision as of 03:45, 7 October 2008
Monadic Function
|
||||
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 — Note this is really an APA, not Boolean
6404
⎕dr 2 64⍴1 1 — This one is Boolean because Reshape produces APAs for integer singleton right arguments only
100
Dyadic Function
|
||||
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. |
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' — Eight 16-bit characters (=128 bits) convert to two 64-bit integers
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,
⍴⎕←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 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 'a'
1601
0 ⎕dr 'a'
Character (1601): 16 bits per element
0 ⎕dr ⍳12
Arithmetic Progression Array (6404): 64 bit offset + 64 bit multiplier
The value 1 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.
For example,
1 ⎕dr 1.1
3FF199999999999A
1 ⎕dr '3fd5555555555555'
0.3333333333
1 ⎕dr ¯∞ ∞
FFF0000000000000
7FF0000000000000
The value 2 converts between character and 64-bit integer. This argument makes it easy to see the representation of integers.
For example,
2 ⎕dr ¯1
FFFFFFFFFFFFFFFF
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
A Word of Caution
This system function 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 — A Quiet NaN (Not a Number)
1 ⎕dr QNaN
FFF8000000000000
⎕ct←0 ⋄ QNaN=¯∞ ∞
1 1