I was very surprised by the output of the following program:
#include <iostream>
#include <numeric>
int main() {
double vals_1[5]= { 0.5, 0.2, 0.1, 0.1, 0.1 };
double vals_2[5]= { 0.1, 0.1, 0.1, 0.2, 0.5 };
double sum1 = std::accumulate( vals_1, vals_1+5, 0. );
double sum2 = std::accumulate( vals_2, vals_2+5, 0. );
if( sum1 != 1. )
std::cout << "sum1 not ok ";
else
std::cout << "sum1 ok ";
if( sum2 != 1. )
std::cout << "sum2 not ok";
else
std::cout << "sum2 ok";
std::cout << std::endl;
}
the output is: sum1 not ok sum2 ok
Is that behavior to be expected? Do I have to use a function that
compares up to some precision everytime
I want to compare 2 doubles? I tried changing double to float: in that
case both sums are ok. 3 7860
nicolas wrote: I was very surprised by the output of the following program:
#include <iostream> #include <numeric>
int main() {
double vals_1[5]= { 0.5, 0.2, 0.1, 0.1, 0.1 }; double vals_2[5]= { 0.1, 0.1, 0.1, 0.2, 0.5 }; double sum1 = std::accumulate( vals_1, vals_1+5, 0. ); double sum2 = std::accumulate( vals_2, vals_2+5, 0. ); if( sum1 != 1. ) std::cout << "sum1 not ok "; else std::cout << "sum1 ok "; if( sum2 != 1. ) std::cout << "sum2 not ok"; else std::cout << "sum2 ok";
std::cout << std::endl; }
the output is: sum1 not ok sum2 ok
Is that behavior to be expected? Do I have to use a function that compares up to some precision everytime I want to compare 2 doubles? I tried changing double to float: in that case both sums are ok.
See: http://www.eskimo.com/~scs/C-faq/q14.5.html
(It's from the C-FAQ, but is the same for C++.)
HTH,
--ag
--
Artie Gold -- Austin, Texas
I think the prolem lies with the fact that .1 and .2 cannot be expressed in
binary...
"nicolas" <ni*******@yahoo.com> wrote in message
news:bk**********@news.Stanford.EDU... I was very surprised by the output of the following program:
#include <iostream> #include <numeric>
int main() {
double vals_1[5]= { 0.5, 0.2, 0.1, 0.1, 0.1 }; double vals_2[5]= { 0.1, 0.1, 0.1, 0.2, 0.5 }; double sum1 = std::accumulate( vals_1, vals_1+5, 0. ); double sum2 = std::accumulate( vals_2, vals_2+5, 0. ); if( sum1 != 1. ) std::cout << "sum1 not ok "; else std::cout << "sum1 ok "; if( sum2 != 1. ) std::cout << "sum2 not ok"; else std::cout << "sum2 ok";
std::cout << std::endl; }
the output is: sum1 not ok sum2 ok
Is that behavior to be expected? Do I have to use a function that compares up to some precision everytime I want to compare 2 doubles? I tried changing double to float: in that case both sums are ok.
Floating point addition is not commutative, hence your
result. Never count on comparing floating point values
directly (via "=="), instead try comparing their difference
with a threshold value, via "<" and/or ">".
HTH,
- J. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;...
|
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)...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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 ...
| |