Vocabulary/xco

From J Wiki
Jump to: navigation, search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

x: y Extended Precision

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



Converts number y to extended precision, either extended integer or rational.

If y has integer precision, the result is an extended integer.

   ]z=: x: 99
99
   datatype z
extended

If y has floating-point precision, the result is a rational, of which both the numerator and denominator are extended integers.

   x: 0.33333333333
33333330575r99999991726
   x: 0.333333333333
1r3

x:^:_1 y, the inverse of x: y, converts an extended precision number to a fixed precision (Boolean, integer, or floating-point as needed)

   3!:0 x:^:_1 ] 1r2 + 1r2   NB. 1 becomes Boolean
1
   3!:0 x:^:_1 ] 1r2 + 2r2   NB. 1/2 becomes floating-point
8
   3!:0 x:^:_1 ] 1r2 + 3r2   NB. 2 becomes integer
4

Common uses

1. Avoid rounding errors in arithmetic with large numbers

   ] z=: 2^64
1.84467e19
   z -: 1+z          NB. rounding error loses the increment
1

   ] zxco=: (x:2)^64
18446744073709551616
   1+zxco            NB. no rounding error to lose the increment
18446744073709551617

   ] zx=: 2x ^ 64    NB. x-notation produces an identical number
18446744073709551616
   1+zx
18446744073709551617

   datatype each z ; zx ; zxco
+--------+--------+--------+
|floating|extended|extended|
+--------+--------+--------+


More information

1. Conversion to rational precision uses tolerant comparison.

Use Fit (x:!.0) for exact comparison

   x: ]5 ^~ 1%3
1r243
   x:!.0 ]5 ^~ 1%3
37066663599757r9007199254740992

2. Explore the numerology of pi

   dpi=: [: ": [: <.@o. 10 ^ [: x: <:   NB. Digits of pi (as a string)

   ] z=: dpi 770
3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648...

   $$ pi=: ". 'x',~z   NB. Digits of pi as a large number
0
   >. 10 ^. pi         NB. (Next highest power of 10) > pi
770
   _50]\z              NB. z in rows of 50 atoms/column
31415926535897932384626433832795028841971693993751
05820974944592307816406286208998628034825342117067
98214808651328230664709384460955058223172535940812
84811174502841027019385211055596446229489549303819
64428810975665933446128475648233786783165271201909
14564856692346034861045432664821339360726024914127
37245870066063155881748815209209628292540917153643
67892590360011330530548820466521384146951941511609
43305727036575959195309218611738193261179310511854
80744623799627495673518857527248912279381830119491
29833673362440656643086021394946395224737190702179
86094370277053921717629317675238467481846766940513
20005681271452635608277857713427577896091736371787
21468440901224953430146549585371050792279689258923
54201995611212902196086403441815981362977477130996
05187072113499999983

NOTE: The six 9s near the end, starting at 762{z are not a bug, but an expected random occurrence. 762{z is known as the Feynman Point. See Chapter 17 of At Play With J by Eugene E. McDonnell.


x x: y Num/Denom

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



A collection of functions for extended precision conversion, the function being chosen by the value of argument x

x What it does Precision of y Result x x: y
1 Convert to extended precision any same as x: y: extended integer or rational, as needed
_1 Convert to fixed precision any same as x:^:_1 y, the inverse of x: y: Boolean, integer, or floating point, as needed
2 Convert to numerator,denominator form any 2-atom list of (numerator;denominator), in extended precision
_2 Convert from numerator,denominator form fixed Same as %/"1 y. Result has floating precision
extended Same as %/"1 y. Result has extended precision

Common uses

1. Convert extended or rational to floating

   _1 x: 1r3
0.333333

2. Convert rational to 2-list: (numerator;denominator)

   2 x: 1r3
1 3

3. Convert a number-pair (2-list) y of the form: (a,b) to the number  a%b

   _2 x: 1 3
0.333333

Convert a table of number-pairs y likewise

   ] y=: 3 2$ 1 3 1 5 1 10
1  3
1  5
1 10
   _2 x: y
0.333333 0.2 0.1

More Information

1. Conversion to rational precision uses tolerant comparison.

Use Fit (x:!.0) for exact comparison

  2  x: ]5 ^~ 1%3
1 243
  2  x:!.0 ]5 ^~ 1%3
37066663599757 9007199254740992