Rational and VFP Numbers: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
 
(39 intermediate revisions by 3 users not shown)
Line 1: Line 1:
===Introduction===
===Introduction===


<p><b>Rational numbers</b> complement the 64-bit integer datatype to provide infinite precision (or <apll>WS FULL</apll>) at the cost of some performance.  Similarly, <b>Variable-precision Floating Point</b> (VFP) numbers complement the 64-bit floating point datatype to provide more precision (user-controlled) with a similar cost in performance.</p>
<p><b>Rational numbers</b> complement the existing 64-bit integer datatype to provide infinite precision (or <apll>WS FULL</apll>) at the cost of some performance.  Similarly, <b>Variable-precision Floating Point</b> (VFP) numbers complement the existing 64-bit floating point (IEEE-754) datatype to provide more precision (as much as the user cares to specify) again with some cost in performance.</p>


<p>There is no separate datatype for infinite precision Integers.  Instead, they are represented as a special case of rational numbers.  In the discussion below, the phrase <b>rational integer</b> means a rational number whose denominator is one.</p>
<p>There is no separate datatype for infinite precision Integers.  Instead, they are represented as a special case of rational numbers.  In the discussion below, the phrase <b>rational integer</b> means a rational number whose denominator is one.</p>
<p class="note">Throughout this discussion the similarity between integer and rational numbers as well as floating point and VFP numbers will become apparent.</p>


===Rationale===
===Rationale===


<p>The reason is simple:  precision.  If the 53 bits of precision in the floating point result of <apll>2÷3</apll> is not enough, you now have two more choices:  one as an exact number, and one with as much precision as you care to specify.</p>
<p>Simply put:  precision.  If the 53 bits of precision in the floating point result of, say, <apll>2÷3</apll> is not enough, you now have two more choices:  one as an exact number, and one as a binary floating point number with as much precision as you care to specify.</p>
 
<p>For example,</p>
 
<apll><pre>
      ⎕PP←60 ⋄ ⎕FPC←512
      2÷3
0.6666666666666666
      2÷3<pn>x</pn>
2<pn>r</pn>3
      2÷3<pn>v</pn>
0.666666666666666666666666666666666666666666666666666666666667
      2*200
1.6069380442589903<pn>E</pn>60
      2*200<pn>x</pn>
1606938044258990275541962092341162602522202993782792835301376
      ○1
3.141592653589793
      ○1<pn>v</pn>
3.14159265358979323846264338327950288419716939937510582097494</pre></apll>


===Constants===
===Constants===


<p>Rational constants may be entered as follows:</p>
<p>Rational constants may be entered by suffixing an integer constant with an <apll><pn>x</pn></apll> (for a rational integer) or separating the numerator and denominator with an <apll><pn>r</pn></apll> (for a rational number) as follows:</p>


* <apll>123x</apll> for the constant <apll>123</apll> (the suffix <apll>x</apll> is but a shorthand for <apll>r1</apll>)
* <apll>123<pn>x</pn></apll> for the constant <apll>123</apll> (the suffix <apll><pn>x</pn></apll> is but a shorthand for <apll><pn>r</pn>1</apll>)
* <apll>123r4567</apll> for the constant <apll>123÷4567</apll>
* <apll>123<pn>r</pn>4567</apll> for the constant <apll>123÷4567</apll>


<p>VFP constants may be entered as follows:</p>
<p>VFP constants may be entered by suffixing the integer or floating point constant with a <apll><pn>v</pn></apll> as follows:</p>


* <apll>123v</apll> for the constant <apll>123</apll>
* <apll>123<pn>v</pn></apll> for the constant <apll>123</apll>
* <apll>123v4567</apll> for the constant <apll>123.4567</apll>
* <apll>123.4567<pn>v</pn></apll> for the constant <apll>123.4567</apll>
* <apll>123v4567e3</apll> for the constant <apll>123.4567e3</apll>
* <apll>123.4567e3<pn>v</pn></apll> for the constant <apll>123.4567<pn>E</pn>3</apll>


<p>The above formats for constants (except for the suffix <apll>x</apll>) may be used in other constants such as <apll>1r4p2</apll> to generate a shorter and more accurate value for <apll>π<sup>2</sup>/4</apll> than, say, <apll>((○1)*2)÷4</apll>.</p>
<p>The above formats for constants (except for the suffix <apll><pn>x</pn></apll>) may be used in other constants such as <apll>1<pn>r</pn>4<pn>p</pn>2</apll> to generate a shorter and more accurate value for <apll>π<sup>2</sup>/4</apll> than, say, <apll>((○1)*2)÷4</apll>.</p>


===Precision===
===Precision===


<p>Rational numbers have <b>infinite precision</b>.  They are stored with a separate numerator and denominator, both of which are exact numbers in the sense that their size (and hence precision) may grow limited only by the available workspace.  Rational numbers complement the 64-bit integer datatype to provide infinite precision (or <apll>WS FULL</apll>) at the cost of some performance.</p>
<p>Rational numbers have <b>infinite precision</b>.  They are stored with a separate numerator and denominator, both of which are exact numbers in the sense that their size (and hence precision) grows limited only by the available workspace.</p>


<p>VFP numbers have <b>user-controlled variable precision</b>, and each number may have a different precision.  The default precision at startup is controlled by the value of the system variable <apll>⎕FPC</apll>.  The system default of this value is <apll>128</apll> in units of bits of precision of the mantissa of the number, not counting the exponent (which is of fixed size).  The current precision may be changed as needed by assigning a new value to the system variable.  All newly created VFP numbers will have the new precision – the precision of VFP numbers already present in the workspace does not change.</p>
<p>VFP numbers have <b>user-controlled variable precision</b>, and each number may have a different precision.  The default precision at startup is controlled by the value of the system variable <apll>⎕FPC</apll>.  The system default of this value is <apll>128</apll> in units of bits of precision of the mantissa (the digits) of the number, not counting the exponent (which is of fixed size).  The current precision may be changed as needed by assigning a new value to the system variable.  All newly created VFP numbers will have the new precision – the precision of VFP numbers already present in the workspace does not change.</p>


