473,326 Members | 2,173 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,326 software developers and data experts.

Comparing two floats or doubles to a precision

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! >:( ";

}

Thanks,

Alex :)

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
Jul 19 '05 #1
4 17808
{AGUT2} {H}-IWIK wrote:
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! >:( ";
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
>> 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
Jul 19 '05 #3
{AGUT2} {H}-IWIK wrote:

if ( fabs(double1-double2) < 0.0000001 )


Yeah, I should have thought of that.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
{AGUT2} {H}-IWIK <al********************@ambtinternet.com> wrote in message news:<op**************@mercury.nildram.net>...
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);
}
Jul 19 '05 #5

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

Similar topics

1
by: dan | last post by:
Can someone tell me the difference between these three data types? Thanks Daniel
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
15
by: Michel Rouzic | last post by:
I tried making a function that would fill half of an array with random doubles that would go from -pi to +pi. Unfortunatly, all it ever returns is -pi. I haven't used an already existing random...
6
by: Mike P | last post by:
Why does this cause the error 'cannot implicitly convert type 'double' to 'float'? Can you not multiply doubles by floats without converting them all to the same datatype? fltPCRateStd =...
6
by: utab | last post by:
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;...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
6
by: Pavel | last post by:
Hello, Does anyone know a (preferably open-source) multi-platform C or C++ library that would be able to write and read C/C++ doubles and floats to/from streambuf, char array or similar device...
18
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.