possible bug with the stl inner_product() function
Graham M. Seed
G.M.Seed@hw.ac.uk
Tue Oct 31 08:33:00 GMT 2000
Dear Sir/Madam
The following program illustrates uses of the
inner_product() function of the c++ STL. I'm using version
2.91.66 gcc/g++ compiler running at Red Hat Linux 6.2.
The default operators for the inner_product() function are
*,+ which gives a final answer of 70 as expected. However,
when we specify the contrary case of +,* operators then I'd
expect the result to be 5760 but it is still 70??
If this isn't a bug then it certainly doesn't agree with
the mathematical interpretation of the inner product.
//...
void main ()
{
int array1[] = { 1, 2, 3, 4 };
int array2[] = { 5, 6, 7, 8 };
ostream_iterator<int> out (cout, " ") ;
cout << "array1: " ;
copy (array1, array1+4, out) ;
cout << endl ;
cout << "array2: " ;
copy (array2, array2+4, out) ;
cout << endl ;
int ip1 = inner_product (array1, array1+4, array2, 0) ;
cout << "ip1: " << ip1 << endl ; // O/P 70 as expected
int ip2 = inner_product (array1, array1+4, array2, 0, plus<int>(), multiplies<int>()) ;
cout << "ip2: " << ip2 << endl ; // O/P still 70 when
should be 5760?
}
Wishes
Graham
--
Dr. Graham M. Seed
Dept. of Mech. & Chem. Eng.
Heriot-Watt University
Riccarton,
Edinburgh, EH14 4AS
Scotland, UK
T: +44(0)131 449 5111; 4377
F: +44(0)131 451 3129
E: g.m.seed@hw.ac.uk
U: http://www.hw.ac.uk/mecWWW/research/staff/gms.htm
More information about the Gcc-bugs
mailing list