Here are two ways:
every
&>
each
&.>
d1 =: 2&{.every @: -.&'.'&.":
d2 =: (10 #.^:_1 <.`(*&10)@.(~: <.)^:_) ::]
NB. In the following, I'm only using ,. in order
NB. to display the results vertically.
,.d1 each 5 2 4 0 1 %3 3 3 3 0
+-------------+
|1 6 6 6 6 7 |
+-------------+
|0 6 6 6 6 6 7|
+-------------+
|1 3 3 3 3 3 |
+-------------+
|0 |
+-------------+
|_ |
+-------------+
,.d2 each 5 2 4 0 1 %3 3 3 3 0
+---------------------------+
|1 6 6 6 6 6 6 6 6 6 6 6 6 7|
+---------------------------+
|6 6 6 6 6 6 6 6 6 6 6 6 7 |
+---------------------------+
|1 3 3 3 3 3 3 3 3 3 3 3 3 3|
+---------------------------+
|0 |
+---------------------------+
|_ |
+---------------------------+
d1 removes any decimal point and pads with spaces under
monadic format. Its representation depends on print
precision:
9!:10 ''
6
9!:11 ] 10
,.d1 each 5 2 4 0 1 %3 3 3 3 0
+---------------------+
|1 6 6 6 6 6 6 6 6 7 |
+---------------------+
|0 6 6 6 6 6 6 6 6 6 7|
+---------------------+
|1 3 3 3 3 3 3 3 3 3 |
+---------------------+
|0 |
+---------------------+
|_ |
+---------------------+
,.d1 each 5 2 4 0 1 %3 3 3 3 0
+-----------------------------------+
|1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 |
+-----------------------------------+
|0 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 3|
+-----------------------------------+
|1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 |
+-----------------------------------+
|0 |
+-----------------------------------+
|_ |
+-----------------------------------+
d2 is more verbose than d1, but works only in
the numeric domain. It multiplies by ten until
the value is "integer", and then uses 10#.^:_1
to separate the digits. (It's a bit like Bo
Jacoby's suggestion, just seen.)
The representation depends on comparison tolerance:
9!:18''
5.684341886e_14
9!:19 ]1e_16
,.d2 each 5 2 4 0 1 %3 3 3 3 0
+---------------------------------+
|1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 2|
+---------------------------------+
|6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 5 |
+---------------------------------+
|1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 |
+---------------------------------+
|0 |
+---------------------------------+
|_ |
+---------------------------------+
If you know you're dealing with a number with a long
or infinite decimal part you might choose to drop the
last digit or two.
Note that d1 fails if the number is a special type,
eg rational. Results are strange for a very small or
very large number where the monadic format would
include the tens exponent. You can get round this
with
d1e =: 2&{.&>@:(-.&'.')@:({.~ i.&'e')&.":
,.d1e each 1234e_21 1234e21
+---------------------------------+
|1 2 3 3 9 9 9 9 9 9 9 9 9 9 9 9 9|
+---------------------------------+
|1 2 3 4 |
+---------------------------------+
d2 will loop on a rational such as 5r3 but is ok-ish
for small or large numbers:
,.d2 each 1234e_21 1234e21
+-------------------------------------------------+
|1 2 3 3 9 9 9 9 9 9 9 9 9 9 9 9 8 |
+-------------------------------------------------+
|1 2 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0|
+-------------------------------------------------+
In view of the above examples, I think I'd prefer d1
for Goldilocks numbers, neither too small nor too large,
otherwise d1e since d2 suffers from rounding problems.
I haven't tested these for time & space requirements.
Mike
On 11/07/2011 10:26 AM, David Vaughan wrote:
> How could one turn a a decimal into a list where each cell is one digit? E.g.
> 1.66667 becomes 1 6 6 6 6 7 or something along those lines.
> ___________________________
>
> David Vaughan
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm