[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: CLN
Hello,
George wrote:
> I'm too lazy to find the CLN list, so I'll ask here first. I'm stuck with
> the changeover because of two issues
Not every project has a list. ;-)
> 1) It's impossible with the defined interface (as I understand it) to figure
> out if a cl_R is actually an exact number, or if it is an integer in fact. I
> copied some things from internal CLN headers such as integerp and rationalp
> but I guess that's wrong to do that. What is the correct way to do this?
Since you insist on using cl_R I assume that you wish to handle
I==sqrt(-1) youself, somehow. That's fine. Then, how about this:
#include <cln/cln.h>
#include <iostream>
void checkme(ostream & os, cl_R x)
{
if (instanceof(x,cl_I_ring))
os << x << " seems to be an integer" << endl;
if (instanceof(x,cl_RA_ring))
os << x << " seems to be a rational" << endl;
}
int main()
{
cl_R n1(2);
cl_R gamma(".5772156649015328606065120900824024310421593359399235988057672348848677267776646709369470632917467495");
cl_R n2 = n1 / 3;
checkme(cout, n1);
checkme(cout, gamma);
checkme(cout, n2);
}
It will return:
2 seems to be an integer
2 seems to be a rational
2/3 seems to be a rational
Since you didn't see it in the manual: Are you sure you aren't using an
old version? instanceof() was introduced rather late, you should use at
least 1.0.2.
> 2) how do I get a native type out of a cl_R? I know something is an integer
> and that it will fit in an integer, but I can't get it. I will need to do
> the same for doubles.
On the above defined constants, do:
cout << cl_double_approx(n1) << endl;
cout << cl_double_approx(gamma) << endl;
cout << cl_double_approx(n2) << endl;
cout << cl_I_to_int(The(cl_I)(n1)) << endl;
Note that for cl_I_to_int you first have to check if it's an integer
because otherwise The()() will bomb out (as documented). If you want to
implement something along these lines in your own program, maybe you
should have a look at class GiNaC::numeric in file ginac/numeric.cpp from
the obvious package. It implements a wrapper around class cl_N suitable
for a CAS.
And later he wrote:
> Well a third issue, the current printing methods print to a C++ stream, so if
> I need a string I guess I might needs some weird stream hacks or does C++
> allow me to make a simple stream that is in fact a string?
#include <sstream> is what you really want to do. Unfortunately, it is
not available as of gcc-2.95.2 and doesn't seem to come into gcc-2.96.
You need to use libstdc++-v3 to get this functionality (I haven't tried
it though). Go to <http://sourceware.cygnus.com/libstdc++/>. It's in the
standard.
Regards
-rbk.
--
Richard Kreckel
<Richard.Kreckel@Uni-Mainz.DE>
<http://wwwthep.physik.uni-mainz.de/~kreckel/>
- Follow-Ups:
- Re: CLN
- From: George <jirka@5z.com>
- References:
- Re: CLN
- From: George <jirka@5z.com>