# Extracting a numeric from an ex

Eduardo M Kalinowski ekalin at iname.com
Fri Dec 8 01:00:07 CET 2000

```Christian Bauer wrote:

> Hi!
>
> On Wed, Dec 06, 2000 at 02:39:29PM -0200, Eduardo M Kalinowski wrote:
> >     I would find it extremely useful if it was possible to get a numeric
> > representing the ex's contents
>
> Given an 'ex e':
>
>   numeric n;
>   if (is_ex_of_type(e, numeric))
>     n = ex_to_numeric(e);

Since this is not documented in the tutorial, and is difficult to be found in
the reference, I've made some little changes to the tutorial to include
description for that (and related functions.) Feel free to use, change, ignore,
etc. the patch, Richard.

--
I tell ya, I was an ugly kid.  I was so ugly that my dad kept the kid's
picture that came with the wallet he bought.
-- Rodney Dangerfield

Eduardo M Kalinowski     (ekalin at iname.com)
http://move.to/hpkb      http://cami-ufpr.hpg.com.br

-------------- next part --------------
--- ginac.texi.old	Thu Dec  7 21:37:01 2000
+++ ginac.texi	Thu Dec  7 21:57:19 2000
@@ -1298,10 +1298,13 @@
@c    node-name, next, previous, up

@subsection Checking expression types
@cindex @code{is_ex_of_type()}
+ at cindex @code{ex_to_numeric()}
+ at cindex @code{ex_to_...}
+ at cindex @code{Converting ex to other classes}
@cindex @code{info()}

Sometimes it's useful to check whether a given expression is a plain number,
a sum, a polynomial with integer coefficients, or of some other specific type.
GiNaC provides two functions for this (the first one is actually a macro):
@@ -1325,10 +1328,24 @@
is_ex_of_type(e2, numeric);  // false
is_ex_of_type(e1, mul);      // false
is_ex_of_type(e2, mul);      // false
+@}
+ at end example
+
+When the test made by @code{is_ex_of_type()} returns true, it is safe to
+call one of the functions @code{ex_to_...}, where @code{...} is one of
+the class names (@xref{The Class Hierarchy}, for a list of all
+classes). For example:
+
+ at example
+@{
+    ...
+    if (is_ex_of_type(e, numeric))
+        GiNaC::numeric n = ex_to_numeric(e);
+    ...
@}
@end example

The @code{info()} method is used for checking certain attributes of
expressions. The possible values for the @code{flag} argument are defined
```