Skip to content

Can I compare two float vectors with an expected accuracy? #814

Answered by cdrnet
jarenduan asked this question in Q&A
Discussion options

You must be logged in to vote

You can use AlmostEqual for that effect:

var v1 = DenseVector.OfArray(new[] {100000.00001, 200000.000001 });
var v2 = DenseVector.OfArray(new[] {100000.0, 200000.0 });

v1.Equals(v2); // false
v1.AlmostEqual(v2, 0.001d); // true (delta)
v1.AlmostEqual(v2, 0.000001d); // false (delta)
v1.AlmostEqual(v2, 3); // true (decimal places)
v1.AlmostEqual(v2, 7); // false (decimal places)
v1.AlmostEqualRelative(v2, 0.001d); // true (delta)
v1.AlmostEqualRelative(v2, 0.000001d); // true (delta)
v1.AlmostEqualRelative(v2, 0.00000000001d); // false (delta)
v1.AlmostEqualRelative(v2, 3); // true (decimal places)
v1.AlmostEqualRelative(v2, 7); // true (decimal places)
v1.AlmostEqualRelative(v2, 10); // …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by cdrnet
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #551 on July 24, 2021 07:22.