Questions tagged [floating-point-comparison]
29 questions
1
vote
1 answer
I have a PriorityQueue of Objects and I need to sort it based on float values.How do I create a comparator for it?
I have already created a comparator for the priority queue.
class CompareBySalary implements Comparator{
@Override
public int compare(Employee e1,Employee e2){
return e1.salarye2.salary ? 1 : 0;
…

Royston Furtado
- 21
- 3
1
vote
1 answer
Minitest assert_equal failing when should be equal
I have a minitest test that is asserting if a decimal is the same. Wondering why this
My assert code:
assert_equal -9.04, elts[2].change.round(2)
Failure:
Failure:…

Andrew Cetinic
- 2,805
- 29
- 44
1
vote
3 answers
How to handle floating point comparisons in C++ mathematical expressions therefore rounding errors can be handled?
Let me explain little further. I am writing a code to calculate the amount of water in swimming pool after filling it for some time at the filling rate.
Input taken is length, width, depth in foot, time to fill the pool as timeToFillPool in seconds,…

SANG
- 31
- 1
1
vote
3 answers
How to compare multiple lists for numerical closeness two at a time?
Say I have 4 lists:
A = [1.1, 1.4, 2.1, 2.4]
B = [1.3, 6.5, -1.0, 2.3]
C = [0.5, -1.0, -1.1, 2.0]
D = [1.5, 6.3, 2.2, 3.0]
How do I 1)compare the lists eg A,B B,C C,D A,C so on and 2)return true if the elements are +/-0.2 ?
Example output: (Or…

Steve. C
- 15
- 3
1
vote
1 answer
Comparing doubles properly using aliasing with integer representations and ULPs
I have tried to avoid epsilon comparisons for comparing floating point types. The best solution I could come up with used the difference in ULPs (Unit in the last place), though this article had a much better solution using integer representations…

Nikita
- 609
- 2
- 7
- 18
0
votes
1 answer
Issue in floating point comparison leads to incorrect query result in DolphinDB GUI
My environment: DolphinDB server 2.00.8 on Linux
I used a where condition (which compares floating point numbers) to filter table data, but the query result is incorrect.
Here’s what I did:
I have a table containing the result of my factor analysis…

Polly
- 603
- 3
- 13
0
votes
1 answer
How to tell rust to do all float comparisons using a given lib by default?
I would like to have all float comparisons done with float_cmp::approx_eq (for example), but keep using the equality comparison operator == for it. How do I achieve this?
impl PartialEq for f32 {
fn eq(&self, other: &Self) -> bool {
…

Sparkler
- 2,581
- 1
- 22
- 41
0
votes
0 answers
mips comparison
I'm having an error with my program. I'm not asking someone to necessarily rewrite my program for me, but constructive advice would be appreciated.
#print Guess
li $v0, 4
la $a0, Guess
syscall
#get user input
li $v0, 6
syscall
#store it to…

Anon
- 31
- 4
0
votes
2 answers
Floating-point arithemtic in C: epsilon comparison
I'm trying to compare values with double precision using epsilon. However, I have a problem - initially I have thought that the difference should be equal to the epsilon, but it isn't. Additionally, when I've tried to check the binary representation…

Funny
- 193
- 8
0
votes
1 answer
Floating point operations results are different in Android, TensorFlow and Pytorch
I am trying to compare the floating point numbers on Android, Tensorflow and Pytorch. What I have observed is I am getting same result for Tensorflow and Android but different on Pytorch as Android and Tensorflow are performing round-down operation.…

Mustansar Saeed
- 2,730
- 2
- 22
- 46
0
votes
1 answer
Can we use !islessgreater(float a, float b) to check a==b given both a and b are not nan
To check whether two floating-point variables are equal, we cannot use something like a==b. But how about using the islessgreater() function from header file?
From C++11, there are three overloads as below
bool islessgreater (float x , float…

James Liu
- 11
- 2
0
votes
1 answer
Does multiplying a floating point number by 0 always yield 0?
Floating point numbers are tricky, since many natural arithmetic properties don't hold.
I SUPPOSE that this particular property holds nevertheless, but I prefer to ask rather than be hit by hard to detect errors.
Assume that d is an arbitrary…
user4385532
-2
votes
1 answer
In Perl, why does 100*18.35 not compare equal to 1835?
When I run the following Perl one-liner:
$ perl -e 'print "Oh no!\n" unless 1835 == 100*18.35'
I get
Oh no!
Why is that?

Tom
- 4,666
- 2
- 29
- 48
-2
votes
1 answer
Difference between ways to compare floating-point numbers
There seems to be many approaches to judge whether two floating-point numbers are identical. Here are some examples I've found:
fabs(x - y) < n * FLT_EPSILON * fabs(x) OR fabs(x - y) < n * FLT_EPSILON * fabs(y)
fabs(x - y) < n * FLT_EPSILON *…

nalzok
- 14,965
- 21
- 72
- 139