Math in PostScript

5.7 Math in PostScript

The PostScript interpreter can be used as a basic calculator. There are some limits here, however, since it only works with 5 digits of acurracy. After all, how accurate do you need to be when describing coordinates on a page?

There are actually two types of numbers in PostScript, integers and reals. Integers are signed and reals are expressed in the usual mantissa E exponent fashion, such as 2.34E32. Type conversions are usually done for you; you divide and integer by a real with the same command that divides a real by a real. This shouldn't ever cause you any difficulty.

You should probably know something about PostScript's trigonometric functions. PostScript has only three trig functions: atan, cos, and sin. These are sufficient to generate any other trig function, however. For example, you could find the arcsine of 1/2 as in the following ghostscript session

GS>.5
GS<1>dup
GS<2>2
GS<3>exp
GS<2>1
GS<3>sub
GS<2>neg
GS<2>sqrt
GS<2>atan
GS<1>=
30.0
Notice that the answer is in degrees. This is true for all trig functions: they expect arguments in degrees and they express angles in degrees.

Next


David Maxwell, who is still writing this, would like to hear your comments and suggestions. And remember, parts of this manual are based on P.J. Weingartner's work: A First Guide to PostScript.