Article View: comp.lang.forth
Article #78Re: FCONSTANT specification
From: Anton Ertl
Date: Wed, 14 Oct 2026 05:50
Date: Wed, 14 Oct 2026 05:50
103 lines
6393 bytes
6393 bytes
marcel hendrix <mhx@iae.nl> writes:
>On 10/12/2026 8:36 PM, Anton Ertl wrote:
>> marcel hendrix <mhx@iae.nl> writes:
>>> In view of the current thread of floating-point input and output, how
>>> should we specify FCONSTANTs ? My concern is when the system potentially
>>> uses single, double, or extended-double (or worse).
>>
>> Standard Forth only has one FP type on the FP stack, and FCONSTANT
>> stores that and pushes that, just like F! and F@ do.
>>
>
>Procrustes Forth? On such a system SF@/! and DF@/! must convert an
>internal to an external format, and the conversion must be precise.
Yes, at least one of them, and possibly both have to perform the
conversion. And if you use the 387 FP instructions, both have to
perform a conversion even if you set the mantissa length to 53 or 24
bits, because the exponent length still is 16 bits, i.e., you can
represent numbers as normal number in the internal format that are
denormal or infinity in the external format.
Concerning precision, there is the double-rounding problem
<https://en.wikipedia.org/wiki/Rounding#Double_rounding>, e.g., if you
perform an FP operation like F+ at 80 bits (or even at 53+16 bits),
and then convert to binary64, in some cases you get a different (less
precise) result than if you had performed FP addition directly in
binary64 format (with only one rounding round), e.g., with SSE2.
I have read that double rounding for the FP operations where IEEE FP
requires a specific result (rather than allowing any result up to a
certain error), in particular F+ F- F* F/ FSQRT, double rounding does
not change the result compared to single rounding if the mantissa
length of the first rounding is more than twice as long than the
mantissa length of the second rounding. E.g., F+ SF! should not
suffer from double-rounding errors if the FP stack uses a 53-bit or
64-bit mantissa.
>Switching between single and double precision is a well-known trick to
>improve convergence of certain algorithms.
I have not heard of that. Why should doing the first steps in SP
improve convergence over doing it all in DP or extended precision?
>>> Is the algorithm that guarantees that a printed number can be converted
>>> 'losslessly' back to its internal presentation able to do that
>>> regardless of the number of bits in the internal representation?
>>
>> Sure.
>
>My question is: if we have a text representation of a number with say 40
>(or whatever) digits, will that unchanged string work to give the
>best approximation on single, double, ... binary internal formats?
>Is your answer to that: 'Sure' ?
Actually, having read up on the double-rounding problem, I am no
longer sure: I have come across
<https://www.exploringbinary.com/double-rounding-errors-in-floating-point-conversions/>,
which shows that for decimal-to-FP conversion there are cases where
double rounding can give a result that's further from the actual value
than 1/2 ulp. And when you store an irrational or otherwise
arbitrarily long number with finite length, you already have one
rounding step when performing this storing.
However, the problem affects only relatively few numbers, and you can
store as many digits of your infinitely long number that the problem
does not occur for it, for the desired mantissa lengths. Once you
have that, you "only" need a decimal-to-binary (or, if you store the
numbers in hex format, a hex-to-binary) conversion routine that
performs the appropriate rounding (not what Visual C++ does according
to
<https://www.exploringbinary.com/double-rounding-errors-in-floating-point-conversions/>.
>> You will have to work with some limit, but I expect that you can make
>> numbers with, say 36 digits, and ideally you can derive the numbers
>> for various formats up to binary128 from that. But if the mathematics
>> behind these numbers is not just to use an approximation to something,
>> but to have a bias that corrects for some error elsewhere, maybe that
>> is not enough.
>...you can derive the numbers...
>
>The question still is: How?
Gforth converts string-to-double with strtod(), and glibc apparently
implements it to give you a result within 1/2 ulp (and it also has
strtold() for long double); for double-extended numbers, you will have
to look at the algorithms that are used for that problem, e.g., in
strtod().
For producing numbers for your table, I would just produce longer and
longer decimal (or hexadecimal) numbers, convert them to all the
formats that you are interested in, check that the result is less than
1/2 ulp (for the format) from the actual number, and stop when that's
the case for all the target formats. With double-extended as one of
the target formats, this will probably require 42 digits of mantissa
or more. I have not worked with Mathematica, but from what I have
read about it, it seems to be the tool for this job.
- 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
Technical Details
Message-ID:
<2026Oct14.075049@mips.complang.tuwien.ac.at>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11aide4$1ibse$1@dont-email.me> <2026Oct12.203631@mips.complang.tuwien.ac.at> <11am4ae$fte4$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: anton@mips.complang.tuwien.ac.at (Anton Ertl) Newsgroups: comp.lang.forth Subject: Re: FCONSTANT specification Date: Wed, 14 Oct 2026 05:50:49 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 103 Message-ID: <2026Oct14.075049@mips.complang.tuwien.ac.at> References: <11aide4$1ibse$1@dont-email.me> <2026Oct12.203631@mips.complang.tuwien.ac.at> <11am4ae$fte4$1@dont-email.me> Injection-Date: Wed, 14 Oct 2026 06:56:15 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1317364"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19POJcVL4tpAYhlRWhtuhKx"; posting-host="4798f8a33d7cba4f1cfb66046bec21f9" Cancel-Lock: sha1:94hYjnuf8T58Sd6W+9U7JN/UUVI= sha256:p2IKPJddRSek/jSSvyk4P/zBunEmplKMLjEZqHjgQoE= sha1:geqbvsqW6ISItSr7juGCHUTHfn8= sha256:JBDeS/M9uejb2r0hjgyEgHOHziRfkn/ELAEuq5nHFuE= X-newsreader: xrn 10.11