>>
<<
Usr
Pri
JfC
LJ
Phr
Dic
Voc
!:
Help
Dictionary
G. Extended and Rational Arithmetic
Extended precision integer constants can be entered as a sequence
of decimal digits terminated by an x.
The monad x:
applies to integers and produces extended integers.
For example, the 2-element vector 1234x 56x (or 1234 56x)
is equivalent to x: 1234 56.
Various primitives produce extended results if the argument(s) are extended.
Some verbs f produce floating point (inexact) results
on some extended arguments because the result is not integral;
however, <.@f and >.@f produce extended
integer results when applied to extended integer arguments.
Comparisons involving extended integers are exact. For example:
!40
8.15915e47
!40x
815915283247897734345611269596115894272000000000
*/ x: >: i.40
815915283247897734345611269596115894272000000000
0j_25 ": ! 2000x * i. 5 1 NB. Exponent format, 25 digits
1.0000000000000000000000000e0
3.3162750924506332411753934e5735
1.8288019515140650133147432e12673
2.6839997657267395961163166e20065
5.1841810604808769398058198e27752
] r=: <.@%: 2 * 10 ^ 56x
14142135623730950488016887242
,. *: r + _1 0 1
199999999999999999999999999968972697904100132394908592081
199999999999999999999999999997256969151562033370942366564
200000000000000000000000000025541240399023934346976141049
Rational constants can be entered as the decimal digits of
the numerator and denominator, separated by an r
and preceded by an optional sign. Thus 3r4 is the
rational number three-quarters and _12r5 is
negative 12 divided by 5. Rational numbers are stored and
displayed in a standard form, with the numerator and
denominator relatively prime and the denominator positive.
Thus:
1r2 _1r2 2r4 2r_4 _2r_4 0r9 5 _5
1r2 _1r2 1r2 _1r2 1r2 0 5 _5
Various primitive verbs produce (exact) rational results
if the argument(s) are rational; non-rational verbs produce
(inexact) floating point or complex results when applied to rationals,
if the verb only has a limited number of rational arguments
that produce rational results. (For example, %:y is rational
if the atoms of y are perfect squares; ^0r1
is floating point.) The quotient of two extended integers is
an extended integer (if evenly divisible) or rational (if not).
Comparisons involving two rationals are exact.
Dyadic verbs (e.g. + - * % , = <)
that require argument type conversions do so
according to the following table:
| B I X Q D Z
---+------------------
B | B I X Q D Z B - boolean
I | I I X Q D Z I - integer
X | X X X Q D Z X - extended integer
Q | Q Q Q Q D Z Q - rational
D | D D D D D Z D - floating point
Z | Z Z Z Z Z Z Z - complex
For example, in the expression 2.5+1r2,
the 1r2 is converted to 0.5 before being added
to 2.5, resulting in a floating point 3.
And in the expression 2+1r2, the 2 is
converted to 2r1 before being added to 1r2,
resulting in 5r2.
In particular, a comparison involving a rational and
a floating point number is tolerant,
because the rational argument is first converted into a
floating point number.
The verb x: produces rational approximations
to non-rational arguments.
2%3
0.666667
2%3x
2r3
(+%)/\10$1 NB. Floating point convergents to golden mean
1 2 1.5 1.66667 1.6 1.625 1.61538 1.61905 1.61765 1.61818
(+%)/\x: 10$1 NB. Rational versions of same
1 2 3r2 5r3 8r5 13r8 21r13 34r21 55r34 89r55
|: 2 x: (+%)/\x: 10$1
1 2 3 5 8 13 21 34 55 89
1 1 2 3 5 8 13 21 34 55
(+%)/ 100$1r1
573147844013817084101r354224848179261915075
0j30 ": (+%)/100$1r1 NB. Display 30 decimal places
1.618033988749894848204586834366
H=: % @: >: @: (+/~) @: i. @ x: NB. Hilbert matrix of order n
] h=: H 6
1 1r2 1r3 1r4 1r5 1r6
1r2 1r3 1r4 1r5 1r6 1r7
1r3 1r4 1r5 1r6 1r7 1r8
1r4 1r5 1r6 1r7 1r8 1r9
1r5 1r6 1r7 1r8 1r9 1r10
1r6 1r7 1r8 1r9 1r10 1r11
-/ .* h NB. Determinant of h
1r186313420339200000
~. q: % -/ .* h NB. Unique prime factors of reciprocal of det
2 3 5 7 11
i.&.(p:^:_1) 2*#h NB. Primes less than 2*n
2 3 5 7 11
^ 2r1 NB. ^y is floating point or complex
7.38906
%: 49r25 NB. %: on a rational perfect square is rational
7r5
%: 49r25 10r9
1.4 1.05409
%: _2r1
0j1.41421
1 = 1+10r1^_15 NB. Exact (rational) comparison
0
(1.5-0.5) = 1+10r1^_15 NB. Tolerant (floating point) comparison
1
0.5 = 1r2
1
>>
<<
Usr
Pri
JfC
LJ
Phr
Dic
Voc
!:
Help
Dictionary