continued fractions

100 views
Skip to first unread message

Hugh Aguilar

unread,
Dec 13, 2012, 10:45:03 PM12/13/12
to Cat Language
Earlier somebody named L.N. posted a message asking if Cat would be a
good language for continued fractions. I think the answer is: no. Cat
doesn't have mixed-precision integer arithmetic like Forth does, so it
is a no go.

In my novice package (http://www.forth.org/novice.html) I provide a
file cf.4th with continued fractions. This is the only file in the
novice package that I didn't write myself. It also provides D/ which
is missing in standard Forth.

The purpose of continued fractions in Forth, is to calculate rational
approximations. For example, 355/113 for pi. These are used with mixed-
precision arithmetic functions, such as */ the scalar:

*/ (a b c -- n ) The result is (a*b)/c. The clever part, is that
the intermediate result (the product of the multiplication) is a
double-precision integer. For example, on a 16-bit computer, you can
do this:
10000 355 113 */ and get a result of 31415. This is despite the fact
that the multiplication produced an intermediate result of 3550000,
which overflows the 16-bit cell. Mixed-precision arithmetic allows a
program to use single-precision arithmetic, which is faster than
double-precision, and yet avoid a lot of the overflow problems that
typically force programmers (I mean C programmers) to upgrade to
double-precision.

I'm not clear on what Cat is for. Without support for mixed-precision
integer arithmetic as briefly discussed above, Cat isn't going to
compete very well against Forth in the micro-controller world. This
means that Cat would be used on desktop computers. But Cat is a pretty
minimal language compared to the myriad languages already available,
so it is a pretty hard sell for application writing.

I get the impression that Christopher Diggins either didn't know
Forth, or didn't like it. We are missing a lot of features that Forth
has. Also, we seem to have gratuitous differences from Forth --- such
as POP rather than DROP --- that make Cat programming awkward for
Forthers. I had the same complaints about Factor and PostScript
though, so Cat isn't alone in this respect.

I'm interested in Cat though. I think there are some good ideas in
there.

Cat might be useful as an embedded scripting language, similar to Lua.
Has this been done? The application I have in mind is to generate
gcode for CNC machines, and to provide a DSL that the machinists can
use to write programs to do this (I have worked as a CNC programmer
myself, and I have written programs in Forth to generate gcode).
Machinists typically don't know much about programming, so the DSL has
to be pretty simple for them to use it --- but Cat is pretty simple
--- C.D. himself has said that it is easy for people to learn as their
first programming language.
Reply all
Reply to author
Forward
0 new messages