Cfv: Floating point truncation




This is actually a poll about how widely the proposal is implemented and
how popular it is among the programmers. It is called a CfV
(call-for-votes) because the process is inspired by the Usenet Rdf/CfV
process.

You find the actual ballot further down (look for "VOTING PROCEDURE"),
after the proposal on which you vote.

PROPOSAL

RfD: Floating point truncation

Author: Krishna Myneni

Version 2.2 -- 28 April 2009
Version 2.1 -- 25 April 2009
Version 2 -- 24 April 2009
Version 1 -- 15 April 2009
Version 0 -- 10 April 2009


Problem
=======

In practice, floating point calculations sometimes require rounding of a
floating point number to a nearby integer, with the result being the floating point representation of the integer. ANS Forth provides two words in the optional floating point wordset, FROUND and FLOOR, for performing such rounding. FROUND (DPANS94, 12.6.1.1612) and FLOOR (DPANS94, 12.6.1.1558), which, respectively, provide rounding to the nearest integer, and rounding to the nearest integer on the side of the number line towards negative infinity. Another common form of rounding, often employed, is rounding to the nearest integer on the side of the number line towards zero. The latter form of rounding is often referred to colloquially as "truncation". ANS Forth does not provide a word for rounding towards zero, and returning a floating point representation of the resulting integer. This type of operation is usually carried out through an awkward, and, on non-optimizing systems, inefficient sequence of words, F>D, followed by D>F. On Forth systems which support the use of signed floating point zero, the sequence, F>D D>F, will fail to produce a result (-0E) compatible with the IEEE floating point arithmetic standard (IEEE FP), for an argument, r, such that -1E < r < 0E, and for r = -0E.


Proposal
========

Include a standard word, named "FTRUNC", to perform "truncation", i.e. rounding of a floating point number towards zero, returning a floating point representation of the resulting integer.

FTRUNC f-trunc FLOATING
( F: r1 -- r2 )

Round r1 to an integral value using the "round towards zero" rule, giving r2.


Reference Implementation
========================

The reference implementation below was posted to comp.lang.forth, by George A. Hubert, on 12 April 2009.

: FTRUNC ( r1 -- r2 )
FDUP F0= 0=
IF FDUP F0<
IF FNEGATE FLOOR FNEGATE
ELSE FLOOR
THEN
THEN ;


Testing
=======

Automated tests using ttester.fs. Unless otherwise noted, all tests should pass.

SET-EXACT

t{ -0E FTRUNC F0= -> TRUE }t t{ -1E-9 FTRUNC F0= -> TRUE }t
t{ -0.9E FTRUNC F0= -> TRUE }t
t{ -1E 1E-5 F+ FTRUNC F0= -> TRUE }t
t{ 0E FTRUNC -> 0E }t t{ 1E-9 FTRUNC -> 0E }t
t{ -1E -1E-5 F+ FTRUNC -> -1E }t
t{ 3.14E FTRUNC -> 3E }t t{ 3.99E FTRUNC -> 3E }t t{ 4E FTRUNC -> 4E }t
t{ -4E FTRUNC -> -4E }t
t{ -4.1E FTRUNC -> -4E }t


Remarks
=======

-- FTRUNC complements the ANS Forth words, FROUND and FLOOR, which also return
floating point values.

-- The Intel FPU instruction set contains FRNDINT, which, with the NDP control word appropriately set, will perform the specified truncation, and leave the floating point result on top of the FPU stack in a single instruction.

-- For consistency with the IEEE floating point arithmetic standard (usually abbreviated as IEEE FP), Forth systems which provide a representation for floating point negative zero must return r2 as floating point negative zero (-0E) in the following cases: -1E < r1 < 0E, and r1 = -0E. However, no provision in the current ANS Forth standard, or currently proposed revisions to the standard, guarantees a program can distinguish systems which support -0E. Hence, the sign of floating point zero is unspecified for the above cases of r1.

-- The tests can be made more rigorous with respect to floating point negative zero, if the Forth standard provides a feature enabling a program to check the Forth system for support of fp negative zero. A method which should work in practice on most, if not all, current systems that provide floating point negative zero is

0E FNEGATE 0E 0E F~ 0=

However, in order to guarantee the proper result, the specification for FNEGATE must state that the result of applying FNEGATE to 0E is -0E (and vice-versa). Another approach is to provide an environmental query for the presence of fp signed zero. Either provision will require its own separate proposal to modify the Forth standard.

-- The current ANS Forth specification of F>D is ambiguous. Even though the adoption of a common meaning seems to have occurred, it is nevertheless advisable to revise the language specifying the F>D operation to remove the ambiguity. A separate proposal is warranted for standardization of F>D. Below is the current language of the ANS Forth specification for F>D.

F>D
f-to-d FLOATING

( -- d ) ( F: r -- ) or ( r -- d )

d is the double-cell signed-integer equivalent of the integer portion of r. The fractional portion of r is discarded. An ambiguous condition exists if the integer portion of r cannot be precisely represented as a double-cell signed integer.

Although not specified in the standard, the type of truncation performed is typically truncation towards zero. An extensive discussion of the type of truncation which should be performed by F>D was held on comp.lang.forth several years ago, and the consensus at that time appeared to be that F>D should perform truncation towards zero.


Experience
==========

-- Many languages which provide floating point support, e.g. C, Lisp, FORTRAN, implement a standard word for the floating point rounding towards zero, or "truncation".

-- In early Forth systems, LMI's UR/Forth provided FTRUNCATE, which, reportedly
was equivalent to the sequence, F>D D>F.

-- The following contemporary Forth systems provide a word for rounding a floating point number towards zero, and returning a floating point representation of the result:

System | Word Name | Conforms to specification in this Rfd
------------------------------------------------------------------
PFE | FTRUNC | yes
Gforth | FTRUNC | yes (1)
kForth | FTRUNC | yes
VFX Forth | FINT | yes
iForth | FTRUNC | yes Win32Forth | FTRUNC | yes


(1) Defined as F>D D>F

\ end of RfD


VOTING INSTRUCTIONS

Fill out the appropriate ballot(s) below and mail it/them to
Anton Ertl:

anton@xxxxxxxxxxxxxxxxxxxxxxxxxx

Your vote will be published (including your name (without email address) and/or the name of your system) on

<http://www.forth200x.org/ftrunc.html>

You can vote (or change your vote) at any time by mailing to Anton Ertl, and the results will be published there.

Note that you can be both a system implementor and a programmer, so
you can submit both kinds of ballots.


Ballot for systems

If you maintain several systems, please mention the systems separately
in the ballot. Insert the system name or version between the brackets.
Multiple hits for the same system are possible (if they do not
conflict).

[ ] conforms to ANS Forth.
[ ] already implements the proposal in full since release [ ].
[ ] implements the proposal in full in a development version.
[ ] will implement the proposal in full in release [ ].
[ ] will implement the proposal in full in some future release.
[ ] There are no plans to implement the proposal in full in [ ].
[ ] will never implement the proposal in full.

If you want to provide information on partial implementation, please do
so informally, and I will aggregate this information in some way.


Ballot for programmers

Just mark the statements that are correct for you (e.g., by putting an
"x" between the brackets). If some statements are true for some of your
programs, but not others, please mark the statements for the dominating
class of programs you write.

[ ] I have used (parts of) this proposal in my programs.
[ ] I would use (parts of) this proposal in my programs if the systems
I am interested in implemented it.
[ ] I would use (parts of) this proposal in my programs if this
proposal was in the Forth standard.
[ ] I would not use (parts of) this proposal in my programs.

If you feel that there is closely related functionality missing from the
proposal (especially if you have used that in your programs), make an
informal comment, and I will collect these, too. Note that the best time
to voice such issues is the RfD stage.

CREDITS:

Proponent: Krishna Myneni
People who contributed to/influenced this Rfd (from discussions on comp.lang.forth and email correspondence): George Hubert, "Ed", Bruce McFarling, Marcel Hendrix, Hans Bezemer, Anton Ertl, Andrew Haley, C.G. Montgomery, David N. Williams,
Stephen Pelc
Votetaker: M. Anton Ertl
.



Relevant Pages

  • Re: Rfd: floating point truncation V1
    ... Some other standard I looked at makes "round towards zero" ... an internal representation of floating point negative zero, "-0E", which differs ... to go for an IEEE FP extension). ...
    (comp.lang.forth)
  • Re: Need modern version of old technique to show missing values
    ... The above-mentioned R has the symbol "NA" to indicate missing values, ... distinguished from the usual IEEE754 floating point special values ... negative zero. ... seem determined to use an older standard this option is of course not ...
    (comp.lang.fortran)
  • Re: Rfd: FNEGATE specification for signed zero
    ... The ANS Forth standard does not currently specify the behavior of FNEGATE for floating point zero on systems which support floating point signed zero. ...
    (comp.lang.forth)
  • Re: Rfd: FNEGATE specification for signed zero
    ... The ANS Forth standard does not currently specify the behavior of ... FNEGATE for floating point zero on systems which support floating ...
    (comp.lang.forth)
  • Rfd: floating point truncation V1
    ... In practice, floating point calculations sometimes require rounding of a floating point number to a nearby integer, with the result being the floating point representation of the integer. ... The latter form of rounding is often referred to colloquially as "truncation". ... ANS Forth does not provide a word for rounding towards zero, and returning a floating point representation of the resulting integer. ...
    (comp.lang.forth)