I'm doing trigonometry with very small angles and I want to keep all my
calculations in rational precision. Is there a J-supported way of
converting from floating-point precision to rational, or reasonably speedy
verbs to do the job routinely?

My problem is this. Let PIa be π expressed as a rational number to 50
places of decimals (…or more!!)

   PIa
31415926535897932384626433832795028841971693993751r10000000000000000000000000000000000000000000000000
   datatype PIa
rational
   datatype y=: 1.23
floating
   datatype PIa + y   NB. loses precision...
floating
   datatype sin PIa   NB. likewise loses precision...
floating

In other words, adding in (or otherwise combining) a number (y) which is
defined *exactly* as a decimal numeral (the sort of thing the SI system of
units does often) results in an avoidable loss of precision.

(In case anyone's thinking at this point: aren't 64 bits good enough for
this guy? -- no, they aren't.)

At present I'm using a mickey-mouse scheme of converting the decimal
numeral (":1.23) to a rational value by omitting the decimal point to get
'123', then reintroducing it as a denominator: '123r100' -- which I then
evaluate using (".) to give, in effect:
   datatype ya=: 123r100
rational
   datatype PIa + ya   NB. --now it behaves itself...
rational

And of course I'm going to have to write my own sin and cosine verbs.

The general purpose engine I'm writing not only needs a way of converting
an inputted numeral '1.23' to a rational number (a trivial task by the
above method) but also to check my results accumulator at every step to
stop it lapsing into floating-point precision, and maybe to convert it back
into rational precision.

This last task is inefficient, the way I'm doing it. Does J have a built-in
way, or a standard way, that's faster than how I'm doing it?

Ian Clark
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to