I recently came across the SameValue
function to compare floating-point numbers. I noticed that we can optionally specify an epsilon value for this function.
From the documentation, I understand that Delphi provides default epsilon values (SingleEpsilon
, DoubleEpsilon
, ExtendedEpsilon
) based on the data type of the floating-point values being compared.
function SameValue(const A, B: Single; Epsilon: Single = 0): Boolean; overload;
function SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload;
function SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;
My questions are:
- In general use-cases, is it reliable to use the SameValue function without specifying a custom epsilon?
- Are there specific scenarios where one must or should provide a custom epsilon value?
- If providing a custom epsilon, what's a good strategy to determine an appropriate value based on the context?
Would appreciate insights or best practices!
SameValue(A, B)
is good enough, even though the semantics may not be precisely right. However, IIRC, David has some rather strong opinions aboutSameValue
that he may have time to repeat here.