Niewidzialny Niewidzialny - 12 days ago 5
C# Question

How to compare thickness?

i want to compare

thickness
, by checking if
Thickness
A equals
thickness
B,

And.. it dont work. Always false, why?

ps.

Why
new Thickness(2.1) returns 2.09923289[..]
not
2.1

and
new Thickness(2.0)
returns clear
2.0
?

thickness

Answer

Double values are not safe to compare, because of how double are stored in memory. I would advise you to use something like if(Math.Abs(Thickness - new Thickness(2.1)) < TOLERANCE). You can do a quick test and try to check something like:

var passed = false;
if(0.2 + 0.1 == 0.3)
    passed = true;

And you'll see that it is false

Comments