If you're expecting 'round' to round to the nearest integer, the
numeric script (http://www.jsoftware.com/svn/base/trunk/main/main/numeric.ijs)
might be handy:

   load'numeric'
   roundint 2.9
3
   roundint 2.5
3
   roundint 2.4
2

with that in mind, here's a possible explicit function to do the job,

   f1 =: 4 : 0
roundint x * %:x * y
)

Parentheses are unnecessary here; execution proceeds from right to
left: (i) x*y (ii) take its square root with %: (iii) multiply that
result by x (iv) round the result to the nearest integer.

We can immediately inspect the tacit version of this verb,

   13 : 'roundint x * %:x * y'
[: roundint ([ * ([: %: *))

and simplify slightly (I find this more readable),

   f2 =. roundint@([ * %:@*)

   a=:4
   b=:5
   a f1 b
18
   a f2 b
18

This verb uses a fork, ([ * %:@*), and composition. First of all, compare,

   %: 4 * 5
4.47214

   4 %:@* 5
4.47214

and with the train,

   4 ([ , %:@*) 5
4 4.47214

hence,

   4 ([ * %:@*) 5
17.8885

which is the product of 4 and 4.47214.

Finally: composing that verb train with roundint rounds the result,

   4 roundint@([ * %:@*) 5
18

Hope that helps;
Best,
M
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to