A few days ago, I released an experiment on how Forth might adapt to
a balanced ternary architecture, with an instruction set emulator and
a native FPGA implementation included. In balanced ternary, all
numbers are signed with low-level arithmetic being quite elegant, and
there are new logic operators to explore. If you like to dive in: >https://codeberg.org/Mecrisp/mecrisp-ternary
Matthias Koch <[email protected]> writes:
...
|In balanced ternary, a right shift is exactly the same as symmetric |division by powers of three, unlike binary in which right-shifting
|negative two-complement numbers to divide by powers of two gives
|rounding artifacts.
Floored division is not any more a rounding artifact than symmetric
division is. Whether to use floored or symmetric division depends on
the application. That's why we have FM/MOD and SM/REM in the
standard. The Forth-83 committee was so strongly in favour of floored
that they broke compatibility with Forth-79 because of that.
Gforth since 0.7 implements / and other division words where Forth-94
allows the system to choose as floored division words. This means
that in Gforth, "2 /" is equivalent to 2/ (which is defined as a shift
right by 1 bit):
-3 2 / . \ prints -2
-3 2/ . \ prints -2
On 26/05/2026 4:10 am, Anton Ertl wrote:
Gforth since 0.7 implements / and other division words where Forth-94
allows the system to choose as floored division words. This means
that in Gforth, "2 /" is equivalent to 2/ (which is defined as a shift
right by 1 bit):
-3 2 / . \ prints -2
-3 2/ . \ prints -2
Which is all very nice until beginners ask how that makes any sense ;-)
ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,
names.
dxf <[email protected]> writes:
On 26/05/2026 4:10 am, Anton Ertl wrote:
Gforth since 0.7 implements / and other division words where Forth-94
allows the system to choose as floored division words. This means
that in Gforth, "2 /" is equivalent to 2/ (which is defined as a shift
right by 1 bit):
-3 2 / . \ prints -2
-3 2/ . \ prints -2
Which is all very nice until beginners ask how that makes any sense ;-)
They don't (I know because I have been teaching Forth beginners for
three decades). That's because negative dividends are rare, and
negative divisors are even rarer. But in those cases where negative dividends occur, floored division usually makes sense, and users
encountering it don't ask.
ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,
names.
The name of 2* is not misleading on architectures with 2s-complement arithmetic. I.e., every architecture designed in the last
half-century (the IBM S/360 is actually 62 years old), and everything
that any existing standard system (for any Forth standard) runs on.
...
... I have been teaching Forth beginners for three decades. ...
On 5/26/2026 12:46 AM, Anton Ertl wrote:
... I have been teaching Forth beginners for three decades. ...
Does your university offer a class on Forth?
https://dl.acm.org/doi/pdf/10.1145/165628.165632 https://www.cs.uaf.edu/~chappell/class/2023_spr/cs331/read/forth_quick.html
Importantly it didn't make sense to forth vendors to make breaking
changes for something that was occasionally useful.
And it's on such machines that one finds symmetric hardware division >co-existing with arithmetic right shift instructions and coders not
getting freaked out by it.
On 5/26/2026 12:46 AM, Anton Ertl wrote:
... I have been teaching Forth beginners for three decades. ...
Does your university offer a class on Forth?
https://www.cs.uaf.edu/~chappell/class/2023_spr/cs331/read/forth_quick.html
dxf <[email protected]> writes:
Importantly it didn't make sense to forth vendors to make breaking
changes for something that was occasionally useful.
Experience: Gforth used to do for /, MOD, and a number of other
division words what the C compiler does, i.e., symmetric division,
before Gforth 0.7. After a discussion Bernd Paysan switched these
words to floored division for Gforth 0.7. I was not particularly
happy, because I worried about "breaking changes". However, we got 0 complaints about this change, which indicates that these words are not
used with operands with different signs, at least not where the
remainder is different from 0, or that the users are not particularly
picky about the result in those cases.
Anyway, in development Gforth you can now choose for every word
explicitly, with /S or /F. / is a synonym of /F, but if somebody is
really into symmetric division, they can define
SYNONYM / /S
and likewise for the other words.
And it's on such machines that one finds symmetric hardware division
co-existing with arithmetic right shift instructions and coders not
getting freaked out by it.
If all you ever divide are positive numbers, you don't notice the
difference.
Buzz McCool <[email protected]> writes:
On 5/26/2026 12:46 AM, Anton Ertl wrote:
... I have been teaching Forth beginners for three decades. ...
Does your university offer a class on Forth?
I have had a course "Stack-based languages" from the late 1990s to
2024. I taught Forth and Postscript. Students did projects in Forth
or Postscript. In recent years it has had few students and most of
them did projects in Forth.
We were tasked with changing the courses to 6 ECTS (credits);
Stack-based Languages had 3 ECTS. I decided to replace Stack-based
Languages with a new course "Low-Level Languages", where I teach Forth
and Rust to students. I did this course the first time last semester.
This course attracted a lot more students. The students surprised me
by often using neither language for the project, but instead, e.g.,
C++ (which is also acceptable).
https://www.cs.uaf.edu/~chappell/class/2023_spr/cs331/read/forth_quick.html
Looking at <https://www.cs.uaf.edu/~chappell/class/2026_spr/cs331/>,
the course covers several programming languages (which explains the
shortness of the introduction), but in 2026 the course no longer
includes Forth. Anyway, good to see that in 2023 there was at least
one other course that taught Forth, and that course used Gforth.
Nevertheless I am surprised to read that Rust and C++ have become
low-level languages.
As a non-vendor with no paying customers, no warranty, no obligations,
I too feel I can make changes without expecting much, if any, flack.
Only reason to support an ASR
instruction is division of negative numbers.
dxf <[email protected]> writes:
As a non-vendor with no paying customers, no warranty, no obligations,
I too feel I can make changes without expecting much, if any, flack.
We do get feedback on some changes in Gforth. In the case of the
switch to floored division, we did not. We were prepared for a quick
fix by having symmetric division as a configuration option. Maybe the
people who prefer symmetric division rebuilt Gforth with that option,
but my impression is that most Gforth users use a binary package, and
do not rebuild Gforth from source.
dxf <[email protected]> writes:
Only reason to support an ASR
instruction is division of negative numbers.
Another common use is sign extension in cases where no more
specialized sign-extension instruction exists; e.g., if you want to sign-extend an 11-bit field.
ANS at least tells you 2* 2/ are bit-shifters with historic, albeitmisleading,
names.You know I'm a pragmatic guy. Back in the 32 bit era, I tested every
On 26-05-2026 07:53, dxf wrote:
ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,You know I'm a pragmatic guy. Back in the 32 bit era, I tested every single value with both 2* and "2 *" - no difference. So, 2* essentially became "2 *".
names.
Very different story with 2/. So, there is a unique opcode 2/ in 4tH - but not a true 2*. ;-)
On 2/06/2026 7:23 am, Hans Bezemer wrote:
On 26-05-2026 07:53, dxf wrote:
ANS at least tells you 2* 2/ are bit-shifters with historic, albeit misleading,You know I'm a pragmatic guy. Back in the 32 bit era, I tested every single value with both 2* and "2 *" - no difference. So, 2* essentially became "2 *".
names.
Very different story with 2/. So, there is a unique opcode 2/ in 4tH - but not a true 2*. ;-)
In eForth one finds:
: 2* 2 * ;
: 2/ 2 / ;
which rather defeats the purpose of the functions which is speed.
As to what is returned by a bit-shifting 2/ depends on the hardware.
For 2's complement it's floored. For 1's complement it's symmetric.
For signed-magnitude it's ...
On 2/06/2026 7:23 am, Hans Bezemer wrote:
In eForth one finds:
: 2* 2 * ;
: 2/ 2 / ;
which rather defeats the purpose of the functions which is speed.
As to what is returned by a bit-shifting 2/ depends on the hardware.
For 2's complement it's floored. For 1's complement it's symmetric.
For signed-magnitude it's ...
True. "2 /" is twice as slow.
Small detail - first I thought "2 /" was faster. That was because the >optimizer was able to fold the value and consequently create a constant.
And - the reason for this all - tokens are a precious resource in 4tH.
I'd rather use it for something else.
dxf <[email protected]> writes:
On 2/06/2026 7:23 am, Hans Bezemer wrote:
In eForth one finds:
: 2* 2 * ;
: 2/ 2 / ;
which rather defeats the purpose of the functions which is speed.
But eForth's purpose is not speed.
As to what is returned by a bit-shifting 2/ depends on the hardware.
For 2's complement it's floored. For 1's complement it's symmetric.
For signed-magnitude it's ...
For sign-magnitude an arithmetic interpretation is that, for positive numbers, it's division by 2, while for negative numbers, it's the
average between the number and -2^(n-1) (where n is the number of bits
in a cell). It's not very useful for sign-magnitude, but given that sign-magnitude and ones-complement have died out long ago and never
had Forth systems, who cares.
On 4/06/2026 10:08 pm, Anton Ertl wrote:[On 2/:]
It's not very useful for sign-magnitude, but given that
sign-magnitude and ones-complement have died out long ago and never
had Forth systems, who cares.
Thanks. Not dead enough for Forth-94 which was the first standard to >expressly mention and support them.
For that reason I was curious what
2/ would do on these. As the effect does vary, I would expect '94 to
have documented it.
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,123 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 36:19:47 |
| Calls: | 14,371 |
| Files: | 186,380 |
| D/L today: |
2,292 files (656M bytes) |
| Messages: | 2,540,645 |