This page uses CSS, and renders properly in the latest versions of Microsoft Internet Explorer and Mozilla.
This is Frontier: The Definitive Guide by Matt Neuburg, unabridged and unaltered from the January 1998 printing.
It deals with the freeware Frontier 4.2.3; for commercial Frontier 8, look here, and for the inexpensive Radio UserLand, look here. See my Web site for information.
Those wishing an offline or printed copy may purchase the book, which is still in print.
Copyright 1998 O'Reilly & Associates, Inc. ALL RIGHTS RESERVED. Reprinted with permission.
TOP UP: Data Manipulation NEXT: Dates

15

Math

Numeric datatypes and coercions, and how these affect the results of arithmetic operations, are discussed Chapter 10, Datatypes. Recall that if both operands are of the same type, the result will be that type; hence, when integers are divided, the result is an integer, and when integers are added, subtracted, or multiplied, the result may "wrap around" in order to stay within the range of permissible integer values.

Basic arithmetic is performed using familiar operators: addition (+), subtraction (-), multiplication (*), and division (/). If an expression contains more than one arithmetic operator, the pairs are evaluated in left-to-right order, except that multiplication and division are evaluated before addition and subtraction. To override this order of evaluation, enclose in parentheses any expressions to be evaluated first. So:

2 + 1 * 3
    « 5
(2 + 1) * 3
    « 9

The modulus or remainder operator (%) operates on integers, yielding the remainder when the first operand is divided by the second. The verb mod() may be used instead. Thus:

7 % 2
    « 1
mod (7, 2)
    « 1

Absolute values are obtained with the verb abs() .

Like C, UserTalk has the unary increment and decrement operators (++ and --). They do nothing that cannot be accomplished more verbosely without them, but they are extremely convenient. Used with the name of a variable or database entry, they respectively add 1 to and subtract 1 from its value, and this is done either before or after the context as a whole is evaluated, depending on whether the operator appears before or after the name. In Example 4-7, the line:

while --t {s = s + "    "}

decrements t and then evaluates it for the while test. In this example it is crucial that t be decremented before we check it, because if its value before we start is 1 we don't want to append any spaces to s.

Many mathematical verbs are provided through a UCMD, trigCmd , living in system.extensions. Since there is a search path to system.extensions, it suffices to call one of these by prefixing trigCmd to its name; for example, trigCmd.sqrt(10).

The trigCmd verbs are: abs(); floor(), and ceil(), which round downwards or upwards, respectively; sqrt() ; sin(), cos(), tan(); asin(), acos(), atan(); sinh(), cosh(), tanh(); exp(), log(), log10(). Several other functions can be derived from these; for example, one could write a utility pow() that raises its first parameter to the power of its second parameter, thus:

on pow (x, y)
    with trigCmd
        return exp(log(x)*y)

and similarly for finding n th roots. Other utilities, such as max() and min(), may be worth writing as well.

To obtain a decimal number's hexadecimal representation, use string.hex() . For example, string.hex(1234) is "0x04D2".

To convert from hexadecimal to decimal, just use number(). Since hexadecimals are strings starting with "0x", if you have a pure hex string such as "F1" you can convert it to decimal as follows:

hexstring = "F1" « or whatever
decimalValue = number ("0x" + hexstring)

You can convert bytes to kilobytes using string.kBytes() ; the result is a string representing an integral number of kilobytes and ending in "K". For example, string.kBytes(1234) is "2K". See also samples.basicstuff.megabyte-String() for a similar conversion to megabytes.

The verb random() returns a pseudo-random integer.

Three bit commands operate on individual bits of a long. A long may be considered as made up of 8 hex digits, 32 binary digits; so its bits are numbered 0 to 31 (where 0 is the low-order bit). See on bit.set() , bit.clear(), and bit.get() in Chapter 46, Verbs.

Three rectangle commands are utility scripts to make some very basic manipulation of rects more convenient. See Chapter 46 on rectangle.random(), rectangle.inset() and rectangle.outset(). The last two are complementary, giving identical results if their parameters are the additive inverse of one another.


TOP UP: Data Manipulation NEXT: Dates

This is Frontier: The Definitive Guide by Matt Neuburg, unabridged and unaltered from the January 1998 printing.
It deals with the freeware Frontier 4.2.3; for commercial Frontier 8, look here, and for the inexpensive Radio UserLand, look here. See my Web site for information.
Those wishing an offline or printed copy may purchase the book, which is still in print.
Copyright 1998 O'Reilly & Associates, Inc. ALL RIGHTS RESERVED. Reprinted with permission.