<p>Generally, precision is set once for a particular application and unchanged thereafter.  Although not recommended, it is possible to mix VFP numbers of different precisions in a single array – presumably you <b>really</b> know what you are doing.  The system function <apll>⎕DR</apll> can be used to display an array's precision(s).</p>
<p>Generally, precision is set once for a particular application and unchanged thereafter.  Although not recommended, it is possible to mix VFP numbers of different precisions in a single array – presumably you <b>really</b> know what you are doing.  The system function <apll>0 ⎕DR</apll> may be used to display an array's precision(s).</p>


<p>In the same vein, VFP numbers in function definitions can be tricky, too.  For example, a cursory look at the function</p>
===Datatype Propagation===


<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ foo;⎕FPC;A<br />
<p>Generally, the datatype of constants propagates through a calculation. That is, if you start with a rational number and don't calculate with irrational or transcendental functions, you'll end up with a rational result, and if you start with a VFP number, you'll end up with a VFP result.</p>
[1]&nbsp;&nbsp;&nbsp;⎕FPC←256<br />
[2]&nbsp;&nbsp;&nbsp;A←1v23456789…23456789<br />
[3]&nbsp;&nbsp;&nbsp;…<br />
&nbsp;&nbsp;&nbsp;&nbsp;∇</apll><br />


