Re: sanity check - floating point comparison
Read each line as text and compare them as text. No conversions. Much
better than doing possibly lossy conversions and then trying to guess
whether there was a big enough loss of information to be concerned about.
I think what you're alluding to is some getline approach?
Here's my approach in a nutshell.
# include <iostream>
# include <string>
# include <algorithm>
# include <iterator>
# include <vector>
# include <algorithm>
# include <fstream>
# include <numeric>
template<typename T>
void read_any_t(std::ifstream& i, const std::vector<T>& v ) {
size_t len = v.size();
i.read( reinterpret_cast<T*>( &v[ 0 ] ), streamsize ( len ) * sizeof
( T ) );
}
unsigned int DEBUG ( 0 );
int main(int argc, char* argv[])
{
std::vector< double > v1;
std::vector< double > v2;
v1.reserve ( 0x100000 ); // wild guess.. reserve enough to prevent
relocation
v2.reserve ( 0x100000 ); // wild guess.. reserve enough to prevent
relocation
std::string file1 ( "file1.txt" );
std::string file2 ( "file2.txt" );
std::ifstream f1 ( file1.c_str() );
std::ifstream f2 ( file2.c_str() );
if ( !f1 || !f2 )
return EXIT_FAILURE;
// only problem i have with this approach
// .. is determining how to check for failure .. if error here how
does one _know_?
std::copy(std::istream_iterator<double>(f1),
std::istream_iterator<double>(),
std::back_inserter(v1));
std::copy(std::istream_iterator<double>(f2),
std::istream_iterator<double>(),
std::back_inserter(v2));
// doesn't fly .. i suspect binary expected.
// read_any_t<double> ( f1, v1 );
// read_any_t<double> ( f2, v2 );
if ( DEBUG )
{
std::copy ( v1.begin(), v1.end(),
std::ostream_iterator< double > ( std::cout, "\n" ) );
std::cout << std::endl;
std::copy ( v2.begin(), v2.end(),
std::ostream_iterator< double > ( std::cout, "\n" ) );
}
typedef std::vector< double >::size_type size_type;
size_type const sz1 = v1.size();
size_type const sz2 = v2.size();
if ( sz1 == sz2 )
{
typedef std::vector< double >::const_iterator const_iter;
const_iter v1beg = v1.begin();
const_iter v1end = v1.end();
const_iter v2beg = v2.begin();
for ( ; v1beg != v1end; ++v1beg )
{
// use Jerry's isEqual
}
}
// finally can we replace that for loop with some algo of sorts..
// more on this later
}
I'm using Jerry's isEqual - which is not shown in the above but you get
the point