Discussion:
[Bug-apl] Access to rational number numerator and denominator
Frederick Pitts
2017-08-28 21:24:04 UTC
Permalink
Hello,

Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know how. If
no, can a mechanism be implemented?

Respectfully,

Fred
Louis de Forcrand
2017-08-28 22:08:59 UTC
Permalink
Hi,

No APL kb with me right now, sorry :(

1 LCM n

gives the numerator of a fraction (floating or exact). If you need the denominator, do the same with the inverse of n. If you need both, for example:

1 LCM n POW 1 _1

Cheers,
Louis
Post by Frederick Pitts
Hello,
Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know how. If
no, can a mechanism be implemented?
Respectfully,
Fred
Frederick Pitts
2017-08-29 00:59:19 UTC
Permalink
Louis,

Thanks for the quick response.

After working with the technique a bit, I observe that as long
as the rational number denominator is well within the range of integers
representable by floating numbers, 1 ∧ n returns the correct result.
But if the absolute value of the denominator exceeds 2 ⋆ 35, the
technique returns incorrect results. For instance,

fiMax ← × / 53 ⍴ 2 ⍝ largest integer 53-bit f.p. mantissa holds
fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ fiMax ⍝ Correct result for fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ ÷ fiMax ⍝ Incorrect result. 1 is numerator of reciprocal.
╔═╗
║0║
╚═╝

I was hoping to be able to access correct rational number parts
even when they approach the ⎕SYL[20;2] limit of 9200000000000000000.
It seems a shame to loose so much of the integer range because floating
point operations are sneaking into the numerator and denominator access
methods.

Regards,

Fred
Post by Louis de Forcrand
Hi,
No APL kb with me right now, sorry :(
1 LCM n
gives the numerator of a fraction (floating or exact). If you need
the denominator, do the same with the inverse of n. If you need both,
1 LCM n POW 1 _1
Cheers,
Louis
Post by Frederick Pitts
Hello,
Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know
how. If
no, can a mechanism be implemented?
Respectfully,
Fred
Elias Mårtenson
2017-08-29 02:33:15 UTC
Permalink
The true benefit of rational numbers is realised once there is support for
bigints. JÃŒrgen has suggested that that is planned, and personally I can't
wait.

Regards,
Elias
Post by Frederick Pitts
Louis,
Thanks for the quick response.
After working with the technique a bit, I observe that as long
as the rational number denominator is well within the range of integers
representable by floating numbers, 1 ∧ n returns the correct result.
But if the absolute value of the denominator exceeds 2 ⋆ 35, the
technique returns incorrect results. For instance,
fiMax ← × / 53 ⍎ 2 ⍝ largest integer 53-bit f.p. mantissa holds
fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ fiMax ⍝ Correct result for fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ ÷ fiMax ⍝ Incorrect result. 1 is numerator of reciprocal.
╔═╗
║0║
╚═╝
I was hoping to be able to access correct rational number parts
even when they approach the ⎕SYL[20;2] limit of 9200000000000000000.
It seems a shame to loose so much of the integer range because floating
point operations are sneaking into the numerator and denominator access
methods.
Regards,
Fred
Post by Louis de Forcrand
Hi,
No APL kb with me right now, sorry :(
1 LCM n
gives the numerator of a fraction (floating or exact). If you need
the denominator, do the same with the inverse of n. If you need both,
1 LCM n POW 1 _1
Cheers,
Louis
Post by Frederick Pitts
Hello,
Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know how. If
no, can a mechanism be implemented?
Respectfully,
Fred
Christian Robert
2017-08-29 02:48:33 UTC
Permalink
what is "bigints" ?

larger than ~62 bits or 9200000000000000000 ?

If so, I would agree 100%. Having a limit that is less than exact 64 bits (signed or unsigned)
is wrong. encode & decode can't manage more than ~62-~63 bits limit.


my 2 cents,
Xtian.
The true benefit of rational numbers is realised once there is support for bigints. Jürgen has suggested that that is planned, and personally I can't wait.
Regards,
Elias
Louis,
        Thanks for the quick response.
        After working with the technique a bit, I observe that as long
as the rational number denominator is well within the range of integers
representable by floating numbers, 1 ∧ n returns the correct result.
But if the absolute value of the denominator exceeds 2 ⋆ 35, the
technique returns incorrect results.  For instance,
      fiMax ← × / 53 ⍴ 2 ⍝ largest integer 53-bit f.p. mantissa holds
      fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
      1 ∧ fiMax ⍝ Correct result for fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
      1 ∧ ÷ fiMax ⍝ Incorrect result. 1 is numerator of reciprocal.
╔═╗
║0║
╚═╝
        I was hoping to be able to access correct rational number parts
even when they approach the ⎕SYL[20;2] limit of 9200000000000000000.
It seems a shame to loose so much of the integer range because floating
point operations are sneaking into the numerator and denominator access
methods.
Regards,
Fred
Post by Louis de Forcrand
Hi,
No APL kb with me right now, sorry :(
1 LCM n
gives the numerator of a fraction (floating or exact). If you need
the denominator, do the same with the inverse of n. If you need both,
1 LCM n POW 1 _1
Cheers,
Louis
Post by Frederick Pitts
Hello,
    Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts?  If yes, please let me know
how.  If
no, can a mechanism be implemented?
Respectfully,
Fred
Elias Mårtenson
2017-08-29 02:57:50 UTC
Permalink
A bigint is an arbitrary-precision integer. Such as what is provided in
Lisp:

In Lisp, I get the correct rational number:

CL> *(/ (expt 2 1000) 3)*
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376/3

In GNU APL:

* ⎕PS←1 0*
* 3÷⍚2⋆1000*
3.571695357E300

With bigints, the APL result would be similar to that of Lisp.

Regards,
Elias
Post by Christian Robert
what is "bigints" ?
larger than ~62 bits or 9200000000000000000 ?
If so, I would agree 100%. Having a limit that is less than exact 64 bits
(signed or unsigned)
is wrong. encode & decode can't manage more than ~62-~63 bits limit.
my 2 cents,
Xtian.
Post by Elias MÃ¥rtenson
The true benefit of rational numbers is realised once there is support
for bigints. JÃŒrgen has suggested that that is planned, and personally I
can't wait.
Regards,
Elias
Louis,
Thanks for the quick response.
After working with the technique a bit, I observe that as long
as the rational number denominator is well within the range of integers
representable by floating numbers, 1 ∧ n returns the correct result.
But if the absolute value of the denominator exceeds 2 ⋆ 35, the
technique returns incorrect results. For instance,
fiMax ← × / 53 ⍎ 2 ⍝ largest integer 53-bit f.p. mantissa holds
fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ fiMax ⍝ Correct result for fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ ÷ fiMax ⍝ Incorrect result. 1 is numerator of reciprocal.
╔═╗
║0║
╚═╝
I was hoping to be able to access correct rational number parts
even when they approach the ⎕SYL[20;2] limit of 9200000000000000000.
It seems a shame to loose so much of the integer range because floating
point operations are sneaking into the numerator and denominator access
methods.
Regards,
Fred
Post by Louis de Forcrand
Hi,
No APL kb with me right now, sorry :(
1 LCM n
gives the numerator of a fraction (floating or exact). If you need
the denominator, do the same with the inverse of n. If you need
both,
Post by Louis de Forcrand
1 LCM n POW 1 _1
Cheers,
Louis
On 28 Aug 2017, at 23:24, Frederick Pitts <
Hello,
Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know how. If
no, can a mechanism be implemented?
Respectfully,
Fred
Frederick Pitts
2017-08-29 10:00:41 UTC
Permalink
Guys,
To make it clear, I'm not asking for bigints. I'm just seeking
to get the maximum benefit of having rational numbers with, as Xtian
says, ~63-bit numerator and denominators. As long as you limit
yourself to using addition, subtraction, multiplication, and division
and avoid overrunning the ~63-bit limitation, the rationals work as
expected up to that ~63-bit limit AFAIK. But use any other function,
say power (⋆) or least common multiple (∧), difficult to predict
floating-point conversions reduce the ~63-bits to 53-bits or less and
produce incorrect results.
Louis recommended the LCM function as a means of separating a
rational into its numerator and denominator so I could program with
them individually. Louis mentioned that he did not have access to a
APL keyboard while composing his email, so I took his use of LCM to
mean the nativeGnu-APL ∧ function and that is why I tried using
it. Turns out that Ken Iverson presented APL code for GCD and LCM
functions in his "Notation as a Tool for Thought" Turing Award paper
that avoid floating point arithmetic. Preliminary testing indicates
those implementations will do the job.
Still, if Gnu-APL is going to support rational numbers, an
efficient mechanism for accessing numerators and denominators similar
to the circle function (○) with 9 and 11 left arguments for accessing
the real and imaginary parts of complex numbers is needed. Could
implementing this functionality be added to someone's to-do list,
please?
Regards,
Post by Elias MÃ¥rtenson
A bigint is an arbitrary-precision integer. Such as what is provided
CL> (/ (expt 2 1000) 3)
107150860718626732094842504906000181056140481170553360744375038837035
105112493612249319837881569585812759467291755314682518714528569231404
359845775746985748039345677748242309854210746050623711418779541821530
464749835819412673987675591655439460770629145711964776865421676604298
31652624386837205668069376/3
⎕PS←1 0
3÷⍚2⋆1000
3.571695357E300
With bigints, the APL result would be similar to that of Lisp.
Regards,
Elias
Post by Christian Robert
what is "bigints" ?
larger than ~62 bits or 9200000000000000000 ?
If so, I would agree 100%. Having a limit that is less than exact
64 bits (signed or unsigned)
is wrong. encode & decode can't manage more than ~62-~63 bits limit.
my 2 cents,
Xtian.
Post by Elias MÃ¥rtenson
The true benefit of rational numbers is realised once there is
support for bigints. JÃŒrgen has suggested that that is planned,
and personally I can't wait.
Regards,
Elias
Louis,
Thanks for the quick response.
After working with the technique a bit, I observe that as long
as the rational number denominator is well within the range of integers
representable by floating numbers, 1 ∧ n returns the correct result.
But if the absolute value of the denominator exceeds 2 ⋆ 35, the
technique returns incorrect results. For instance,
fiMax ← × / 53 ⍎ 2 ⍝ largest integer 53-bit f.p.
mantissa holds
fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ fiMax ⍝ Correct result for fiMax
╔════════════════╗
║9007199254740992║
╚════════════════╝
1 ∧ ÷ fiMax ⍝ Incorrect result. 1 is numerator of
reciprocal.
╔═╗
║0║
╚═╝
I was hoping to be able to access correct rational number parts
even when they approach the ⎕SYL[20;2] limit of
9200000000000000000.
It seems a shame to loose so much of the integer range
because floating
point operations are sneaking into the numerator and
denominator access
methods.
Regards,
Fred
Post by Louis de Forcrand
Hi,
No APL kb with me right now, sorry :(
1 LCM n
gives the numerator of a fraction (floating or exact). If
you need
Post by Louis de Forcrand
the denominator, do the same with the inverse of n. If you
need both,
Post by Louis de Forcrand
1 LCM n POW 1 _1
Cheers,
Louis
Post by Frederick Pitts
Hello,
Is there an existing mechanism for accessing rational
number
Post by Louis de Forcrand
Post by Frederick Pitts
numerator and denominator parts analogous to that for
accessing
Post by Louis de Forcrand
Post by Frederick Pitts
complex
number real and imaginary parts? If yes, please let me
know
Post by Louis de Forcrand
Post by Frederick Pitts
how. If
no, can a mechanism be implemented?
Respectfully,
Fred
Juergen Sauermann
2017-08-29 10:51:30 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi Fred,<br>
<br>
the existing mechanism is <b>27 ⎕CR</b> B to get the numerator of
B and <b>28 ⎕CR</b> to get the denominator <b>&gt; 0</b><br>
of a quotient or 0 if the number is not a quotient (and then <b>27
</b><b>⎕CR</b> is the<b> double B</b> cast to </font><font
face="Helvetica, Arial, sans-serif"><b>uint64</b></font><font
face="Helvetica, Arial, sans-serif"><b>_t</b>.<br>
<br>
Unfortunately there was a bug <b>28 ⎕CR</b> returning always <b>0</b>.
Fixed in<b> SVN 1004</b>.<br>
</font><br>
Best Regards,<br>
/// Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 08/28/2017 11:24 PM, Frederick Pitts
wrote:<br>
</div>
<blockquote cite="mid:***@comcast.net"
type="cite">
<pre wrap="">Hello,

Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know how. If
no, can a mechanism be implemented?

Respectfully,

Fred


</pre>
</blockquote>
<br>
</body>
</html>
Frederick Pitts
2017-08-30 05:59:50 UTC
Permalink
Hello JÃŒrgen,
After reading your email and the apl.html documentation on ⎕CR
and so, I've have two questions:
1) How does one know B is a rational number? The apl.html
documentation for 26 ⎕CR B does not showthat there is a cell type for
rationals and I could not locate one in the source code. Admittedly my
examination was cursory. 2) Why is a real number being used to
represent the numerator of a rational number? I thought ~63-
bitintegers would consistently be used to represent the parts of a
rational. 26 ⎕CR ÷ 2 yields 32 indicating that 1 is being represented
by a real rather than an integer. That is puzzling to me. It almost
seems irrational, no pun intended.
Regards.
Post by Juergen Sauermann
Hi Fred,
the existing mechanism is 27 ⎕CR B to get the numerator of
B and 28 ⎕CR to get the denominator > 0
of a quotient or 0 if the number is not a quotient (and then 27
⎕CR is the double B cast to uint64_t.
Unfortunately there was a bug 28 ⎕CR returning always 0.
Fixed in SVN 1004.
Best Regards,
/// JÃŒrgen
On 08/28/2017 11:24 PM, Frederick Pitts
Post by Frederick Pitts
Hello,
Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know
how. If
no, can a mechanism be implemented?
Respectfully,
Fred
Juergen Sauermann
2017-08-30 11:43:20 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi Fred,<br>
<br>
Rational. and non-rational numbers use the same Cell type
(FloatCell).<br>
</font><br>
Non-Rational numbers have a <b>denominator = 0</b> and store their
value in <b>value.fval.u1.flt</b> (which is a <b>double</b> in
union <b>u1</b>)<br>
Rational numbers have a <b>denominator &gt;= 1</b> and store their
value in <b>value.fval.u1.num (</b>which is an <b>int64_t</b> in
union <b>u1</b>)<br>
<br>
So basically a rational number uses the same space that a complex
number would use for its imaginary part (to avoid an<br>
increase of the Cell size by introducing rational numbers).<br>
<br>
<b>26 ⎕CR </b>has nothing to do with all of this.<br>
<b>27 ⎕CR</b> returns the primary value of a Cell (which is always
an integer that was possibly cast from a different type like<br>
APL_Char or APL_Float to APL_Integer = int64_t).<br>
<b>28 ⎕CR </b>is 0 or the secondary value of a Cell (imaginary part
for Complex Cells, Denominator for Rational FloatCells, ...),<br>
and is also always an APL_Integer.<br>
<br>
<font face="Courier New, Courier, monospace"><b>      28 ⎕CR ÷ 2</b><b><br>
</b><b>2</b></font><br>
<br>
So the denominator is NOT represented by a <b>double</b> but by an
<b>int64_t</b>.<br>
<br>
The idea behind <b>27/28 ⎕CR</b> is to get the underlying machine
representation of the value(s) store in a Cell (similar to dyadic <b>⎕CR</b><br>
in good old APL68000) which by chance is the denominator of a
rational float value.<br>
<br>
Hope that explains it,<br>
<br>
/// Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 08/30/2017 07:59 AM, Frederick Pitts
wrote:<br>
</div>
<blockquote cite="mid:***@comcast.net"
type="cite">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<div>Hello Jürgen,</div>
<div><br>
</div>
<div> After reading your email and the apl.html documentation on
⎕CR and so, I've have two questions:</div>
<div><br>
</div>
<div> 1) How does one know B is a rational number? The apl.html
documentation for 26 ⎕CR B does not show</div>
<div>that there is a cell type for rationals and I could not
locate one in the source code. Admittedly my examination was
cursory. </div>
<div> 2) Why is a real number being used to represent the
numerator of a rational number? I thought ~63-bit</div>
<div>integers would consistently be used to represent the parts of
a rational. 26 ⎕CR ÷ 2 yields 32 indicating that 1 is being
represented by a real rather than an integer. That is puzzling
to me. It almost seems irrational, no pun intended.</div>
<div><br>
</div>
<div>Regards.</div>
<div><br>
</div>
<div><br>
</div>
<div>On Tue, 2017-08-29 at 12:51 +0200, Juergen Sauermann wrote:</div>
<blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px
#729fcf solid;padding-left:1ex"> <font face="Helvetica, Arial,
sans-serif">Hi Fred,<br>
<br>
the existing mechanism is <b>27 ⎕CR</b> B to get the
numerator of B and <b>28 ⎕CR</b> to get the denominator <b>&gt;
0</b><br>
of a quotient or 0 if the number is not a quotient (and then <b>27
</b><b>⎕CR</b> is the<b> double B</b> cast to </font><font
face="Helvetica, Arial, sans-serif"><b>uint64</b></font><font
face="Helvetica, Arial, sans-serif"><b>_t</b>.<br>
<br>
Unfortunately there was a bug <b>28 ⎕CR</b> returning always
<b>0</b>. Fixed in<b> SVN 1004</b>.<br>
</font><br>
Best Regards,<br>
/// Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 08/28/2017 11:24 PM, Frederick
Pitts wrote:<br>
</div>
<blockquote cite="mid:***@comcast.net"
type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf
solid;padding-left:1ex">
<pre wrap="">Hello,

Is there an existing mechanism for accessing rational number
numerator and denominator parts analogous to that for accessing complex
number real and imaginary parts? If yes, please let me know how. If
no, can a mechanism be implemented?

Respectfully,

Fred


</pre>
</blockquote>
<br>
</blockquote>
</blockquote>
<br>
</body>
</html>

Loading...