<p>might lead you to think that the VFP constant saved into <apll>A</apll> has a precision of 256 bitsActually, the precision of that constant is set to the current value of <apll>⎕FPC</apll> during tokenization <b>when the function is saved in the workspace</b>, not when the function is executed.  If you must set the precision of constants within a user-defined function/operator to a specific value, use <apll></apll> on the character form of the VFP constant as the following function demonstrates:</p>
<p>An example from the programming problems site [http://projecteuler.net/ ProjectEuler.net] illustrates this point[http://projecteuler.net/problem=48 Problem #48] asks what are the low-order ten digits of the sum of the first thousand instances of N<sup>N</sup>?</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ foo;⎕FPC<br />
<p>The obvious expression <apll>¯10↑⍕+/*⍨⍳1000</apll>, at first sight, seems to solve the problem until you realize that it quickly runs afoul of the limited precision of 64-bit integer and floating point numbers. Clearly, this is a problem for the infinite precision of rational integers.</p>
[1]&nbsp;&nbsp;&nbsp;⎕FPC←2×⎕FPC<br />
[2]&nbsp;&nbsp;&nbsp;0 ⎕dr&nbsp;&nbsp;&nbsp;1v234567890123456789<br />
[3]&nbsp;&nbsp;&nbsp;0 ⎕dr ⍎'1v234567890123456789'<br />
&nbsp;&nbsp;&nbsp;&nbsp;∇<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo<br />
Variable Floating Point (3221):  variable precision mantissa, 32-bit exponent -- FPC128<br />
Variable Floating Point (3221): variable precision mantissa, 32-bit exponent -- FPC256</apll><br />


===Datatype Propagation===
<p>As <apll>⍳1000</apll> generates the first thousand integers as an integer datatype (actually an Arithmetic Progression Array), <apll>⍳1000<pn>x</pn></apll> generates the same values as rational integers.  Next, <apll>*⍨⍳1000<pn>x</pn></apll> generates the first thousand instances of N<sup>N</sup> as exact rational integers, and unlike its integer counterpart, there is no overflow to floating point, just an increase in precision (as well as space used in the workspace).  Then, <apll>+/*⍨⍳1000<pn>x</pn></apll> sums them into a single 3001-digit rational integer, and finally <apll>¯10↑⍕+/*⍨⍳1000<pn>x</pn></apll> converts the large integer to characters and extracts the low-order ten digits &mdash; <apll>9110846700</apll> &mdash; all in a small number of milliseconds.</p>
 
<p>Note how we started with an obvious expression that failed because of its limited precision, and made a single change to suffix the constant <apll>1000</apll> with an <apll><pn>x</pn></apll> to convert it to a rational integer which then propagates through the calculation with infinite precision to yield the correct result.</p>
 
===Display===


<p>Generally, the datatype of constants propagates through a calculation.  For example, <apll>⍳1000</apll> generates the first thousand integers as an integer (actually an Arithmetic Progression Array) datatype, and <apll>⍳1000x</apll> and <apll>⍳1000v</apll> generate the same values as rational and VFP numbers, respectively.</p>
<p>Rational integers are displayed as an integer with no special adornment; rational non-integers are displayed as a numerator and denominator separated by an <apll><pn>r</pn></apll> as in <apll>34<pn>r</pn>9</apll>.  As with the integer datatype, the numerator and denominator of a rational number are displayed exactly, unaffected by the current setting for Printing Precision (<apll>⎕PP</apll>).</p>
<br />
<apll><pre>
      !40
8.159152832478977<pn>E</pn>47
      !40<pn>x</pn>
815915283247897734345611269596115894272000000000
      +\÷⍳10<pn>x</pn>
1 3<pn>r</pn>2 11<pn>r</pn>6 25<pn>r</pn>12 137<pn>r</pn>60 49<pn>r</pn>20 363<pn>r</pn>140 761<pn>r</pn>280 7129<pn>r</pn>2520 7381<pn>r</pn>2520
</pre></apll>


<p>Continuing the above example, <apll>*⍨⍳1000x</apll> generates the first thousand instances of N<sup>N</sup> as rational numbers, <apll>+/*⍨⍳1000x</apll> sums them into a single 3001-digit rational integer, and finally <apll>¯10↑⍕+/*⍨⍳1000x</apll> extracts the low-order ten digits – <apll>9110846700</apll> (ProjectEuler.net #48) – all in a small number of milliseconds.</p>
<p>VFP numbers are displayed as decimal numbers to the precision inherent in the number or <apll>⎕PP</apll>, whichever is smaller, just as floating point numbers are displayed.  For example,</p>


<p>Note how the obvious expression <apll>¯10↑⍕+/*⍨⍳1000</apll> at first seems to solve the problem except that it quickly runs afoul of the limited precision of 64-bit integer and floating point numbers. Suffixing the constant <apll>1000</apll> with an <apll>x</apll> converts it to a rational number which then propagates through the calculation with infinite precision to yield the correct result.</p>
<apll><pre>
      ⎕PP←100
      ⎕FPC←64
      ○1
3.141592653589793
      ○1<pn>x</pn>
3.14159265358979323851
      ⎕FPC←128
○1<pn>x</pn>
3.141592653589793238462643383279502884195</pre></apll>  


===Formatting===
<p>where both of the above displays were limited by the precision of the number, not <apll>⎕PP</apll>.</p>


<p>Rational integers are formatted as an integer with no special adornment; rational non-integers are formatted as a numerator and denominator separated by an <apll>r</apll> as in <apll>34r9</apll>.  As with the integer datatype, the numerator and denominator of a rational number are formatted exactly, unaffected by <apll>⎕PP</apll>.</p>
<p>However, the first of the following displays <b>is</b> limited by <apll>⎕PP</apll>:</p>
<br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2*100<br />
1.2676506002282294E30<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2*100x<br />
1267650600228229401496703205376</apll><br />


<p>VFP numbers are formatted as decimal numbers to the precision inherent in the number or <apll>⎕PP</apll>, whichever is smaller.  For example,</p>
<apll><pre>
      ⎕FPC←128
      ⎕PP←20
      !40<pn>v</pn>
81591528324789773435____________________________
      ⎕PP←80
      !40<pn>v</pn>
8159152832478977343456112695961158942720________</pre></apll>  


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕PP←100<br />
<p>In the last display, the current setting of Printing Precision is large enough, but the current setting of the Floating Point Control (<apll>⎕FPC</apll>) whose value is in bits is too small, so the display is truncated.</p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕FPC←64<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;○1r2<br />
1.57079632679489661926<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕FPC←128<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;○1r2<br />
1.570796326794896619231321691639751442098</apll><br />


<p>where both of the above displays were limited by the precision of the number, not <apll>⎕PP</apll>.</p>
===Formatted Display===


<p>However the following displays <b>are</b> limited by <apll>⎕PP</apll>:</p>
<p>The system function <apll>⎕FMT</apll> has been enhanced to allow formatting of rational numbers via the (new) <apll>R</apll>-format specifier.  For example,</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕FPC←128<br />
<apll><pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕PP←20<br />
      'R4.2' ⎕FMT ∘.÷⍨⍳6<_x/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!40v<br />
1  1<_r/>2 1<_r/>3 1<_r/>4 1<_r/>5 1<_r/>6
815915283247897734350000000000000000000000000000<br />
2  1  2<_r/>3 1<_r/>2 2<_r/>5 1<_r/>3
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⎕PP←80<br />
3  3<_r/>2 1  3<_r/>4 3<_r/>5 1<_r/>2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!40v<br />
4  2  4<_r/>3 1  4<_r/>5 2<_r/>3
815915283247897734345611269596115894272000000000</apll><br />
5  5<_r/>2 5<_r/>3 5<_r/>4 1  5<_r/>6
6  3  2  3<_r/>2 6<_r/>5 1  </pre></apll>


<p>In other words, you might be fooled into thinking that a VFP number has more trailing zeros than it actually does simply because <apll>⎕PP</apll> is too small.</p>
<p>Moreover, the Symbol Substitution (<apll>S&lt;…&gt;</apll>) feature of <apll>⎕FMT</apll> allows you to substitute a different symbol for the default <apll><_r/></apll> used to separate the numerator and denominator of a rational number, as in</p>


<p><b>► In general, the VFP datatype is better suited for representing fractional numbers than large integers.</b></p>
<apll><pre>
      'S&lt;r/&gt;R4.2' ⎕FMT ∘.÷⍨⍳6<_x/>
1  1/2 1/3 1/4 1/5 1/6
2  1  2/3 1/2 2/5 1/3
3  3/2 1  3/4 3/5 1/2
4  2  4/3 1  4/5 2/3
5  5/2 5/3 5/4 1  5/6
6  3  2  3/2 6/5 1  </pre></apll>


===Datatype Promotion===
===Datatype Promotion===
Line 98: Line 134:
<p>For the most part, rational numbers beget rational numbers and VFP numbers beget VFP numbers.  However, when irrational, transcendental, and certain other functions are used, rational numbers beget VFP numbers.  For example,</p>
<p>For the most part, rational numbers beget rational numbers and VFP numbers beget VFP numbers.  However, when irrational, transcendental, and certain other functions are used, rational numbers beget VFP numbers.  For example,</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;○1<br >
<apll><pre>
3.1415926535897931<br />
      *1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;○1x<br />
2.718281828459045
3.141592653589793238462643383279502884195</apll><br />
      *1<pn>x</pn>
2.718281828459045235360287471352662497759</pre></apll>  


<p>where the datatype of the two results are floating point and VFP, respectively.  That is, in a manner similar to how some primitive functions with integer arguments may return floating point results, when a rational number is used as an argument to a primitive function that can't return a result with infinite precision, it is promoted to VFP.</p>
<p>where the datatype of the two results are floating point and VFP, respectively.  That is, in a manner similar to how some primitive functions with integer arguments may return floating point results when a rational number is used as an argument to a primitive function that can't return a result with infinite precision, it returns a VFP number.</p>


<p>The reason irrational, transcendental, and certain other functions on rational numbers don't return rational numbers is that, by definition, the result of such a function is, in general, not representable as a rational number; instead, VFP numbers are better suited to represent irrational results where the end user may control exactly how much precision is desired.</p>
<p class="note">The reason irrational, transcendental, and certain other functions on rational numbers do not return rational numbers is that, by definition, the result of such a function is, in general, not representable as a rational number; instead, VFP numbers are better suited to represent irrational results where the end user may control exactly how much precision is desired in an obviously inexact number.</p>


<p>Two special functions are the prime decomposition (<apll>πR</apll>)/number theoretic (<apll>LπR</apll>) functions.  In order to return accurate results over a wide range of precision, all numeric arguments are converted to rational integers and the result is returned as a rational integer.</p>
<p>Two special functions are the prime decomposition (<apll>πR</apll>)/number theoretic (<apll>LπR</apll>) functions.  In these cases, fractional or VFP right arguments are converted to integers or rational integers, respectively, which is the datatype of the result except for <apll>0πR</apll> (Primality Test) which always returns a Boolean result regardless of the type of <apll>R</apll>.</p>


<p>The list of functions that produce VFP numbers given rational numbers is as follows:</p>
<p>Ignoring purely structural functions, the list of functions that produce VFP numbers given rational numbers is as follows:</p>


* Exponentiation:  <apll>*R</apll> and <apll>L*R</apll> (except when <apll>L</apll> is rational and <apll>R</apll> is a 32-bit integer, in which case the result is a rational integer)
* Power:  <apll>*R</apll> and <apll>L*R</apll> (except when <apll>R</apll> is a 32-bit integer, in which case the result is a rational number)
* Logarithm:  <apll>⍟R</apll> and <apll>L⍟R</apll>
* Logarithm:  <apll>⍟R</apll> and <apll>L⍟R</apll>
* Pi Times and Circle functions:  <apll>○R</apll> and <apll>L○R</apll>
* Pi Times and Circle functions:  <apll>○R</apll> and <apll>L○R</apll>
* Root:  <apll>√R</apll> and <apll>L√R</apll>
* Root:  <apll>√R</apll> and <apll>L√R</apll>
* Factorial and Binomial:  <apll>!R</apll> and <apll>L!R</apll> (except when the arguments are rational integers, in which case the result is a rational integer)


<p>The list of functions that <b>don't</b> produce a rational or VFP result given those argument(s) is as follows:</p>
<p>Beyond the ones mentioned above, the list of functions that <b>don't</b> produce a rational or VFP result given those argument(s) is as follows:</p>


* Depth:  <apll>≡</apll> (Integer)
* Depth:  <apll>≡</apll> (Integer)
* Dyadic Comparison:  <apll>=≠<≤≥>≡≢</apll> (Boolean)
* Dyadic Comparison:  <apll>= ≠ < ≤ ≥ > ≡ ≢</apll> (Boolean)
* Nand and Nor:  <apll>⍲⍱</apll> (Boolean)
* Nand and Nor:  <apll>⍲ ⍱</apll> (Boolean)
* Grade Up/Down:  <apll>⍋⍒</apll> (Integer)
* Grade Up/Down:  <apll>⍋ ⍒</apll> (Integer)
* Index Of:  <apll>⍳</apll> (Integer)
* Index Of:  <apll>⍳</apll> (Integer)
* Member Of:  <apll>∊</apll> (Boolean)
* Member Of:  <apll>∊</apll> (Boolean)
* Find:  <apll>⍷</apll> (Boolean)
* Find:  <apll>⍷</apll> (Boolean)
* Subset and Superset:  <apll>⊆⊇</apll> (Boolean)
* Subset and Superset:  <apll>⊆ ⊇</apll> (Boolean)
* Prime Decomposition and Number Theoretic:  <apll>π</apll> (Rational)
* Format:  <apll></apll> (Character)


<p>Otherwise, rational argument(s) produce rational result(s) and VFP argument(s) produce VFP result(s).</p>
<p>Otherwise, rational argument(s) produce rational result(s) and VFP argument(s) produce VFP result(s).</p>
Line 132: Line 170:
===Datatype Demotion===
===Datatype Demotion===


<p>It is common in APL implementations to demote datatypes where appropriate.  For example, the constant <apll>1.0</apll> might actually be represented as an integer or even Boolean datatype.  The idea is there is no loss of precision and the storage is typically smaller, so why not?</p>
<p>It is common in APL implementations to demote datatypes where appropriate.  For example, the constant <apll>1.0</apll> might actually be represented as an integer or even Boolean datatype.  The idea is there is no loss of precision and the storage is typically smaller which might lead to a more efficient algorithm when next used, so why not?</p>


<p>With rational and VFP numbers those reasons no longer apply.  While the constant <apll>1x</apll> might have the same precision as the constant <apll>1.0</apll>, the difference in latent precision between the two is vast.  In fact, in order for datatype propagation of rational and VFP numbers to work at all, we must be careful <b>not</b> to demote them automatically to a smaller datatype.  Otherwise, it would require an intolerable degree of analysis on the part of the programmer to ensure that the desired datatype (rational or VFP) remains in effect throughout a calculation.</p>
<p class="note">With rational and VFP numbers those reasons no longer apply.  While the constant <apll>1<pn>x</pn></apll> might have the same precision as the constant <apll>1.0</apll>, the difference in latent precision between the two is vast.  In fact, in order for datatype propagation of rational and VFP numbers to work at all, we must be careful <b>not</b> to demote them automatically to a smaller datatype.  Otherwise, it would require an intolerable degree of analysis on the part of the programmer to ensure that the desired datatype (rational or VFP) remains in effect throughout a calculation.</p>


===Conversions===
===Conversions===


<p>To convert manually from one datatype to another,</p>
<p>To convert manually from one datatype to another, use the system function <apll>⎕DC</apll> to convert any numeric datatype</p>


* Integer to rational, add <apll>0x</apll>
* To 64-bit Integer, use <apll>'i' ⎕DC R</apll>
* Integer, floating point, or rational number to VFP, add <apll>0v</apll>
* To 64-bit Floating Point, use <apll>'f' ⎕DC R</apll>
* Rational integer to a 64-bit integer (possibly losing some precision), use <apll>⍎⍕</apll>
* To Multiple Precision Integer/Rational, use <apll>'r' ⎕DC R</apll>
* Rational non-integer to a floating point number (possibly losing some precision), use <apll>⍎⍕0v+</apll>
* To Multiple Precision Floating point, use <apll>'v' ⎕DC R</apll>
* VFP number to a floating point number (possibly losing some precision), use <apll>⍎⍕</apll>
* VFP number to rational &mdash; at the moment, no APL method exists although the underlying libraries do have such a function


===Comparisons===
===Comparisons===


<p>Comparisons between two rational numbers or a rational number and any other integer is exact &mdash; just as they are between integers.</p>
<ul><li><p>Comparisons between two rational numbers or a rational number and any other integer is exact &mdash; just as they are between integers.</p></li>
 
<li><p>Comparisons between a rational number and a floating point number convert both arguments to VFP numbers and compare the two as below.</p></li>
 
<li><p>Comparisons between a VFP number and any other number is sensitive to the current setting of Comparison Tolerance (<apll>⎕CT</apll>) &mdash; just as they are between floating point numbers.</p></li>
 
</ul>


<p>Comparisons between a VFP number and any other number is sensitive to <apll>⎕CT</apll> &mdash; just as they are between floats.</p>
<p>That is, comparisons continue the analogy between integers and rationals as well as floats and VFPs.</p>


===Integer Tolerance===
===Integer Tolerance===


<p>Both rational and VFP numbers may be used where the system ordinarily requires an integer (such axis coordinates, indexing, left argument to structural primitives, etc.) just as the system tolerates floating point numbers in those contexts if they are sufficiently near an integer.  In all cases, the system attempts to convert the non-integer to an integer using the fixed system  comparison tolerance (at the moment, <apll>3E¯15</apll>).</p>
<p>Both rational and VFP numbers may be used where the system ordinarily requires an integer (such axis coordinates, indexing, left argument to structural primitives, etc.) just as the system tolerates floating point numbers in those contexts if they are sufficiently near an integer.  In all cases, the system attempts to convert the non-integer to an integer using the fixed system  comparison tolerance (at the moment, <apll>3<pn>E</pn>¯15</apll>).</p>


===Infinities===
===Infinities===
Line 161: Line 203:
<p>Support for <apll>±∞</apll> has been extended to rational and VFP numbers in the same manner as it applies to 64-bit integers and 64-bit floats.  That is, the same cases covered by the system variable <apll>⎕IC</apll> (Indeterminate Control) also apply to infinite rational and VFP numbers.  Moreover, infinite numeric constants may be entered, for example, as</p>
<p>Support for <apll>±∞</apll> has been extended to rational and VFP numbers in the same manner as it applies to 64-bit integers and 64-bit floats.  That is, the same cases covered by the system variable <apll>⎕IC</apll> (Indeterminate Control) also apply to infinite rational and VFP numbers.  Moreover, infinite numeric constants may be entered, for example, as</p>


* <apll>∞x</apll>
* <apll>∞<pn>x</pn></apll>
* <apll>∞r1</apll>
* <apll>∞<pn>r</pn>1</apll>
* <apll>∞v</apll>
* <apll>∞<pn>v</pn></apll>
* <apll>∞v0</apll>
* <apll>∞<pn>v</pn>0</apll>


<p>Also constants such as <apll>2r∞</apll> resolve to <apll>0x</apll>.</p>
<p>Also constants such as <apll>2<pn>r</pn>∞</apll> resolve to <apll>0<pn>x</pn></apll>.</p>


===New And/Or Different Behavior===
===New And/Or Different Behavior===


<ul>
<ul>
   <li><p>Both roll (<apll>?R</apll>) and deal (<apll>L?R</apll>) on rational integers use a built-in random number generator so as to use the entire range of rational integers – this algorithm uses its own internal seeds that are much more complicated than the simple integer seed that is <apll>⎕RL</apll>.</p>
   <li><p>Both roll (<apll>?R</apll>) and deal (<apll>L?R</apll>) on rational integers use a built-in random number generator so as to use the entire range of rational integers – this algorithm uses its own internal seeds that are much more complicated than the simple integer seed that is <apll>⎕RL</apll> (Random Link).  Thus <apll>⎕RL</apll> is unchanged by these functions on rationals.</p>


<p>For example, if you need <b>really</b> large random numbers</p>
<p>For example, if you need <b>really</b> large random numbers</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?10*60x<br />
<apll><pre>
370857192605742854709703007683731949504799559659692534573173</apll>
      ?10*60<pn>x</pn>
370857192605742854709703007683731949504799559659692534573173</pre></apll>
</li>
</li>
   <li><p>At the moment, matrix inverse (<apll>⌹R</apll>) and matrix division (<apll>L⌹R</apll>) on rational or VFP arguments each have two limitations above and beyond that of normal conformability:</p>
   <li><p>Matrix inverse (<apll>⌹R</apll>) and matrix division (<apll>L⌹R</apll>) on rational or VFP arguments each have two limitations above and beyond that of normal conformability:</p>
     <ul>
     <ul>
       <li><p>for a square right argument that it be non-singular, and</p></li>
       <li><p>for a square right argument that it be non-singular, and</p></li>
       <li><p>for an overdetermined (<apll>>/⍴R</apll>) right argument that the symmetric matrix <apll>(⍉R)+.×R</apll> be non-singular.</p></li>
       <li><p>for an overdetermined (<apll>&gt;/⍴R</apll>) right argument that the symmetric matrix <apll>(⍉R)+.×R</apll> be non-singular.</p></li>
     </ul>
     </ul>
     <p>These limitations are due to the FLINT library used to implement Matrix Inverse/Divide on rational and VFP numbers.</p>
     <p>These limitations are due to the algorithm ([http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination Gauss-Jordan Elimination]) used to implement Matrix Inverse/Divide on rational and VFP numbers.</p>


     <p>Integer and floating point arguments are not subject to these limitations because they use a more general algorithm (Singular Value Decomposition) that produces a unique result even for singular arguments (e.g., <apll>⌹5 3⍴0</apll>).</p></li>
     <p>Integer and floating point arguments are not subject to these limitations because they use a more general algorithm ([http://en.wikipedia.org/wiki/Singular_value_decomposition Singular Value Decomposition]) that produces a unique result even for singular arguments (e.g., <apll>⌹5 3⍴0</apll>).</p></li>
</ul>
</ul>


===Conclusion===
===Conclusions===


<p>The new datatypes offer several benefits:</p>
<p>The new datatypes offer several benefits:</p>
Line 194: Line 237:
* They extend the precision of existing integer and floating point datatypes to a much greater level.
* They extend the precision of existing integer and floating point datatypes to a much greater level.
* As integer blows up to floating point, rational blows up to VFP, providing a natural parallel progression for irrational and transcendental primitive functions.
* As integer blows up to floating point, rational blows up to VFP, providing a natural parallel progression for irrational and transcendental primitive functions.
* There is a close similarity between integer and rational numbers as well as floating point and VFP numbers.
* Datatype propagation without demotion allows one to code an algorithm in either of the new types easily and without the need for detailed analysis of the datatype in intermediate results.
* Datatype propagation without demotion allows one to code an algorithm in either of the new types easily and without the need for detailed analysis of the datatype in intermediate results.
* All primitives extend naturally to encompass the new types as numbers.
* All primitives extend naturally to encompass the new types as numbers.
* The notation for constants builds on existing point notation formats.


===Acknowledgments===
===Acknowledgments===
Line 201: Line 246:
<p>The designers of J are thanked for having the foresight to include [http://www.jsoftware.com/help/dictionary/dictg.htm rational numbers] as a separate datatype.</p>
<p>The designers of J are thanked for having the foresight to include [http://www.jsoftware.com/help/dictionary/dictg.htm rational numbers] as a separate datatype.</p>


<p>The following GPL libraries have been used to provide support for these datatypes:</p>
<p>The following LGPL libraries have been used to provide support for these datatypes:</p>


* MPIR (Multiple Precision Integers and Rationals) at [http://www.mpir.org mpir.org].
* MPIR (Multiple Precision Integers and Rationals) at [http://www.mpir.org mpir.org].
* MPFR (Multiple Precision Floating-Point Reliable Library ) at [http://www.mpfr.org mpfr.org].
* MPFR (Multiple Precision Floating-Point Reliable Library ) at [http://www.mpfr.org mpfr.org].
* FLINT (Fast Library for Number Theory) at [http://www.flintlib.org flintlib.org].
 
===References===
 
<p>For a PDF version of this page, view it [http://www.sudleyplace.com/APL/Rational%20&%20Variable-Precision%20FP.pdf here].</p>

Latest revision as of 10:57, 17 October 2019

Introduction

Rational numbers complement the existing 64-bit integer datatype to provide infinite precision (or WS FULL) at the cost of some performance. Similarly, Variable-precision Floating Point (VFP) numbers complement the existing 64-bit floating point (IEEE-754) datatype to provide more precision (as much as the user cares to specify) again with some cost in performance.

There is no separate datatype for infinite precision Integers. Instead, they are represented as a special case of rational numbers. In the discussion below, the phrase rational integer means a rational number whose denominator is one.

Throughout this discussion the similarity between integer and rational numbers as well as floating point and VFP numbers will become apparent.

Rationale

Simply put: precision. If the 53 bits of precision in the floating point result of, say, 2÷3 is not enough, you now have two more choices: one as an exact number, and one as a binary floating point number with as much precision as you care to specify.

For example,

      ⎕PP←60 ⋄ ⎕FPC←512 
      2÷3 
0.6666666666666666 
      2÷3x
2r3 
      2÷3v
0.666666666666666666666666666666666666666666666666666666666667 
      2*200 
1.6069380442589903E60 
      2*200x 
1606938044258990275541962092341162602522202993782792835301376 
      ○1 
3.141592653589793 
      ○1v 
3.14159265358979323846264338327950288419716939937510582097494

Constants

Rational constants may be entered by suffixing an integer constant with an x (for a rational integer) or separating the numerator and denominator with an r (for a rational number) as follows:

  • 123x for the constant 123 (the suffix x is but a shorthand for r1)
  • 123r4567 for the constant 123÷4567

VFP constants may be entered by suffixing the integer or floating point constant with a v as follows:

  • 123v for the constant 123
  • 123.4567v for the constant 123.4567
  • 123.4567e3v for the constant 123.4567E3

The above formats for constants (except for the suffix x) may be used in other constants such as 1r4p2 to generate a shorter and more accurate value for π2/4 than, say, ((○1)*2)÷4.

Precision

Rational numbers have infinite precision. They are stored with a separate numerator and denominator, both of which are exact numbers in the sense that their size (and hence precision) grows limited only by the available workspace.

VFP numbers have user-controlled variable precision, and each number may have a different precision. The default precision at startup is controlled by the value of the system variable ⎕FPC. The system default of this value is 128 in units of bits of precision of the mantissa (the digits) of the number, not counting the exponent (which is of fixed size). The current precision may be changed as needed by assigning a new value to the system variable. All newly created VFP numbers will have the new precision – the precision of VFP numbers already present in the workspace does not change.

Generally, precision is set once for a particular application and unchanged thereafter. Although not recommended, it is possible to mix VFP numbers of different precisions in a single array – presumably you really know what you are doing. The system function 0 ⎕DR may be used to display an array's precision(s).

Datatype Propagation

Generally, the datatype of constants propagates through a calculation. That is, if you start with a rational number and don't calculate with irrational or transcendental functions, you'll end up with a rational result, and if you start with a VFP number, you'll end up with a VFP result.

An example from the programming problems site ProjectEuler.net illustrates this point. Problem #48 asks what are the low-order ten digits of the sum of the first thousand instances of NN?

The obvious expression ¯10↑⍕+/*⍨⍳1000, at first sight, seems to solve the problem until you realize that it quickly runs afoul of the limited precision of 64-bit integer and floating point numbers. Clearly, this is a problem for the infinite precision of rational integers.

As ⍳1000 generates the first thousand integers as an integer datatype (actually an Arithmetic Progression Array), ⍳1000x generates the same values as rational integers. Next, *⍨⍳1000x generates the first thousand instances of NN as exact rational integers, and unlike its integer counterpart, there is no overflow to floating point, just an increase in precision (as well as space used in the workspace). Then, +/*⍨⍳1000x sums them into a single 3001-digit rational integer, and finally ¯10↑⍕+/*⍨⍳1000x converts the large integer to characters and extracts the low-order ten digits — 9110846700 — all in a small number of milliseconds.

Note how we started with an obvious expression that failed because of its limited precision, and made a single change to suffix the constant 1000 with an x to convert it to a rational integer which then propagates through the calculation with infinite precision to yield the correct result.

Display

Rational integers are displayed as an integer with no special adornment; rational non-integers are displayed as a numerator and denominator separated by an r as in 34r9. As with the integer datatype, the numerator and denominator of a rational number are displayed exactly, unaffected by the current setting for Printing Precision (⎕PP).


      !40 
8.159152832478977E47 
      !40x
815915283247897734345611269596115894272000000000 
      +\÷⍳10x
1 3r2 11r6 25r12 137r60 49r20 363r140 761r280 7129r2520 7381r2520 

VFP numbers are displayed as decimal numbers to the precision inherent in the number or ⎕PP, whichever is smaller, just as floating point numbers are displayed. For example,

      ⎕PP←100 
      ⎕FPC←64 
      ○1 
3.141592653589793 
      ○1x
3.14159265358979323851
      ⎕FPC←128 
 ○1x
3.141592653589793238462643383279502884195

where both of the above displays were limited by the precision of the number, not ⎕PP.

However, the first of the following displays is limited by ⎕PP:

      ⎕FPC←128 
      ⎕PP←20 
      !40v
81591528324789773435____________________________ 
      ⎕PP←80 
      !40v
8159152832478977343456112695961158942720________

In the last display, the current setting of Printing Precision is large enough, but the current setting of the Floating Point Control (⎕FPC) whose value is in bits is too small, so the display is truncated.

Formatted Display

The system function ⎕FMT has been enhanced to allow formatting of rational numbers via the (new) R-format specifier. For example,

      'R4.2' ⎕FMT ∘.÷⍨⍳6x
1   1r2 1r3 1r4 1r5 1r6
2   1   2r3 1r2 2r5 1r3
3   3r2 1   3r4 3r5 1r2
4   2   4r3 1   4r5 2r3
5   5r2 5r3 5r4 1   5r6
6   3   2   3r2 6r5 1  

Moreover, the Symbol Substitution (S<…>) feature of ⎕FMT allows you to substitute a different symbol for the default r used to separate the numerator and denominator of a rational number, as in

      'S<r/>R4.2' ⎕FMT ∘.÷⍨⍳6x
1   1/2 1/3 1/4 1/5 1/6
2   1   2/3 1/2 2/5 1/3
3   3/2 1   3/4 3/5 1/2
4   2   4/3 1   4/5 2/3
5   5/2 5/3 5/4 1   5/6
6   3   2   3/2 6/5 1  

Datatype Promotion

For the most part, rational numbers beget rational numbers and VFP numbers beget VFP numbers. However, when irrational, transcendental, and certain other functions are used, rational numbers beget VFP numbers. For example,

      *1 
2.718281828459045 
      *1x
2.718281828459045235360287471352662497759

where the datatype of the two results are floating point and VFP, respectively. That is, in a manner similar to how some primitive functions with integer arguments may return floating point results when a rational number is used as an argument to a primitive function that can't return a result with infinite precision, it returns a VFP number.

The reason irrational, transcendental, and certain other functions on rational numbers do not return rational numbers is that, by definition, the result of such a function is, in general, not representable as a rational number; instead, VFP numbers are better suited to represent irrational results where the end user may control exactly how much precision is desired in an obviously inexact number.

Two special functions are the prime decomposition (πR)/number theoretic (LπR) functions. In these cases, fractional or VFP right arguments are converted to integers or rational integers, respectively, which is the datatype of the result except for 0πR (Primality Test) which always returns a Boolean result regardless of the type of R.

Ignoring purely structural functions, the list of functions that produce VFP numbers given rational numbers is as follows:

  • Power: *R and L*R (except when R is a 32-bit integer, in which case the result is a rational number)
  • Logarithm: ⍟R and L⍟R
  • Pi Times and Circle functions: ○R and L○R
  • Root: √R and L√R
  • Factorial and Binomial: !R and L!R (except when the arguments are rational integers, in which case the result is a rational integer)

Beyond the ones mentioned above, the list of functions that don't produce a rational or VFP result given those argument(s) is as follows:

  • Depth: (Integer)
  • Dyadic Comparison: = ≠ < ≤ ≥ > ≡ ≢ (Boolean)
  • Nand and Nor: ⍲ ⍱ (Boolean)
  • Grade Up/Down: ⍋ ⍒ (Integer)
  • Index Of: (Integer)
  • Member Of: (Boolean)
  • Find: (Boolean)
  • Subset and Superset: ⊆ ⊇ (Boolean)
  • Format: (Character)

Otherwise, rational argument(s) produce rational result(s) and VFP argument(s) produce VFP result(s).

Datatype Demotion

It is common in APL implementations to demote datatypes where appropriate. For example, the constant 1.0 might actually be represented as an integer or even Boolean datatype. The idea is there is no loss of precision and the storage is typically smaller which might lead to a more efficient algorithm when next used, so why not?

With rational and VFP numbers those reasons no longer apply. While the constant 1x might have the same precision as the constant 1.0, the difference in latent precision between the two is vast. In fact, in order for datatype propagation of rational and VFP numbers to work at all, we must be careful not to demote them automatically to a smaller datatype. Otherwise, it would require an intolerable degree of analysis on the part of the programmer to ensure that the desired datatype (rational or VFP) remains in effect throughout a calculation.

Conversions

To convert manually from one datatype to another, use the system function ⎕DC to convert any numeric datatype

  • To 64-bit Integer, use 'i' ⎕DC R
  • To 64-bit Floating Point, use 'f' ⎕DC R
  • To Multiple Precision Integer/Rational, use 'r' ⎕DC R
  • To Multiple Precision Floating point, use 'v' ⎕DC R

Comparisons

  • Comparisons between two rational numbers or a rational number and any other integer is exact — just as they are between integers.

  • Comparisons between a rational number and a floating point number convert both arguments to VFP numbers and compare the two as below.

  • Comparisons between a VFP number and any other number is sensitive to the current setting of Comparison Tolerance (⎕CT) — just as they are between floating point numbers.

That is, comparisons continue the analogy between integers and rationals as well as floats and VFPs.

Integer Tolerance

Both rational and VFP numbers may be used where the system ordinarily requires an integer (such axis coordinates, indexing, left argument to structural primitives, etc.) just as the system tolerates floating point numbers in those contexts if they are sufficiently near an integer. In all cases, the system attempts to convert the non-integer to an integer using the fixed system comparison tolerance (at the moment, 3E¯15).

Infinities

Support for ±∞ has been extended to rational and VFP numbers in the same manner as it applies to 64-bit integers and 64-bit floats. That is, the same cases covered by the system variable ⎕IC (Indeterminate Control) also apply to infinite rational and VFP numbers. Moreover, infinite numeric constants may be entered, for example, as

  • x
  • r1
  • v
  • v0

Also constants such as 2r resolve to 0x.

New And/Or Different Behavior

  • Both roll (?R) and deal (L?R) on rational integers use a built-in random number generator so as to use the entire range of rational integers – this algorithm uses its own internal seeds that are much more complicated than the simple integer seed that is ⎕RL (Random Link). Thus ⎕RL is unchanged by these functions on rationals.

    For example, if you need really large random numbers

          ?10*60x
    370857192605742854709703007683731949504799559659692534573173
  • Matrix inverse (⌹R) and matrix division (L⌹R) on rational or VFP arguments each have two limitations above and beyond that of normal conformability:

    • for a square right argument that it be non-singular, and

    • for an overdetermined (>/⍴R) right argument that the symmetric matrix (⍉R)+.×R be non-singular.

    These limitations are due to the algorithm (Gauss-Jordan Elimination) used to implement Matrix Inverse/Divide on rational and VFP numbers.

    Integer and floating point arguments are not subject to these limitations because they use a more general algorithm (Singular Value Decomposition) that produces a unique result even for singular arguments (e.g., ⌹5 3⍴0).

Conclusions

The new datatypes offer several benefits:

  • They extend the precision of existing integer and floating point datatypes to a much greater level.
  • As integer blows up to floating point, rational blows up to VFP, providing a natural parallel progression for irrational and transcendental primitive functions.
  • There is a close similarity between integer and rational numbers as well as floating point and VFP numbers.
  • Datatype propagation without demotion allows one to code an algorithm in either of the new types easily and without the need for detailed analysis of the datatype in intermediate results.
  • All primitives extend naturally to encompass the new types as numbers.
  • The notation for constants builds on existing point notation formats.

Acknowledgments

The designers of J are thanked for having the foresight to include rational numbers as a separate datatype.

The following LGPL libraries have been used to provide support for these datatypes:

  • MPIR (Multiple Precision Integers and Rationals) at mpir.org.
  • MPFR (Multiple Precision Floating-Point Reliable Library ) at mpfr.org.

References

For a PDF version of this page, view it here.