• Fast base 10 mantissa / exponent to base 2 mantissa / exponent conversion

    From Hans Bezemer@the.beez.speaks@gmail.com to comp.lang.forth on Fri Jun 19 18:34:26 2026
    From Newsgroup: comp.lang.forth

    For those who simply ask - and don't start to whine like little children
    - I'll provide any missing word definitions. ;-)

    Lemme help you getting started:

    0 constant u>d
    : >zero dup xor ;
    : ?exit if exit then ;

    create (mant) 2 cells allot \ limit on mantissa
    variable (exp) \ limit on exponent

    : normalize10 ( d1 n1 d2 n2 -- d' n')
    (exp) ! (mant) 2! over 0< >r >r dabs \ save sign and exponent
    begin 2dup (mant) 2@ d< while (exp) @ r@ < while d10* r> 1- >r repeat
    r> -rot r> if dnegate then 2dup d0= if rot >zero ;then rot
    ; \ zero is a special case

    : me10>me2 ( m1 m2 e10 -- m3 m4 e2)
    dup 0= ?exit over >r >r dabs ( |m1 m2| R: msign e10)
    r@ abs dup 10 * 2 + 3 / ( |m1 m2| |e10| |e2| R: msign esign)
    dup r> 0< if negate then >r ( |m1 m2| |e10| |e2| R: msign e2)
    >r 10 swap ** 1 r> lshift ( |m1 m2| |n^e10| |n^e2| R:
    msign e2)
    r@ 0< if swap then >r mu* r> mu/ ( |m1 m2| R: msign e2)
    r> -rot r> if dnegate then rot ( m1 m2 e2)
    ;

    64bit? [IF] -18 [ELSE] -9 [THEN] constant (exp-max)

    : me10>norm>me2 max-n 10 / u>d (exp-max) normalize10 me10>me2 ;

    Hans Bezemer
    --- Synchronet 3.22a-Linux NewsLink 1.2