Robert Bernecky <[EMAIL PROTECTED]> wrote:
> ps: I've never understood people who prefer fast answers to
> correct ones.

I take Robert's point about printing decimal numbers, however the
tradeoff of speed vs accuracy is common in numerical analysis, where
mathematical correctness is impossible.

Here is the bisection method used to find the square root of 2, with
initial bracketing interval [1,2].

bisect=:1 : 0
'a b'=.y
m=.-:a+b
if. 0 < (u m)*u a do. m,b else. a,m end.
)

error=:-:@|@(-/)"1

f=:_2+*:

   error f bisect^:(i.5) 1 2
0.5 0.25 0.125 0.0625 0.03125

You can never get the "correct" answer, but you can make the error
arbitrary small at the cost of more iterations.

Best wishes,

John



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

Reply via email to