1 ! (c)2009 Joe Groff bsd license
2 USING: accessors byte-arrays combinators kernel kernel.private
3 math namespaces sequences sequences.private splitting strings
9 { [ dup CHAR: 9 <= ] [ CHAR: 0 - dup 0 < [ drop 255 ] when ] }
10 { [ dup CHAR: a < ] [ CHAR: A 10 - - dup 10 < [ drop 255 ] when ] }
11 [ CHAR: a 10 - - dup 10 < [ drop 255 ] when ]
18 { length fixnum read-only }
19 { radix fixnum read-only } ;
21 : <number-parse> ( str radix -- i number-parse n )
27 : (next-digit) ( i number-parse n digit-quot end-quot -- n/f )
28 [ 2over length>> < ] 2dip
29 [ [ 2over str>> nth-unsafe >fixnum [ 1 + >fixnum ] 3dip ] prepose ] dip if ; inline
31 : require-next-digit ( i number-parse n quot -- n/f )
32 [ 3drop f ] (next-digit) ; inline
34 : next-digit ( i number-parse n quot -- n/f )
35 [ 2nip ] (next-digit) ; inline
37 : add-digit ( i number-parse n digit quot -- n/f )
38 [ [ dup radix>> ] [ * ] [ + ] tri* ] dip next-digit ; inline
40 : digit-in-radix ( number-parse n char -- number-parse n digit ? )
41 digit> pick radix>> over > ; inline
43 : ?make-ratio ( num denom/f -- ratio/f )
44 [ / ] [ drop f ] if* ; inline
49 { exponent read-only } ;
51 : inc-point ( float-parse -- float-parse' )
52 [ radix>> ] [ point>> 1 + ] [ exponent>> ] tri float-parse boa ; inline
54 : store-exponent ( float-parse n expt -- float-parse' n )
55 swap [ [ drop radix>> ] [ drop point>> ] [ nip ] 2tri float-parse boa ] dip ; inline
57 : ?store-exponent ( float-parse n expt/f -- float-parse' n/f )
58 [ store-exponent ] [ drop f ] if* ; inline
60 : ((pow)) ( base x -- base^x )
61 iota 1 rot [ nip * ] curry reduce ; inline
63 : (pow) ( base x -- base^x )
64 dup 0 >= [ ((pow)) ] [ [ recip ] [ neg ] bi* ((pow)) ] if ; inline
66 : add-mantissa-digit ( float-parse i number-parse n digit quot -- float-parse' n/f )
67 [ [ inc-point ] 4dip ] dip add-digit ; inline
69 : make-float-dec-exponent ( float-parse n/f -- float/f )
70 [ [ radix>> ] [ point>> ] [ exponent>> ] tri - (pow) ] [ swap /f ] bi* ; inline
72 : make-float-bin-exponent ( float-parse n/f -- float/f )
73 [ drop [ radix>> ] [ point>> ] bi (pow) ]
75 [ drop 2.0 swap exponent>> (pow) * ] 2tri ; inline
77 : ?default-exponent ( float-parse n/f -- float-parse' n/f' )
80 [ [ [ radix>> ] [ point>> ] bi 0 float-parse boa ] dip ]
84 : ?make-float ( float-parse n/f -- float/f )
85 { float-parse object } declare
88 { [ dup not ] [ 2drop f ] }
89 { [ over radix>> 10 = ] [ make-float-dec-exponent ] }
90 [ make-float-bin-exponent ]
93 : ?neg ( n/f -- -n/f )
94 [ neg ] [ f ] if* ; inline
96 : ?add-ratio ( m n/f -- m+n/f )
97 dup ratio? [ + ] [ 2drop f ] if ; inline
99 : @abort ( i number-parse n x -- f )
100 2drop 2drop f ; inline
102 : @split ( i number-parse n -- n i number-parse n' )
105 : @split-exponent ( i number-parse n -- n i number-parse' n' )
106 -rot [ str>> ] [ length>> ] bi 10 number-parse boa 0 ; inline
108 : <float-parse> ( i number-parse n -- float-parse i number-parse n )
109 [ drop nip radix>> 0 f float-parse boa ] 3keep ; inline
111 DEFER: @exponent-digit
112 DEFER: @mantissa-digit
118 : @exponent-digit-or-punc ( float-parse i number-parse n char -- float-parse n/f )
120 { CHAR: , [ [ @exponent-digit ] require-next-digit ] }
124 : @exponent-digit ( float-parse i number-parse n char -- float-parse n/f )
125 { float-parse fixnum number-parse integer fixnum } declare
126 digit-in-radix [ [ @exponent-digit-or-punc ] add-digit ] [ @abort ] if ;
128 : @exponent-first-char ( float-parse i number-parse n char -- float-parse n/f )
130 { CHAR: - [ [ @exponent-digit ] require-next-digit ?neg ] }
131 { CHAR: + [ [ @exponent-digit ] require-next-digit ] }
135 : ->exponent ( float-parse i number-parse n -- float-parse' n/f )
136 @split-exponent [ @exponent-first-char ] require-next-digit ?store-exponent ; inline
138 : exponent-char? ( number-parse n char -- number-parse n char ? )
139 3dup nip swap radix>> {
140 { 10 [ [ CHAR: e CHAR: E ] dip [ = ] curry either? ] }
141 [ drop [ CHAR: p CHAR: P ] dip [ = ] curry either? ]
144 : or-exponent ( i number-parse n char quot -- n/f )
145 [ exponent-char? [ drop <float-parse> ->exponent ?make-float ] ] dip if ; inline
147 : or-mantissa->exponent ( float-parse i number-parse n char quot -- float-parse n/f )
148 [ exponent-char? [ drop ->exponent ] ] dip if ; inline
150 : @mantissa-digit-or-punc ( float-parse i number-parse n char -- float-parse n/f )
152 { CHAR: , [ [ @mantissa-digit ] require-next-digit ] }
156 : @mantissa-digit ( float-parse i number-parse n char -- float-parse n/f )
157 { float-parse fixnum number-parse integer fixnum } declare
160 [ [ @mantissa-digit-or-punc ] add-mantissa-digit ]
162 ] or-mantissa->exponent ;
164 : ->mantissa ( i number-parse n -- n/f )
165 <float-parse> [ @mantissa-digit ] next-digit ?make-float ; inline
167 : ->required-mantissa ( i number-parse n -- n/f )
168 <float-parse> [ @mantissa-digit ] require-next-digit ?make-float ; inline
170 : @denom-digit-or-punc ( i number-parse n char -- n/f )
172 { CHAR: , [ [ @denom-digit ] require-next-digit ] }
173 { CHAR: . [ ->mantissa ] }
174 [ [ @denom-digit ] or-exponent ]
177 : @denom-digit ( i number-parse n char -- n/f )
178 { fixnum number-parse integer fixnum } declare
179 digit-in-radix [ [ @denom-digit-or-punc ] add-digit ] [ @abort ] if ;
181 : @denom-first-digit ( i number-parse n char -- n/f )
183 { CHAR: . [ ->mantissa ] }
187 : ->denominator ( i number-parse n -- n/f )
188 { fixnum number-parse integer } declare
189 @split [ @denom-first-digit ] require-next-digit ?make-ratio ;
191 : @num-digit-or-punc ( i number-parse n char -- n/f )
193 { CHAR: , [ [ @num-digit ] require-next-digit ] }
194 { CHAR: / [ ->denominator ] }
198 : @num-digit ( i number-parse n char -- n/f )
199 { fixnum number-parse integer fixnum } declare
200 digit-in-radix [ [ @num-digit-or-punc ] add-digit ] [ @abort ] if ;
202 : ->numerator ( i number-parse n -- n/f )
203 { fixnum number-parse integer } declare
204 @split [ @num-digit ] require-next-digit ?add-ratio ;
206 : @pos-digit-or-punc ( i number-parse n char -- n/f )
208 { CHAR: , [ [ @pos-digit ] require-next-digit ] }
209 { CHAR: + [ ->numerator ] }
210 { CHAR: / [ ->denominator ] }
211 { CHAR: . [ ->mantissa ] }
212 [ [ @pos-digit ] or-exponent ]
215 : @pos-digit ( i number-parse n char -- n/f )
216 { fixnum number-parse integer fixnum } declare
217 digit-in-radix [ [ @pos-digit-or-punc ] add-digit ] [ @abort ] if ;
219 : (->radix) ( number-parse radix -- number-parse' )
220 [ [ str>> ] [ length>> ] bi ] dip number-parse boa ; inline
222 : ->radix ( i number-parse n quot radix -- i number-parse n quot )
223 [ (->radix) ] curry 2dip ; inline
225 : with-radix-char ( i number-parse n radix-quot nonradix-quot -- n/f )
228 { CHAR: b [ drop 2 ->radix require-next-digit ] }
229 { CHAR: o [ drop 8 ->radix require-next-digit ] }
230 { CHAR: x [ drop 16 ->radix require-next-digit ] }
231 { f [ 3drop 2drop 0 ] }
232 [ [ drop ] 2dip swap call ]
234 ] 2curry next-digit ; inline
236 : @pos-first-digit ( i number-parse n char -- n/f )
238 { CHAR: . [ ->required-mantissa ] }
239 { CHAR: 0 [ [ @pos-digit ] [ @pos-digit-or-punc ] with-radix-char ] }
243 : @neg-digit-or-punc ( i number-parse n char -- n/f )
245 { CHAR: , [ [ @neg-digit ] require-next-digit ] }
246 { CHAR: - [ ->numerator ] }
247 { CHAR: / [ ->denominator ] }
248 { CHAR: . [ ->mantissa ] }
249 [ [ @neg-digit ] or-exponent ]
252 : @neg-digit ( i number-parse n char -- n/f )
253 { fixnum number-parse integer fixnum } declare
254 digit-in-radix [ [ @neg-digit-or-punc ] add-digit ] [ @abort ] if ;
256 : @neg-first-digit ( i number-parse n char -- n/f )
258 { CHAR: . [ ->required-mantissa ] }
259 { CHAR: 0 [ [ @neg-digit ] [ @neg-digit-or-punc ] with-radix-char ] }
263 : @first-char ( i number-parse n char -- n/f )
265 { CHAR: - [ [ @neg-first-digit ] require-next-digit ?neg ] }
266 { CHAR: + [ [ @pos-first-digit ] require-next-digit ] }
272 : base> ( str radix -- n/f )
273 <number-parse> [ @first-char ] require-next-digit ;
275 : string>number ( str -- n/f ) 10 base> ; inline
277 : bin> ( str -- n/f ) 2 base> ; inline
278 : oct> ( str -- n/f ) 8 base> ; inline
279 : dec> ( str -- n/f ) 10 base> ; inline
280 : hex> ( str -- n/f ) 16 base> ; inline
282 : string>digits ( str -- digits )
283 [ digit> ] B{ } map-as ; inline
287 : (digits>integer) ( valid? accum digit radix -- valid? accum )
288 2dup < [ swapd * + ] [ 2drop 2drop f 0 ] if ; inline
290 : each-digit ( seq radix quot -- n/f )
291 [ t 0 ] 3dip curry each swap [ drop f ] unless ; inline
295 : digits>integer ( seq radix -- n/f )
296 [ (digits>integer) ] each-digit ; inline
299 dup 10 < [ CHAR: 0 + ] [ 10 - CHAR: a + ] if ; inline
303 : positive>base ( num radix -- str )
304 dup 1 <= [ "Invalid radix" throw ] when
305 [ dup 0 > ] swap [ /mod >digit ] curry "" produce-as nip
310 GENERIC# >base 1 ( n radix -- str )
312 : number>string ( n -- str ) 10 >base ; inline
313 : >bin ( n -- str ) 2 >base ; inline
314 : >oct ( n -- str ) 8 >base ; inline
315 : >hex ( n -- str ) 16 >base ; inline
322 : sign ( -- str ) negative? get "-" "+" ? ;
324 : with-radix ( radix quot -- )
325 radix swap with-variable ; inline
327 : (>base) ( n -- str ) radix get positive>base ;
338 [ neg ] dip positive>base CHAR: - prefix
344 dup 0 < negative? set
346 [ [ "" ] [ (>base) sign append ] if-zero ]
348 [ numerator (>base) ]
349 [ denominator (>base) ] bi
352 negative? get [ CHAR: - prefix ] when
355 : fix-float ( str -- newstr )
358 [ CHAR: e over member? ]
359 [ "e" split1 [ fix-float "e" ] dip 3append ]
361 [ CHAR: . over member? ]
369 : mantissa-expt-normalize ( mantissa expt -- mantissa' expt' )
371 [ over log2 52 swap - [ shift 52 2^ 1 - bitand ] [ 1022 + - ] bi-curry bi* ]
374 : mantissa-expt ( float -- mantissa expt )
376 [ -0.0 double>bits bitnot bitand -52 shift ] bi
377 mantissa-expt-normalize ;
379 : float>hex-sign ( bits -- str )
380 -0.0 double>bits bitand zero? "" "-" ? ;
382 : float>hex-value ( mantissa -- str )
383 >hex 13 CHAR: 0 pad-head [ CHAR: 0 = ] trim-tail
384 [ "0" ] when-empty "1." prepend ;
386 : float>hex-expt ( mantissa -- str )
387 10 >base "p" prepend ;
389 : float>hex ( n -- str )
392 mantissa-expt [ float>hex-value ] [ float>hex-expt ] bi*
395 : format-float ( n format -- string )
396 0 suffix >byte-array (format-float)
397 dup [ 0 = ] find drop head >string
400 : float>base ( n base -- str )
403 [ drop "%.16g" format-float ]
408 : float>string ( n -- str )
409 10 float>base ; inline
413 { [ over fp-nan? ] [ 2drop "0/0." ] }
414 { [ over 1/0. = ] [ 2drop "1/0." ] }
415 { [ over -1/0. = ] [ 2drop "-1/0." ] }
416 { [ over 0.0 fp-bitwise= ] [ 2drop "0.0" ] }
417 { [ over -0.0 fp-bitwise= ] [ 2drop "-0.0" ] }
421 : # ( n -- ) number>string % ; inline