Number Types and Modes
The simplest objects are numbers. The Document has, at any time, two settings: One for the Reading Mode, and another for the precision. These may be changed from the menu of any TextBox or Mathedit Object. And they may also be modified in Scripts by the:
Readexact;
Readlong;
Readfloat;
Readbig;
Precision x;
commands.
We do not discuss in detail here the complex numbers and quaternions. Mathscript interprets a 4-vector of floats [a, x, y, z] as the quaternion: a+x*i+y*j+z*k
For vector operations, this does not affect the behavior in any noticeable way, unless 4-vectors are multiplied together. For other orders, vector multiplication is componentwise, but for quaternions and the complex subfield, it is not.
The current reading mode can affect the way that dialog box settings are read, in particular the precision. If in long reading mode, the function domain cannot be set in a dialog to, say, [-5.5, 5.5]. One must switch to decimal mode for that.
There is a Numtype function that determines the type of a number if desired.
There are 5 number types:
numtype = 0 Long
(long) This is a 64-bit signed integer
There are 5 Reading Modes for numbers:
Long,
They can be set as we said, from the menu of any TextBox or Mathedit Object, or using the commands listed above.
Rational mode
turns off the others. When in rational mode, enter fractions as ...
2/3 ....
If later, you
switch to another mode, rationals will be printed accordingly, but
they are stored as rationals and are suitable for exact arithmetic in
rational mode. These modes can be set using the commands:
Readexact;
Readlong;
Readfloat;
Readbig;
Numbers are converted when read to the appropriate type. Numbers may also be cast to different types, overriding the reading mode, if desired by the functions below. These casting functions are:
Long(x)
The function numtype(x) returns the type of a number that will be: 0 - 4 as above. Number types combine in the following way to produce new numbers



The (*) indicates that the type is BD if the reading mode is Long. Otherwise, the type is D.
(Note: BDs and Rs are first translated to Ds before exponentiation. The result, as for Ds is always a D).
Each number has an internal precision setting that is the precision setting when it was created.
This has meaning only for the Double and BigDec types. For these types, the internal precision is determined by the number of places explicitly after the decimal point..
Precision is set either with the menus described above, or with the MathScript command: Precision. For example:
Precision 10; sets the precision to 10.
All calculations are done with full precision of the return type. Rationals are printed as Doubles if the mode is not exact, otherwise, they are printed as fractions. But results are printed for Double and BigDec in the following way (rounding is down).
Current system precision can be set from the menu or through a function call. Below, 'places' means places after the decimal point.
If the current system precision equals 0:
if the
number precision equals 0, prints all places
if the
number precision is not equal to 0, prints to number precision number
of places.
If the current system precision is not equal to 0:
if the
number precision equals 0, print current precision number of places,
possibly appending 0s
if the
number precision is not equal to 0
if current system precision >= number precision, print current system precision number of places, possibly appending 0s
if current system precision < number precision, print current precision number of places.
For Longs, Rationals, and BigInts, precision has no interpretation
Exact rational and Decimal calculations
Normalization means the following: algebraic expressions have a variety of representations. For example, x²-y² might be represented as (x+y)*(x-y) or as 1-(y²-x²+1), and so on. For each such expression, there is a normal form. In the normal form, all polynomial expressions in one or several variables are represented as sums of monomials. Given two algebraic expressions which are polynomials in one or several variables with rational coefficients: p1 and p2 it is guaranteed that p1 is algebraically equivalent to p2 if and only if normal(p1 - p2) == 0. Translation to normal form implies a large number of automatic simplifications that are desirable in exact rational calculations. Other simplifications are available through the Expert System via the Simplify or Solve commands, especially if appropriate rulesets are supplied. For the expert system to be available in a WorkBook, the WorkBook script should contain the statement: load "algebra".
The simplest way to produce a normal form within a program or script is to use the function d in long mode. This function is the partial differentiation operator. Thus for example, d( f(x,y), x, x, y ) is the third partial of f(x,y), twice with respect to x, and once with respect to y. The return value is automatically in normal form.
Now when d is used without any parameters, e.g. d( f(x,y) ) it is interpreted as a 0th order differential operator: the identity function. But it returns automatically the normal form of its argument. Thus, make u d( (x+y)^2 - (x-y)^2 ) has the effect of setting u to 4*x*y. Often, especially when algebraic expressions are to be compared within scripts, this is the best way to obtain an exact comparison. That is, if d( p1 - p2) == 0, then you know that p1 = p2.
Certain commands automatically use the exact mode for their calculations. They are meant to be executed from command lines, and they write their normalized result automatically to a MathEdit object if one happens to be open. When executed in scripts, they do not write to Math Edit objects. These commands store their result in the variable answer. These commands should always be done in exaxt or long mode.
The commands of this type are:
The Expand Command
The Dif Command
The Solve Command
The Simplify Command
The Factor Command
In addition to these, there is a command that writes normalized results to Math Edit windows from within scripts or programs:
The Output Command with the /p option. This does not save the result in answer but is used to send normalized output to Math Edit Windows in the course of executing scripts.
When working with rational numbers in exact mode use:
The make or let command with the exact option. This does not use the full normal form, but it guarantees that fractions will not be translated to decimals. For example, if MyProg(x,y) takes fractions x and y and does arithmetic with them to produce another number, then
make u exact myprog(1/2,1/3) or u := exact myprog(1/2,1/3) will set u to its value without translating the fractions 1/2 and 1/3 (or any intermediate fractions created in MyProg) to decimals.
The numerators and denominators of fractions are long integers with up to 19 places. In normal form, fractions are always reduced to lowest terms. Calculations that are not done in exact mode are done in decimal mode. Here, the fractions will tend to be projected to decimals at whatever level of precision is currently in effect.