My first reaction to Bo's remarks (far below) was "well said and very sensible" although my instinct was to use 1e8 near half the precision of floats. I started playing with tacit versions and got to observe what I thought was a surprising number of false positives.
First some tacit fun:

   rat1=:13 : '1e9>1{"1]2 x: y'
   rat2=:1e9 > 1 {"1 (2&x:)
   rat3=:1e9 > 1 {"1 x:~&2
   rat1
1000000000 > 1 {"1 [: ] 2 x: ]
   rat2
1000000000 > 1 ({"1) 2&x:
   rat3
1000000000 > 1 {"1 x:~&2
   ts=:6!:2 , 7!:2@]
   100 ts 'rat1 1r3^~i.10000'
0.118844 1.00918e7
   100 ts 'rat2 1r3^~i.10000'
0.117574 1.02228e7
   100 ts 'rat3 1r3^~i.10000'
0.117814 1.02228e7
All very close in performance. I think rat2 is the most readable but rat3 is a character shorter. Now some experiments where I had some expectations.

   +/rat3 1r3^~i.10000
109

I expected around

   3%:10000-1
21.5436

poke around for a small false positive
   i#~rat3 1r3^~i=:i.1000
0 1 8 27 64 89 125 216 343 441 512 607 729
   89^1r3
4.46475
   x: 89^1r3
1344576733r301154199
   rat3 89^1r3
1

How about using 1e5 instead of 1e9:
   rat4=:1e5 > 1 {"1 x:~&2
   +/rat4 1r3^~i.10000
26
   +/rat4 1r2^~i.10000
100
   +/rat4 o.i.1000
4

And a couple more experiments
   +/rat3 o.i.1000
289
   +/rat3 0.01*1r3^~i.10000
28

So, as previously noted by others, beware. But if you are going to use a test for rational, you might do better with something less than 1e9.

On 6/14/2018 7:00 PM, 'Bo Jacoby' via Programming wrote:
I completely agree with Raul. We all agree that the problem of identifying 
irrational floating point numbers is unsolvable, simply because any floating 
point number is rational. Critics need not show that a proposed algorithm is 
imperfect, but rather suggest improvements.
    rational=.3 : '1e9>1{"1]2 x: y'
    rational 10^_8 _9 NB. useless result
1 0
    rational (%3)^~i.9 NB. useful result
1 1 0 0 0 0 0 0 1

/Bo..

     Den 16:37 torsdag den 14. juni 2018 skrev Raul Miller 
<[email protected]>:
  Or, put different, it's a heuristic:

It's likely to be right significantly more often than it's wrong for a
variety of likely contexts.

But it's going to be wrong some of the time, also, and it's useful to
understand why that would be.

Thanks,


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

Reply via email to