Comparing two floats or doubles to a precision

A

{AGUT2} {H}-IWIK

Hi,

Is it possible to compare / output two numbers to a certain precision?

for instance:

#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

main()
{

double one = 0.987432100
double two = 0.987123456

if ( one == two ) // [[ to a precision of 3 d.p.]]
{
std::cout << "YES! :D ";
} else
{
cout << "No! :mad: ";

}

Thanks,

Alex :)
 
K

Kevin Goodsell

{AGUT2} {H}-IWIK said:
Hi,

Is it possible to compare / output two numbers to a certain precision?

for instance:

#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

main()

int main()

There is no 'implicit int' rule in C++.
{

double one = 0.987432100
double two = 0.987123456

if ( one == two ) // [[ to a precision of 3 d.p.]]

You could write a function for it. But depending on exactly what you
want to do it could be tricky. In this case you could probably multiply
by 1000, convert to int, then compare. But this doesn't work well as a
general solution because 1) int (or long) may not be wide enough for the
result, and 2) the rounding method (truncation) may not be what you want.
{
std::cout << "YES! :D ";
} else
{
cout << "No! :mad: ";

std::cout

Also, you are missing:

}

And finally, you need to end your program's output with a newline:

std::cout << std::endl;
}

Thanks,

Alex :)

-Kevin
 
A

{AGUT2} {H}-IWIK

int main()
Also, you are missing:
}
And finally, you need to end your program's output with a newline:
std::cout << std::endl;

OK, I apologise PROFUSELY for my apalling syntax. :D I'm trying to jump in
headfirst as that's how (I find) I learn the fastest. Your tips are
appreciated.

Also, thank you for your idea. However, after lots (and lots and lots) of
Googling, I found the answer!

I have got an

if ( fabs(double1-double2) < 0.0000001 )
{
....
}

In my program and this seems to work wonderfully.

Thank you again for your time.

Alex

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
If you fancy a chat, #agut on quakenet :)

For all your UT2003 questions, visit the official UT2003 newsgroup FAQ at:
http://www.unrealtower.org/faq.php
 
M

Marcelo Pinto

{AGUT2} {H}-IWIK said:
I have got an

if ( fabs(double1-double2) < 0.0000001 )
{
...
}

In my program and this seems to work wonderfully.

Note however that this is not the general rule. Consider the problem
where you have the typical magnitude of values inferior to 0.0000001,
then all your values will seem equal according to that rule.

A more general solution would be to consider the magnitude of the
numbers involved in the comparison:


#include <iostream>

bool equals(double d1, double d2, double precision);
int main()
{
double a = 1, b = 1.0001;
if (equals(a, b, 0.00001))
std::cout << "equals" << std::endl;
else
std::cout << "differs" << std::endl;
return 0;
}

bool equals(double d1, double d2, double precision)
{
double eps1 = fabs(d1), eps2 = fabs(d2), eps;
eps = (eps1 > eps2) ? eps1 : eps2;
if (eps == 0.0)
return true; //both numbers are 0.0
//eps hold the minimum distance between the values
//that will be considered as the numbers are equal
//considering the magnitude of the numbers
eps *= precision;
return (fabs(d1 - d2) < eps);
}
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top