Ok, I think I've fixed this bug in string>forth. > bl parse 3.14159265366 string>float fs. 3.1415925 ok > bl parse 314159265366 string>float fs. 3.1415915E11 ok
The new code is below (and available at https://github.com/lnmaurer/amforth-float as usual). -Leon : string>float ( c-addr u-length -- f ) \ get exponent first -- this is the number that follows e, E, d, or D 101 extract not if drop \ 'e' 69 extract not if drop \ 'E' 100 extract not if drop \ 'd' 68 extract not if drop \ 'D' 0 \ if you can't find anything, then it's zero then then then then >r ( adr length, R: exp ) over c@ 45 = if \ starts with negative sign r> true >r >r ( adr length, R: bool-isneg exp) 1- swap 1+ swap ( adr+1 length-1, R: bool-isneg exp) else r> false >r >r then ( adr length, R: bool-isneg exp) over c@ 43 = if \ if it's a plus sign, just ignore it, but reduce string size 1- swap 1+ swap ( adr+1 length-1, R: bool-isneg exp) then r> rot rot ( exp adr length, R: bool-isneg ) [ 0. ] fliteral fnswap ( exp adr d-0 length, R: bool-isneg ) >r false nfswap r> ( exp adr bool-after_decimal d-0 length, R: bool-isneg ) 0 do ( exp adr bool-after_decimal d-0 ) \ get next character fover drop i + c@ ( exp adr bool-after_decimal d-sum char ) dup 46 = if \ it's a '.' drop \ get rid of character \ we're after the decimal now, so make bool-after_decimal true fnswap drop true nfswap ( exp adr bool-after_decimal d-sum ) else nfover [ 214748364. ] fliteral d> if \ d-sum can't hold any more drop \ don't care about character fnover not if \ we're before the decimal place, so add one to the exponent >r >r >r >r 1+ r> r> r> r> else leave \ there's nothing left to do if we're after the decimal place then else 48 - ( exp adr bool-after_decimal d-sum possible-digit ) dup 0 < ( exp adr bool-after_decimal d-sum pd bool ) over 9 > ( exp adr bool-after_decimal d-sum pd bool bool ) or if abort then \ it's not a digit, abort ( exp adr bool-after_decimal d-sum digit ) >r d10* r> s>d d+ ( exp adr bool-after_decimal d-sum ) fnover if ( exp adr bool-after_decimal d-sum ) >r >r >r >r 1- r> r> r> r> \ decriment the exponent by one then then then loop ( exp adr bool-after_decimal d-sum, R: bool-isneg ) >r >r drop drop r> r> ( exp d-sum, R: bool-isneg ) d>f fnswap ( f-sum exp, R: bool-isneg ) ?dup if \ if the exponent isn't zero, adjust the float to match the exp [ 1 s>f ] fliteral ( f-sum exp f-1) fnover abs 0 do [ 10 s>f ] fliteral f* loop ( f-sum exp 10^abs[exp], R: bool-isneg ) fnswap 0< if f/ else f* then then ( f-num, R: bool-isneg ) \ take care of negative sign r> if fnegate then ; On Thursday 26 May 2011 11:57:01 pito wrote: > Hi, this is when we enter a little bit more decimal places - > probably a bug in >float or fsn.: > > \ floating point input with >float, "& 1.23456e12" > : # bl word count >float not abort" NaN" > state @ if postpone fliteral then ; immediate > > \ print 6 decimal places > : fp. 6 fsn. ; > > > > # 3.141592 fp. > 3.141592 ok > > # 3.1415926 fp. > 3.141592 ok > > # 3.14159265 fp. > 3.141592 ok > > # 3.141592653 fp. > 3.141592E-1 ok > > # 3.14159265366 fp. > 3.141592E-3 ok > > > > > ------------------------------------------------------------------------------ > vRanger cuts backup time in half-while increasing security. > With the market-leading solution for virtual backup and recovery, > you get blazing-fast, flexible, and affordable data protection. > Download your free trial now. > http://p.sf.net/sfu/quest-d2dcopy1 > _______________________________________________ > Amforth-devel mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/amforth-devel ------------------------------------------------------------------------------ vRanger cuts backup time in half-while increasing security. With the market-leading solution for virtual backup and recovery, you get blazing-fast, flexible, and affordable data protection. Download your free trial now. http://p.sf.net/sfu/quest-d2dcopy1 _______________________________________________ Amforth-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/amforth-devel
