ANSI Forth BASE upper limit vs. gforth upper limit

2019-09-15 07:46发布

问题:

Fiddling with the gforth version of BASE shows that BASE can be used for values past those most languages permit. For example, this prints the number 0ABC (base 15950) in decimal, and vice versa:

gforth -e '15950 base ! ABC decimal . cr bye'
gforth -e '2544200462 15950 base ! . cr bye'

Output:

2544200462
ABC

Without writing additional Forth words, what are the default Gforth and ANSI Forth BASE upper limits for meaningful conversions both to and from numbers of different bases?

(Ignore for the moment the complexities of mapping an existing character set to depict some arbitrary radix, or else imagine an endless fractal character set that adds little 30-150 degree serifs when new characters are needed.)

回答1:

According to the standard, it is

BASE

( -- a-addr )

a-addr is the address of a cell containing the current number-conversion radix {{2...36}}.

So it is 2..36.

That makes sense, doesn't it?

  • The lower limit is 2 because any number of digits less than 2 would be useless.
  • The upper limit is 36 because that allows for 0-9 and A-Z as digits, case insensitive.


回答2:

The maximum value ofBASE in Gforth v0.7.2+dfsg1-1.1, (for converting ABC to a number and back again), seems to be the unexpectedly large 960,383,882. That number was found and tested by comparing Gforth's output with calc (an arbitrary precision calculator which can whip out all 76,976 digits of (27^3)! in about a second), and as used below calc is presumed to be correct:

# Output Gforth calculation of (12*b^2)+(11*b^1)+(10*b^0) to
# base 10 and inversely back to base b.
b=960383882 n=ABC ; bd=$(gforth -e $b' base ! '$n' decimal . cr bye')
gforth -e "${bd} dup . cr $b"' base ! . cr bye'
calc "10*($b^2)+11*($b^1)+12*($b^0)" | xargs echo
9223372018618121954 
ABC 
9223372018618121954

# It fails here:
b=960383883 n=ABC ; bd=$(gforth -e $b' base ! '$n' decimal . cr bye')
gforth -e "${bd} dup . cr $b"' base ! . cr bye'
calc "10*($b^2)+11*($b^1)+12*($b^0)" | xargs echo
-9223372035883752001 
-A8f 
9223372037825799615

Tests for upper limit of $n, (i.e. the n=ABC in the code above) pending...



标签: base gforth