I have tried:
$point = Formula('2x')>eval(x=>5);
This results in (10) (including the brackets. How can I get the
value just as a number without the brackets  (so that it can be used
in a num_cmp)?
You need to update your copy of the Parser. This behavior
was changed about 10 months ago. You can update just the Parser and
Value packages that are in pg/lib without updating anything else if you
wish.
The reason that the Parser objects put parentheses around themselves
during string concatenation operations is so that you can build more
complicated formulas from simpler parts. For example:
$f = Formula("2x+1"); $g = Formula("x/$f");
This will make $g equal to the expected x/(2x+1) rather than (x/2)*x+1 ,
which is what the result would be if objects were concatenated without
parentheses (insertion into a string is handle by Perl using string
concatenation). When $f is being inserted into a string, there is no
way for it to know if that string is going to be used as a Formula or
other computation, or just for display purposes, like in
BEGIN_TEXT/END_TEXT. Since you usually want the TeX version of $f
rather than its string version for BEGIN_TEXT/END_TEXT anyway, the
default of using parentheses seemed best, since it is much more obvious
that you have parentheses when you don't want them than that they are
missing when you do.
It turned out, however, that often the parentheses were not needed, and
people were confused about the parens in situations like yours, and so
10 months ago, I changed the defaults for most objects so that they
would not insert parentheses unless it was likely that they would be
needed. So, for example, Reals don't insert parentheses any more, but
complexes do, since the plus in 1+3i has such low precedence. (The one
bad effect of this is that when 5 is inserted, the lack of parens can
be a problem. For example, $a = Real(5); $f = Formula("$a^2"); would produce 10 not 10. But this is true of $a = 5; as well)
In terms of using parser objects within BEGIN_TEXT/END_TEXT, I
mentioned that you usually want the objects to produce their TeX form
rather than their string form (for something like a Real, that is not
much different, but if the real were something like 2.4E10, then it
would make a difference). To make this easier, the Parser has a Context
flag that says how Parser objects should stringify themselves. Using Context()>texStrings; just before BEGIN_TEXT and Context()>normalStrings; just after will make it easier to obtain the desired output.
Given a Parser object, $f (not necessarily a formula, but any Parser object), you can get its TeX form from $f>TeX , and its string form as $f>string . These can be used within BEGIN_TEXT/END_TEXT by using the command escapes \{...\}. For example:
$f=Formula("sin(x/(x+1))"); BEGIN_TEXT \(f(x) = \{$f>TeX\}\) END_TEXT
would get you properly formatted TeX output. It may be easier to use
$f=Formula("sin(x/(x+1))"); Context()>texStrings; BEGIN_TEXT \(f(x) = $f\) END_TEXT Context()>normalStrings;
however, especially if there are a number of objects to be inserted.
In any case, updating your copy of the Parser and Value packages should make this work more like what you would expect.
Davide
< Post or View Comments >
