J lacks a long precision decimal type, probably because Ken realized that
base 10 is a silliness tied to our biology.  Instead, j supports extended
rationals, which let us access many more numbers than I triple E 754
floating point.  There are many problems which require computations to so
many decimal digits.  This is somewhat reasonable as measurement
uncertainties are often expressed in base 10.  J indirectly supports long
decimal in that the complex format of a rational number can be any
(reasonable) length.  We compute series to different term tally to find the
number of literal digits remaining the same.  Note that j does not support
a rational result from rational to the power of funky rational.

  2 2r3 ^&.> 2r3 2
+------+---+
|1.5874|4r9|
+------+---+

   x:inv".'x',~500#'1'   NB. x:inv  1111111...111x
_

   3j1 ": 22 + 1r10
***

Thoughts on long decimal arithmetic.  1) The Digits adverb,  u Digits y
 computes u to y significant decimal digits, returning that number.
Meanwhile the verb  u y  calls  u  with increasing integral y, from 1,
typically the number of terms.  u >:y should be more accurate than u y .  u
can be memoized and recursive since it's forced to recompute successively
deeper.  Taylor series is easily recurrent.  The "50" in Digits is
arbitrary and might be replaced with some function of logarithm.


Digits=: adverb define NB. u Digits yu    u yu  is less accurate than  u
yu+1
 NB. returns u to at least y significant digits
 format=. ' _.' -.~ ((j.~ 50&+) y)&":
 i =. 1
 current=. format u i
 whilst. last -.@-: current do.
  last =. current
  i =. i + 2  NB. increment by 2 for no good reason.
  current=. format result =. u i
 end.
 result
)

And 2) Following some arithmetic on rationals can result in long numerators
and denominators.  The cf dyad computes a perhaps wieldly fraction within x
tolerance of y.

cf=: 0.1&$: :(4 :0)  NB. tolerance cf value -> continued fraction
approximation of value to tolerance
 Y =. y
 X =. 0 >. x
 terms =. 0 $ 0x
 whilst. X < | approximation - y do.
  'term Y' =. <.`([:%1&|)`:0 Y
  terms =. terms , term
  approximation =. +`%/ }: 1j1 #!.1 terms
 end.
)

assert (-: 0&cf) 649r200
assert 13r4 (-: cf) 649r200


references:
 http://rosettacode.org/wiki/Ramanujan%27s_constant#J
 jpath'~addons/math/misc/contfrac.ins'
 labs of which I am unaware.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to