473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Comparing the values of two vectors

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.

Apr 6 '06 #1
6 11517
utab wrote:
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.
Apr 6 '06 #2
In message <11**********************@e56g2000cwe.googlegroups .com>, utab
<um********@gmail.com> writes
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.
--
Richard Herring
Apr 6 '06 #3

Richard Herring wrote:
In message <11**********************@e56g2000cwe.googlegroups .com>, utab
<um********@gmail.com> writes
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;

if (a==b) //...


But bear this in mind
http://www.parashift.com/c++-faq-lit...html#faq-29.17

operator== is defined for std::vector, but it is not necessarily a good
way to compare floating point numbers.

Gavin Deane

Apr 6 '06 #4
In message <11**********************@z34g2000cwc.googlegroups .com>,
Gavin Deane <de*********@hotmail.com> writes

Richard Herring wrote:
In message <11**********************@e56g2000cwe.googlegroups .com>, utab
<um********@gmail.com> writes
>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;

if (a==b) //...


But bear this in mind
http://www.parashift.com/c++-faq-lit...html#faq-29.17

operator== is defined for std::vector, but it is not necessarily a good
way to compare floating point numbers.


True. OTOH it's often exactly what's required. We can't tell without
knowing more about the OP's requirements..

--
Richard Herring
Apr 6 '06 #5
"Richard Herring" wrote, 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)
}
--
Robbie Hatley
Tustin, CA, USA
lone wolf intj at pac bell dot net
home dot pac bell dot net slant earnur slant
Apr 6 '06 #6
In message <kl*******************@newssvr12.news.prodigy.com> , Robbie
Hatley <bo*********@no.spam.com> writes
"Richard Herring" wrote, 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.
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?


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.
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)
}


--
Richard Herring
Apr 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jorgen Gustafsson | last post by:
Hi, im trying to write a small progam to compare data in 2 textfiles. I want to search for values that doesnt exist in File2. The result should be "3" in the example below but Im not able to do...
2
by: darrel | last post by:
I have two comma delimted strings that I need to compare individual values between the two. I assume the solution is likely to put them into an array? If so, do I need to loop through one,...
11
by: John Salerno | last post by:
I'd like to compare the values in two different sets to test if any of the positions in either set share the same value (e.g., if the third element of each set is an 'a', then the test fails). I...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
1
by: Adrienne Boswell | last post by:
I have a form which I am letting the user enter more than one event at a time. I need to check whether one date is less than another. If the display to date is earlier than the event date, then...
0
by: John Wright | last post by:
I have a datagrid that contains a list of decimal values. I want to compare the values of one datatable to the other datatable and update my data grid. For example my first datatable has the...
5
by: SneakyElf | last post by:
I need to write a function where the third parameter points to an address whose dereferenced value equals one of the array element's value, then the the function returns the index of the first...
2
by: sumuka | last post by:
Hello Everyone, I'm doing a project in VB 6.0 and i've a flexgrid which contains some numerical values in it's cell now i need to compare these values present in flexgrid with the values in...
17
by: fgh.vbn.rty | last post by:
I know that std::vector<boolis specialized to store individual bools as single bits. I have a question about the comparison operation on a pair of vector<bool>s. Is the comparison done bit by...
9
Thekid
by: Thekid | last post by:
Hi, I can't seem to figure this out. Here's my objective: I have a value that is an md5 hash and I have a wordlist. I need to md5 the words in the list, then compare them to the given hash, then have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.