Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #135304

Re: E.P E. E.EXACT

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: E.P E. E.EXACT
Date 2026-08-06 14:00 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2026Aug6.160054@mips.complang.tuwien.ac.at> (permalink)
References <2026Aug5.200203@mips.complang.tuwien.ac.at> <115219i$3rqt4$1@dont-email.me>

Show all headers | View raw


Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>On 8/5/26 13:02, Anton Ertl wrote:
>> Gforth now has three new words:
>> 
>> E. ( r -- ) prints an FP number r in a format that is understood by
>>     Gforth's text interpreter and produces the same number.  Tries to
>>     produce short outputs.
>> 
>> E.EXACT ( r -- ) prints an FP number, showing all (possibly hundreds)
>>     of the mantissa digits.
>> 
>> E.P ( r u -- ) prints an FP number with at most u (or, if that saves
>>     printing an exponent, u+1) mantissa digits.
>> 
>> Example usage and output (as comment):
>> 
>> : s-as-f {: w^ n :} n f@ ;
>> 0e e.         \ 0e ok
>> 1e e.         \ 1e ok
>> 0.1e e.       \ .1e ok
>> 0.00005e e.   \ .00005e ok
>> 0.000005e e.  \ 5e-6 ok
>> 2000e e.      \ 2e3 ok
>> 20000e e.     \ 2e4 ok
>> 1 s-as-f e.   \ 5e-324 ok
>> 
>> Note that the latter number takes more than 700 digits when printed
>> with e.exact; only one mantissa digit is necessary to distinguish it
>> from other numbers, because the next FP number has a value close to
>> 1e-323.
>> 
>> 0e e.exact        \ 0e ok
>> 1e e.exact        \ 1e ok
>> 0.1e e.exact      \ .1000000000000000055511151231257827021181583404541015625e ok
>> 0.00005e e.exact  \ .0000500000000000000023960868011929647991564706899225711822509765625e ok
>> 0.000005e e.exact \ 5.0000000000000004090152695701565477293115691281855106353759765625e-6 ok
>> 2000e e.exact     \ 2000e ok
>> 20000e e.exact    \ 20000e ok
>> 2e23 e.exact      \ 199999999999999983222784e ok
>> 
>> Note that the difference between the input and the output in these
>> cases comes from the input side (>FLOAT): 0.1e, 0.00005e, 0.000005e
>> and 2e23 cannot be represented exactly in binary64 (IEEE
>> double-precision) FP, do >FLOAT produces the FP number closest to the
>> decimal representation.  E.EXACT then prints all the digits of this
>> number, while E. prints a number that is close and consumes as few
>> digits as possible.
>> 
>> 0e 6 e.p        \ 0e ok
>> 1e 6 e.p        \ 1e ok
>> 0.1e 6 e.p      \ .1e ok
>> 0.00005e 6 e.p  \ .00005e ok
>> 0.000005e 6 e.p \ 5e-6 ok
>> 2000e 6 e.p     \ 2000e ok
>> 20000e 6 e.p    \ 20000e ok
>> 1234567e 6 e.p  \ 1234567e ok
>> 
>
>I would use FS.EXACT (instead of E.EXACT) and keep the output in 
>scientific notation.

Go ahead.  I find the non-scientific notation helpful when it does not
introduce too many zeros.

> Less to remember that way.

?


>Similarly, E.P is unneeded if you format the output in scientific 
>notation and use FS. in combination with SET-PRECISION to specify the 
>number of digits after the decimal point.

10 set-precision 20e fs. \ 2.000000000E1  ok
20e 10 e.p               \ 20e ok

To each their own.  Also, E.P is a factor of E. and E.EXACT, so not
documenting it buys little.

>How does "E." work for an inexactly representable binary64 floating 
>point number?

For printing r, it first tries

r 1 e.p

and looks if applying >FLOAT on the result produces r.  If not it tries

r 2 e.p

and so on.  In the end you have the string with the shortest mantissa
that converts back to r.

Yes, this approach is expensive (~9us per e. for random normal numbers
on a Ryzen 8700G (~5GHz Zen4)).

In the meantime I found, that for normal (not subnormal) numbers, it's
probably enough to test

r 15 e.p
r 16 e.p

and if that's not enough

r 17 e.p

can be used without checking (that's for binary64 numbers, other
formats need other precisions).  The benefit would be that e. now only
takes about 1.6us.  One would need a mathematical proof to be sure,
though.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-05 18:02 +0000
  Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-06 08:13 -0500
    Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-06 14:00 +0000
      Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-06 10:45 -0500
        Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-06 15:58 +0000
          Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-06 17:20 +0000
        Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-06 21:05 +0200
          Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-06 20:38 +0000
          Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-06 18:44 -0500
          Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-07 11:03 +1000
            Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-07 10:11 +0000
              Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-08 14:39 +1000
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-08 06:32 +0000
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-09 04:50 +1000
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-10 20:01 +1000
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-10 18:06 +0000
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-10 17:24 +0000
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-11 14:01 +1000
                Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-10 22:10 -0500
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-11 04:56 +0000
                Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-11 22:16 -0500
                Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-12 05:47 -0500
                Re: E.P E. E.EXACT peter <peter.noreply@tin.it> - 2026-08-12 18:03 +0200
                Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-12 11:48 -0500
                Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-08 09:14 +0200
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-08 09:40 +0000
                Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-08 15:26 +0200
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-10 16:43 +0000
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-09 13:43 +1000
                Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-09 07:19 +0200
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-10 19:15 +1000
                Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-10 13:17 +0200
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-10 22:59 +1000
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-10 23:54 +1000
                Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-10 20:42 +0200
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-11 17:10 +1000
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-12 14:04 +1000
                Re: E.P E. E.EXACT albert@spenarnc.xs4all.nl - 2026-08-22 16:43 +0200
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-22 15:47 +0000
                Re: E.P E. E.EXACT albert@spenarnc.xs4all.nl - 2026-08-22 18:27 +0200
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-22 16:40 +0000
                Re: E.P E. E.EXACT albert@spenarnc.xs4all.nl - 2026-08-22 19:42 +0200
                Re: E.P E. E.EXACT anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-22 18:03 +0000
                Re: E.P E. E.EXACT albert@spenarnc.xs4all.nl - 2026-08-22 22:56 +0200
                Re: E.P E. E.EXACT marcel hendrix <mhx@iae.nl> - 2026-08-23 00:50 +0200
                Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-23 12:25 +1000
                Re: E.P E. E.EXACT Hans Bezemer <the.beez.speaks@gmail.com> - 2026-08-09 14:52 +0200
            Re: E.P E. E.EXACT Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-07 07:50 -0500
              Re: E.P E. E.EXACT dxf <dxforth@gmail.com> - 2026-08-08 12:33 +1000
                Re: E.P E. E.EXACT albert@spenarnc.xs4all.nl - 2026-08-22 17:01 +0200
          Re: E.P E. E.EXACT albert@spenarnc.xs4all.nl - 2026-08-22 16:15 +0200

csiph-web