Linked Questions
221 questions linked to/from How do you compare float and double while accounting for precision loss?
4
votes
4
answers
3k
views
Why two float type variables have different values [duplicate]
I have two integer vectors of nearly 1000 size, and what I am going to do is to check whether the sum of the square integer for these two vectors is the same or not. So I write the following codes:
...
8
votes
1
answer
10k
views
Modern practice to compare double/float for equality in modern C++ [duplicate]
if (std::abs(double1 - double2) < std::numeric_limits<double>::epsilon())
std::cout<<"Equal";
else
std::cout<<"Not equal";
Is this code with modern C++11/...
0
votes
8
answers
341
views
Why the control goes in "else" part? [duplicate]
Possible Duplicate:
Most effective way for float and double comparison
strange output in comparison of float with float literal
int main()
{
float a = 0.8;
if (a == 0.8)
printf("x\n");
...
0
votes
5
answers
4k
views
How to set precision for double data type variables in C? [duplicate]
Possible Duplicate:
Most effective way for float and double comparison
In my program some double variables take values of the form 1.00000001. Equality check of these variables with 1 obviously ...
5
votes
5
answers
550
views
Two General CS Questions [duplicate]
When comparing two "real" numbers for equality, why should I not use the == operator, and what should I use instead?
What is the difference between coersion and casting? It is my general assumption ...
0
votes
4
answers
2k
views
Can float values add to a sum of zero? [duplicate]
Possible Duplicate:
Most effective way for float and double comparison
I have two values(floats) I am attempting to add together and average. The issue I have is that occasionally these values ...
0
votes
3
answers
784
views
C++ Floating point gotcha [duplicate]
Possible Duplicate:
Most effective way for float and double comparison
I am new to C++. I had a doubt, while reading C++.
How to decide two floating point numbers equal to each other or not ?
...
3
votes
2
answers
2k
views
Comparing two floats/doubles in c++ [duplicate]
I read that floats/doubles equality should be implemented using an interval (given by some epsilon):
bool aresame(double a, double b)
{
return (fabs(a-b) < EPSILON);
}
Is their any predefined ...
-1
votes
3
answers
394
views
How to compare float or double? [duplicate]
Is there built-in library can compare float or double
I do not think compare like a == b or a !=b makes any sense. Any suggestion?
0
votes
2
answers
1k
views
Ideal way to test for practical floating point equality in c# [duplicate]
So I've understood for a long time that floating point equality is not perfect in any programing language. But until recently after uncovering a bug at work related to this issue, I never realized ...
1
vote
2
answers
833
views
comparing float variable [duplicate]
Possible Duplicate:
Most effective way for float and double comparison
How dangerous is it to compare floating point values?
I have const float M = 0.000001; and float input;. I want to not ...
-4
votes
5
answers
785
views
Comparing Floating Point Numbers [duplicate]
Please before you think that I'm asking the same N% Question read it first and please pay Attention to it.
I'm working on a project where I have more functions which returns double and it may be ...
Michi's user avatar
- 5,327
-5
votes
6
answers
394
views
double or float comparison [duplicate]
I've seen posts like:
What is the most effective way for float and double comparison?
Compare two floats
And many other related posts.
I saw in d3js library, it uses the following comparison:
...
0
votes
1
answer
913
views
C++ - double type alternative for subtraction of decimals? [duplicate]
I'm writing a function which accepts a double data type, say variable 't', and if 't' is equal to 0.05, the function does something.
Now, my problem is that if 't' is 100-99.5, the function fails to ...
-2
votes
1
answer
149
views
How to compare floating point variables in C++? [duplicate]
This post stated that you shouldn't compare floating point variables with == because of rounding errors. What should I use then, and when?
-2
votes
1
answer
277
views
What should I do when relative comparison of two equal doubles doesn't work? [duplicate]
I use relative comparison as it described here.
I have two doubles. The first of them is result of computation:
double d1(callComputation() );
The second is defined as following:
double d2(0.009);
...
0
votes
1
answer
112
views
What operation can I apply to float to ensure the same outcome up to a certain precision? [duplicate]
I have two floating points, x, y. They are "supposed to be" the same, but with floating-point, you never know, so x == y may return False.
So I am looking for an operation O() which ensures ...
0
votes
1
answer
83
views
Accurate comparison of two variables of type float in C++ [duplicate]
What can I do to compare two variables of type float accurately in C++, one being initialized at the time of declaration and the other being computed through the program?
When I am comparing two ...
1
vote
0
answers
80
views
linspace c++ vs matlab floating point precsion [duplicate]
I'm trying to implement Linspace in c++
I created some random tests and have a problem with precision I don't understand why.
c++ implementation
std::vector<double> libcalc::linear_space(...
Gilad's user avatar
- 6,625
0
votes
0
answers
46
views
Deal with double addition/subtraction errors in C [duplicate]
I have the following code which minus 0.1 from 0.5 for 5 times, but the result isn't exact 0.0.
I understand why there is an error, and I'm finding a way to deal with it.
The only way I came up with ...
Pegasis's user avatar
- 1,444
0
votes
0
answers
38
views
float comparsion (operator !=) returns false, but numbers are equal although the numbers appear to be the same [duplicate]
I wrote a small test to evaluate my programm, but somehow a weired thing happens:
Values of the vector(as printed using 'cout' in the first loop):
0.06
0.06
0.06
0.06
0.06
0.6
...
226
votes
12
answers
90k
views
Compare double to zero using epsilon
Today, I was looking through some C++ code (written by somebody else) and found this section:
double someValue = ...
if (someValue < std::numeric_limits<double>::epsilon() &&
...
133
votes
13
answers
160k
views
How should I do floating point comparison?
I'm currently writing some code where I have something along the lines of:
double a = SomeCalculation1();
double b = SomeCalculation2();
if (a < b)
DoSomething2();
else if (a > b)
...
63
votes
10
answers
67k
views
Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to
http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx
If you create a custom algorithm that
determines whether two floating-point
numbers can be considered equal, you
must use a ...
ss2k's user avatar
- 1,288
93
votes
9
answers
193k
views
Comparing floating point number to zero
The C++ FAQ lite "[29.17] Why doesn't my floating-point comparison work?" recommends this equality test:
#include <cmath> /* for std::abs(double) */
inline bool isEqual(double x, ...
106
votes
4
answers
72k
views
When do I use fabs and when is it sufficient to use std::abs?
I assume that abs and fabs are behaving different when using math.h. But when I use just cmath and std::abs, do I have to use std::fabs or fabs? Or isn't this defined?
math's user avatar
- 8,870
66
votes
5
answers
16k
views
Floating point comparison [duplicate]
int main()
{
float a = 0.7;
float b = 0.5;
if (a < 0.7)
{
if (b < 0.5) printf("2 are right");
else printf("1 is right");
}
else printf("0 are right")...
51
votes
7
answers
13k
views
How to alter a float by its smallest increment (or close to it)?
I have a double value f and would like a way to nudge it very slightly larger (or smaller) to get a new value that will be as close as possible to the original but still strictly greater than (or less ...
Owen's user avatar
- 7,874
43
votes
8
answers
78k
views
How to correctly and standardly compare floats?
Every time I start a new project and when I need to compare some float or double variables I write the code like this one:
if (fabs(prev.min[i] - cur->min[i]) < 0.000001 &&
fabs(...
32
votes
7
answers
44k
views
What's the best way to compare Double and Int?
The following code in C# doesn't work:
int iValue = 0;
double dValue = 0.0;
bool isEqual = iValue.Equals(dValue);
So, the question: what's the best way to compare Double and Int?