-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftrunc.html
270 lines (204 loc) · 9.59 KB
/
ftrunc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<title>Floating point truncation</title>
<!--
From: Krishna Myneni <[email protected]>
Newsgroups: comp.lang.forth
Subject: Cfv: Floating point truncation
Message-ID: <[email protected]>
Date: Fri, 15 May 2009 08:13:18 -0500
-->
<h3>Floating point truncation</a>
<h4>Poll standings</h4>
See <a href="#voting">below</a> for voting instructions.
<h5>Systems</h5>
<pre>
[ ] conforms to ANS Forth.
iForth (Marcel Hendrix)
VFX Forth for Windows, Linux and DOS (Stephen Pelc)
Gforth (Anton Ertl)
[ ] already implements the proposal in full since release [ ].
iForth 3.0.3 (Marcel Hendrix)
kForth 1.0.14 (Krishna Myneni)
Win32Forth 1 (George Hubert)
Gforth 0.7.0 (Anton Ertl)
[ ] implements the proposal in full in a development version.
[ ] will implement the proposal in full in release [ ].
4th 3.5d release 2 (Hans Bezemer)
[ ] 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.
</pre>
<h5>Programmers</h5>
<pre>
[ ] I have used (parts of) this proposal in my programs.
Hans Bezemer
Marcel Hendrix
Krishna Myneni
George Hubert
[ ] I would use (parts of) this proposal in my programs if the systems
I am interested in implemented it.
David N. Williams
[ ] I would use (parts of) this proposal in my programs if this
proposal was in the Forth standard.
David N. Williams
Stephen Pelc
[ ] I would not use (parts of) this proposal in my programs.
</pre>
<h5>Informal results</h5>
<h4>Proposal</h4>
<pre>
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
========================
<a href="reference-implementations/ftrunc.fs">Reference Implementation</a>
Testing
=======
<a href="tests/ftrunc.fs">Tests</a>
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
</pre>
<h4><a name="voting">Voting instructions</a></h4>
Fill out the appropriate ballot(s) below and mail it/them to me
<[email protected]>. Your vote will be published
(including your name (without email address) and/or the name of your
system) here. You can vote (or change your vote) at any time by
mailing to me, and the results will be published here.
<p>Note that you can be both a system implementor and a programmer, so
you can submit both kinds of ballots.
<h5>Ballot for systems</h5>
If you maintain several systems, please mention the systems separately
in the ballot. Insert the system name or version between the brackets
or in the line below the statement. Multiple hits for the same system
are possible (if they do not conflict).
<pre>
[ ] 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.
</pre>
If you want to provide information on partial implementation, please do
so informally, and I will aggregate this information in some way.
<h5>Ballot for programmers</h5>
Just mark the statements that are correct for you (e.g., by putting
your name on the line below the statement you agree with). 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.
<pre>
[ ] 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.
</pre>
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.
<h5>Credits</h5>
<pre>
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
</pre>
<hr><a href="http://www.complang.tuwien.ac.at/anton/">Anton Ertl</a>