Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #135293
| From | Krishna Myneni <krishna.myneni@ccreweb.org> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: prelim test code for Forth big number code (FSL #47) |
| Date | 2026-08-02 21:33 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <114oump$vdib$1@dont-email.me> (permalink) |
| References | <1145sgl$2mg4t$1@dont-email.me> |
On 7/26/26 16:00, Krishna Myneni wrote:
> I ported Len Zettel's big number code, big.4th (Forth Scientific Library
> algorithm #47) to kForth, and added some preliminary test code to it.
> The existing version on the FSL website (May 1999 revision) does not
> include any test code. The test code passes on kForth-64/32/Win-32 and
> is listed below.
>
> A glossary of words has also been added (see below).
>
> See
> https://github.com/mynenik/kForth-64/blob/master/forth-src/fsl/big.4th
>
> The file big.4th is now a part of all three kForth repos (64-bit Linux,
> 32-bit Linux, and Win32). To load and run the tests from kForth:
>
The big number code can be used to perform correct conversion of
IEEE-754 double precision numbers to ascii strings. Below is a proof of
concept demonstration for kForth-64. The dtoa.4th. code demonstrates
conversion of simple cases: normal dp floats, limited number of
converted digits, limited positive and negative exponents -- it is not
yet useful for general dp floats. Some of the limitations stem from the
big number package itself (I think) while others are the simplicity of
the present code, compared to something like dtoa.c.
In addition to fsl/big.4th I needed to add a few more big number words,
big-extras.4th, which is not yet a part of the kForth packages, but is
appended below. The other include files are included in kForth packages.
The dtoa.4th code is presently limited to 64-bit Forths with a separate
floating point stack. This is not an intrinsic limitation however.
--
Krishna Myneni
Proof of concept examples for converting double precision floating point
numbers to big whole numbers.
Note the differences in the exact representations for 1 through 3.
Number 4 is the example Anton Ertl gave using a GNU glibc library
function in a separate thread. Number 5 shows proper handling of the sign.
1)
1.508e0 df>big big.
15080000000000000071054273576010018587112426757812500 ok
2)
1.508e2 df>big big.
1508000000000000113686837721616029739379882812500000000 ok
3)
1.508e3 df>big big.
15080000000000000000000000000000000000000000000000000000 ok
4)
0.1e0 df>big big.
1000000000000000055511151231257827021181583404541015 ok
5)
-0.1e0 df>big big.
-1000000000000000055511151231257827021181583404541016 ok
While the word BIG. is used to print the converted big number, the big
number pictured output words from big.4th may be used to go from a big
number to a string in the usual manner e.g.
0.1e0 df>big <big# big#s #big> type
1000000000000000055511151231257827021181583404541015 ok
Other words BIGSIGN and BIGHOLD and BIG# may be used in similar fashion
to the ordinary SIGN HOLD and # in pictured output for standard Forth.
The location of the decimal point in the output string can be determined
from the length of the output.
=== Begin dtoa.4th ===
\ dtoa.4th
\
\ Convert IEEE-754 double-precision floating point to a decimal string.
\
\ K. Myneni
\ 2026-08-02
\
include ans-words
include modules
include ieee-754
include fsl/big
include fsl/extras/big-extras
[UNDEFINED] fp-stack? [IF]
: fp-stack? [DEFINED] fdepth literal ;
[THEN]
1 CELLS 4 = CONSTANT 32bit?
1 CELLS 8 = CONSTANT 64bit?
64bit? FP-STACK? and 0= [IF]
cr .( Currently requires 64-bit + separate FP stack!)
cr
QUIT
[THEN]
BASE @
HEX
8000000000000000 constant DP_SIGNBIT_MASK
000FFFFFFFFFFFFF constant DP_FRACTION_MASK
7FF0000000000000 constant DP_EXPONENT_MASK
DECIMAL
52 constant DP_FRACTION_BITS \ number of binary digits in fraction
1023 constant DP_EXPONENT_BIAS
8 constant BIG_DBL \ max no. of cells for big number mantissa
\ BIG number constants and variables
create 5^52 BIG_DBL CELLS allot 5 52 big_s^n 5^52 big-move
create 10^52 BIG_DBL CELLS allot 10 52 big_s^n 10^52 big-move
create mantissa BIG_DBL CELLS allot
: get-df-fraction ( -- ud) ( F: r -- )
FP@ @ DP_FRACTION_MASK and s>d ;
\ Convert an IEEE-754 double precision float to a big integer
: df>big ( -- addr) ( F: r -- )
fdup fexponent DP_EXPONENT_BIAS - >R
fdup F0< >R
FABS
get-df-fraction drop >R
big-here dup 5^52 big>here R> ( a1 a1 ufraction) ( F: r)
big*s 10^52 big+
fdrop
R> IF dup bignegate THEN \ restore the sign of the big number
dup
R> dup 0< IF \ handle exponent
\ !!!! correct handling of negative exponents is still needed !!!
abs nip 1 swap lshift
big-here swap 1 big, big, big/
ELSE
1 swap lshift big*s
THEN
;
=== End dtoa.4th ===
=== Begin fsl/extras/big-extras.4th ===
\ big-extras.4th
\
\ Additional definitions for big number arithmetic
\ using FSL #47 big.4th
\
\ K. Myneni, 2026-8-01
\
\ Move a big number from src address to destination
: big-move ( addr1 addr2 -- )
OVER @ ABS 1+ CELLS \ Number of address units in the number
MOVE ;
\ "big factorial"
: big! ( n -- addr )
big-here 1 big, 1 big, \ addr of new big 1
swap abs 1+ 200 min
1 do dup I BIG*S loop ;
\ Raise single length signed integer to a positive
\ integer power and return the result as a big number
: big_s^n ( n1 n2+ -- addr)
dup 0= IF
2drop big-here 1 big, 1 big, EXIT
THEN
swap dup >r ( upow n) ( R: n)
0< IF -1 ELSE 1 THEN big, ( upow) ( R: n)
r> dup abs big, ( upow n)
swap >r ( n) ( R: upow)
big-here 2 cells -
swap r> ( addr n upow)
1 ?DO 2dup big*s LOOP drop ;
=== End fsl/extras/big-extras.4th ===
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
prelim test code for Forth big number code (FSL #47) Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-26 16:00 -0500
Re: prelim test code for Forth big number code (FSL #47) Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-02 21:33 -0500
Re: prelim test code for Forth big number code (FSL #47) Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-05 06:47 -0500
Re: prelim test code for Forth big number code (FSL #47) Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-13 07:24 -0500
csiph-web