Here is yet another update

This version supports arbitrary base constants (e.g. 12b_345.678),
and even supports semi-degenerate cases (e.g. 12b_.456).
However, I am not supporting 12b_. nor 12b_123. which the
current implementation does support.

My parsing process consists of breaking the string of characters
into pieces based on the significant characters and counting
how many pieces I have, which means I support one
interpretation for each number of tokens at that level of hierarchy.
I could add tests to treat the other cases but from my point of
view those other cases are not useful.  But I am willing to be
convinced otherwise...

Anyways, here is the code as it currently stands.  (And I am reposting
the whole thing because I hate having to search around through
several messages to try and piece together a working program.)

NB. core numeric implementation
alfnum=: '0123456789abcdefghijklmnopqrstuvwxyz'
enquote=: '''',,&''''
refdig=: alfnum enquote@:{.~ >./@:>:@i.~&alfnum
digits=: refdig,'i.',enquote
isb10=: ass...@e.&'0123456789'
isb36=: ass...@e.&alfnum
decimal=:  (isb10 ] '10#.',digits )@;@{.
extend=:   (isb10 ] '10x#.',digits)@;@{.
arbitrary=:(isb36 ] digits        )@;

NB. parsing
split=:1 :0
 (0;(0 10#:<.10*".;._2]0 :0);< <;._1 m)&;:@;
)

NO=: assert bind 0

sel=:2 :0
 'ndx opts'=.y
 x`''@.((;:opts) i. ndx&{)
)

NB. fractions
tkn1=: ' .r' split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb1=: no`decimal`decimal`num...@.#@tkn1
numb1e=: numb1d`numb1r sel (1;'.r')
numb1d=: '(', decimal,')+(%10)#.|.', (isb10 ] digits)@,~&'0'@>@{:
numb1r=: '(', extend, ')%',ext...@{:

NB. negative sign
tkn2=: ' _' split
 1.1 2.1
 2.2 2.2
 2   2
)
numb2=: no`inf`num...@.#@tkn2
numb2e=: '_.'"_`('-_'"_)`('-',nu...@}.) sel (1;'._')
inf=: numb1`('_'"_)@.('_'={.@;)

NB. exponential (scientific notation)
tkn3=: ' e'split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb3=: no`numb2`no`num...@.#@tkn3
numb3e=: ('(',nu...@{.,')*10^',nu...@{:) sel (1;'e')

NB.  Complex
tkn4=: ' a dr j'split
 0.6 0.6 0.6 1.1
 2.2 2.2 3.2 1
 3   3   4.2 4.2
 4.2 4.2 4.2 4.2
 4   4   4   4
)
numb4=: no`numb3`no`num...@.#@tkn4
numb4e=: numb4j`numb4ad`numb4ar sel (1;'j ad ar')
numb4j=: '(',nu...@{.,')j.',nu...@{:
numb4ar=: 'j./(',nu...@{.,')*1 2 o.',nu...@{:
numb4ad=: 'j./(',nu...@{.,')*1 2 o.180%~o.',nu...@{:

NB. other exponentials and extended precision
tkn5=: ' px'split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb5=: no`numb4`extend`num...@.#@tkn5
numb5e=: numb5p`numb5x sel (1;'p x')
numb5p=: '(',nu...@{.,')*(o.1)^',nu...@{:
numb5x=: '(',nu...@{.,')*^',nu...@{:

NB. arbitrary bases
tkn6=: ' b _'split
 0.6 1.1 1.1
 2.2 1   1
 4.2 3.2 4.2
 4.2 4.2 4.2
 4   4   4
)
numb6=: NO`numb5`NO`numb6e`('-',numb6e)@....@tkn6
numb6e=: ('(',nu...@{.,')#.',signed) sel (1;'b')
numb6e=: (fraction,nu...@{.) sel (1;'b')

NB. fraction support for arbitrary bases
tkn6f=: ' .' split
 2.1 1.1
 2.2 1
 3.2 3.2
 3   3
)
fraction=:no`arbint`arbfrac1`arbf...@.#@tk...@{:
arbint=: '((',')#.~])',~arbitr...@{.
arbfrac1=: '((|.',arbitrary@,~&'0'@>@{:,')#.~%)'"_
arbfrac=: '((((',arbitr...@{.,')#.~]))+(|.',arbitrary@,~&'0'@>@{:,')#.~%)'"_

NB. vectors
tkn7=: '/ 'split
 0   1.1
 0.2 1
)
numb7=: [:;('(',[,'),',])&.>/@:(numb6&.>)@tkn7

require'strings'
pc_rdm2=: nu...@tolower

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

Reply via email to