What's the best way to compare doubles? I am testing a method that should
return the fraction 2/9. For my expected result, I entered 0.222222 (a six
decimal place representation of 2/9). The CLR resolved the fraction to full
precision, so I got this error back from my test:
expected:<0.222 222>
but was:<0.22222222 2222222>
For my purposes, the two values are equal.
The Double.Epsilon constant is too small to use for an epsilon comparison
here. So, is my only option to create my own epsilon constant as 0.00001? Or
is there another solution to this problem that I am missing? Thanks in
advance
David Veeneman
Foresight Systems 1 2058
Test whether they are equal to within 0.001 or whatever you consider the
tolerance to be. That's the only way.
Remember that they could differ in either direction, so test the absolute
value of the difference.
"David Veeneman" <da****@nospam. com> wrote in message
news:OL******** ******@TK2MSFTN GP14.phx.gbl... What's the best way to compare doubles? I am testing a method that should return the fraction 2/9. For my expected result, I entered 0.222222 (a six decimal place representation of 2/9). The CLR resolved the fraction to full precision, so I got this error back from my test:
expected:<0.222 222> but was:<0.22222222 2222222>
For my purposes, the two values are equal.
The Double.Epsilon constant is too small to use for an epsilon comparison here. So, is my only option to create my own epsilon constant as 0.00001? Or is there another solution to this problem that I am missing? Thanks in advance
David Veeneman Foresight Systems This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: nicolas |
last post by:
I was very surprised by the output of the following program:
#include <iostream>
#include <numeric>
int main() {
double vals_1= { 0.5, 0.2, 0.1, 0.1, 0.1 };
double vals_2= { 0.1, 0.1, 0.1, 0.2, 0.5 };
double sum1 = std::accumulate( vals_1, vals_1+5, 0. );
|
by: Matt Billock |
last post by:
Hello everyone,
I am having some issues with what should be an insanely simple problem.
Basically, I'm working on an implementation of the TSP algorithm (I have a
static set of 5 locations, so I'm just brute forcing my way through it), and
during execution I build a list of 120 potential distances to be travelled,
stored in an array of type double. I then have another function which
traverses this array and locates the position of the...
|
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;
vector<double> b;
and that is enough for me to check the first three or four values of
|
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) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a)))
Do the same issues involved in comparing 2 fp types for equality
apply to comparing a float to zero? E.g. is if(x == 0.0)
considered harmful?
|
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 fails if comparing doubles of different
magnitude since it's hard or not possible to find a suitable threshold
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |