Postscript general question
What does the following mean in Postscript
/Total psi.subtotal Total add def\n
Here ps1.subtotal is a variable that I have populated elsewhere and is of datatype currency(eg: 10.00 USD)
Per my understanding the above line of code开发者_运维技巧 adds psi.subtotal and Total and then stores the result in Total. If so, Im seeing this PS to error out at the above line. I have also seen in some places that even for integer additions, the variables are first converted into strings and then the add or any other operations are carried out on the variable. Is that the case ?
Thanks
AFAIK there is no currency datatype, so I assume both psi.subtotal
and Total
are of the type real. The add
operation adds numbers of type integer or real. The PostScript Language Reference is written quite good and relatively easy to understand, with datatypes on page 48 and arithmetic operators on page 66 of the PDF.
"the above line of code adds psi.subtotal and Total and then stores the result in Total" is a pretty good summary of what the code should be doing.
PostScript has no currency type built in (see the PostScript Language Reference 3rd edition, section 3.3). It would not be too hard to create something like a currency type. One way to do this would be by defining a custom add operator.
A variable usually works by pushing an object (such as a number) onto the operand stack. The built-in add operator works only on numbers (ibid., page 527). If you try it with strings, for example, you will see a 'typecheck' error.
With the code you provide, however, there is no guarantee that psi.subtotal and Total are currency values (whatever currency might mean). There is no way to be know whether the code runs the standard 'add'.
It would help to know the details of the error, and how Total and psi.subtotal are defined before the code gets executed. And whether add has been re-defined.
The following code shows how your code could mean two different things.
(Using numbers for currencies...) =
/psi.subtotal 42.5 def
/Total 37 def
/Total psi.subtotal Total add def %%%%%%%%%%%%%%%%%% Your code.
(Total ) print Total ==
(Using strings for currencies...) =
/psi.subtotal (42.50) def
/Total (37.00) def
/standard_add { add } bind def
/currency_add { % stack: str str -- both string reps of numbers
cvr % stack: str num
exch % stack: num str
cvr % stack: num num
standard_add % stack: num
20 string % stack: num str
cvs % stack: str
}
def
/add { currency_add } def % Override existing add.
/Total psi.subtotal Total add def %%%%%%%%%%%%%%%%%% Your code.
(Total ) print Total =
flush
精彩评论