Comparing the values of two vectors

U

utab

Hi there,

I would like to compare the values in two vectors of double. I can do
it by using iterators, I had an idea but wondering there is a library
facility to do that.

vector<double> a;
vector<double> b;

and that is enough for me to check the first three or four values of
these two vectors. Maybe in the future I will need something from STL
if existent :)) .

Thanks in advance.
 
R

Rolf Magnus

utab said:
Hi there,

I would like to compare the values in two vectors of double. I can do
it by using iterators, I had an idea but wondering there is a library
facility to do that.

Depends on what exactly you mean by "compare the values in two vectors". Do
you want to compare each element of a with the corresponding element of b?
vector<double> a;
vector<double> b;

and that is enough for me to check the first three or four values of
these two vectors. Maybe in the future I will need something from STL
if existent :)) .

You could use std::equal, which returns true if both sequences are equal or
false otherwise.
 
R

Richard Herring

utab said:
Hi there,

I would like to compare the values in two vectors of double. I can do
it by using iterators, I had an idea but wondering there is a library
facility to do that.

vector<double> a;
vector<double> b;

Doesn't your library documentation describe the std::vector comparison
operators?

and that is enough for me to check the first three or four values of
these two vectors. Maybe in the future I will need something from STL
if existent :)) .

Thanks in advance.

if (a==b) //...
if (a<b) // ...
etc.
 
R

Robbie Hatley

, regarding equality-checking doubles:
True [that it's a bad idea]. OTOH it's often exactly what's
required.

I've been burned by that before. Just what does "equal" mean
with doubles? "Equal" to +- 10^-5? "Equal" to +- 10^-10?
Often two variables that should be "equal" will compare
as inequal due to tiny round-off errors in the ninth or tenth
decimal places.

Idea:

#include <cmath>
bool MostlyEqual(double A, double B, int Precision)
{
if (Precision< 1) Precision= 1;
if (Precision>10) Precision=10;
double Max = A;
if (B > A) {Max = B;}
double Divisor = pow(10.0, (double)Precision);
double Difference = A - B;
if (B > A) {Difference = B - A;}
return (Difference < Max / Divisor)
}
 
R

Richard Herring

Robbie said:
, regarding equality-checking doubles:
True [that it's a bad idea].

No. True that it's a bad idea to do it without understanding how
floating-point works.
I've been burned by that before. Just what does "equal" mean
with doubles? "Equal" to +- 10^-5? "Equal" to +- 10^-10?

Equal as in "==". If that isn't what you mean, don't call it "equal",
and write a function that expresses what you _do_ mean, as you have done
below.
Often two variables that should be "equal" will compare
as inequal due to tiny round-off errors in the ninth or tenth
decimal places.

Yes, when they are the result of arbitrary computation. No, when they
result from some restricted set of operations. For example, you can
store quite large integers *exactly* in doubles, and expect to get exact
integer results from adding and subtracting them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top