Valarray Problem

Marc A. Lepage mlepage@molecularmining.com
Sat Apr 1 00:00:00 GMT 2000


I'm trying to compute the distance between two valarrays. This simple
application of Pythagoras (which I believe to be correct) compiles:

double
euclideanDistance(const std::valarray<double>& kvfFrom,
    const std::valarray<double>& kvfTo)
{
    std::valarray<double> vfDiff = kvfFrom - kvfTo;

    std::valarray<double> vfSquare = pow(vfDiff, 2.0);

    double fSum = vfSquare.sum();

    return sqrt(fSum);
}

I'm trying to collapse it to this, which I believe should be equivalent:

double
euclideanDistance(const std::valarray<double>& kvfFrom,
    const std::valarray<double>& kvfTo)
{
    return sqrt(pow(kvfFrom - kvfTo, 2.0).sum());
}

The compiler (GCC 2.95.2) chokes when taking the sum of the square
valarray. So even this does not work:

    pow(vfDiff, 2.0).sum();

The relevant compiler spew is:

c++ -g -DDEBUG -ansi -pedantic -Wall -Wtraditional -Wid-clash-8
-Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion
-Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations
-Wnested-externs -Woverloaded-virtual -Werror -pipe -I../.. -DLINUX
-DMMC_NO_IOS_BASE -o ../../mmc/proj/file.o -c ../../mmc/proj/file.cpp
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/std/valarray_meta.h:
In method `double
_Expr<_BinFunClos<_ValArray,_Constant,double,double>,double>::sum()
const':
../../mmc/proj/file.cpp:83:   instantiated from here
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/std/valarray_meta.h:721:
invalid operands `double ()(double (*)())' and `double' to binary
`operator +'
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/std/valarray_meta.h:721:  
in evaluation of `operator +=(double ()(double (*)()), double)'
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/std/valarray_meta.h:722:
return to `double' from `double (*)(double (*)())'

I'm wondering if this is a bug in the 2.95.2 valarray, and if so, if it
exists in the libstdc++-v3 valarray?

-- 
Marc A. Lepage
Software Developer
Molecular Mining Corporation
http://www.molecularmining.com/


More information about the Libstdc++ mailing list