Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-07-06 09:36:32


I approve of the functionality, but am a little worried about the casts in
the implementation. Wouldn't it be better and simpler to write something
like:

if (PyInt_Check(p))
    return std::complex<T>(PyInt_AS_LONG(p));
if (PyLong_Check(p))
    return static_cast<T>(PyLong_AsLong(p));
...

And furthermore, is there not a python routine for converting a Python Long
(infinite precision) to a double? That would be less likely to snip off the
most significant digits. Hmm, I'm also concerned about the lack of error
checks for loss of information. The other numeric conversions try to make
sure that the most significant digits aren't lost (e.g. when converting from
a Python Long to an integer) IIRC.

-Dave

----- Original Message -----
From: <Peter.Bienstman_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, July 06, 2001 9:50 AM
Subject: [boost] python patch: convert int/double to complex

> Hi,
>
> This simple patch adds automatic type conversions from Python ints,
> longs and doubles to C++ complex numbers.
>
> Useful for me, but useful enough to be included in a future release?
>
>
> *** boost_orig/boost_1_22_0/boost/python/conversions.hpp Mon
> May 28 17:11:56
> 2001
> --- boost_1_22_0/boost/python/conversions.hpp Fri Jul 6 15:22:18
> 2001
> ***************
> *** 93,98 ****
> --- 93,105 ----
> template <class T>
> std::complex<T> complex_from_python(PyObject* p,
> boost::python::type<T>)
> {
> + if (PyInt_Check(p))
> + return static_cast<T>(PyInt_AS_LONG(p));
> + if (PyLong_Check(p))
> + return static_cast<T>(PyLong_AsLong(p));
> + if (PyFloat_Check(p))
> + return static_cast<T>(PyFloat_AS_DOUBLE(p));
> +
> expect_complex(p);
>
> return std::complex<T>(
>
>
> -------------------------------------
> Peter Bienstman
> Department of Information Technology
> INTEC/IMEC - Ghent University
> St.-Pietersnieuwstraat 41
> B-9000 Gent - Belgium
> E-mail: Peter.Bienstman_at_[hidden]
> Tel: +32 9 264 3445
> Fax: +32 9 264 3593
> -------------------------------------
>
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk