2

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:

  1. In general use-cases, is it reliable to use the SameValue function without specifying a custom epsilon?
  2. Are there specific scenarios where one must or should provide a custom epsilon value?
  3. 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!

6
  • Without attempting to write a full answer, just some quick spontaneous thoughts: (1) the default implementation for Epsilon = 0 seems just fine, (2) one clear case (but probably you were looking for more obscure cases) is where you yourself simply define that everything that is within e.g. 0.5 or 1 or even 10 from each other is the same, (3) if your float values are going to be stored in a database table, then in some scenario it could perhaps be useful to choose an Epsilon that makes your SameValue work (roughly) with the same precision as the data type in that database table.
    – Matthias B
    Sep 28 at 7:36
  • For example, the money type in SQL server uses 4 decimals, so an Epsilon of 0.0001 or 0.0000999 might be useful. (Although you probably shouldn't use the money type like that, it's essentially an integer value. But, still, as a theoretical example.)
    – Matthias B
    Sep 28 at 7:40
  • This entirely depends on your application. You may want to buy a textbook about numerical algorithms or scientific computing. Sep 28 at 10:07
  • @AndreasRejbrand yes I agree but i read somewhere that using samevalue, comparevalue, etc without specifying an Epsilon value is a non sense. I just want to dig in it to know why ?
    – zeus
    Sep 28 at 11:18
  • I'd say in quite a few every-day instances a plain SameValue(A, B) is good enough, even though the semantics may not be precisely right. However, IIRC, David has some rather strong opinions about SameValue that he may have time to repeat here. Sep 28 at 11:21

1 Answer 1

2

One of the issue with the default epsilon supplied by delphi is that, in a chain of operations, the cumulative rounding error can outgrow the default epsilon. So, the more operations you apply to a value, the less likely SameValue will behave as desired with the default epsilon.

As for a strategy for determining a custom epsilon. I personnaly find that using a value that is 1 order of magnitude lower than what we could call the "base meaningful unit"(BMU) of the context is decent.

For example, if we're talking about a retail USD price, the BMU would be 0.01$ so I'd use 0.001 as an epsilon. But then, unitary prices are often required to have a 4 decimals precision, in which case the BMU would be 0.0001$. In that situation, I'd use 0.00001 as the epsilon.

There are most likely contexts where the idea of a BMU doesn't apply. In those cases, I'm not sure how I would go about selecting a custom epsilon. Using the default one would probably be the safest bet.

As everything floating point related, this remains a "must read"

EDIT: Andreas brings a good point. Delphi uses by default an epsilon that is at least FloatTypeResolution. For Double(Other types have similar situation), that value is 1E-12, but Double can have values of the order of 1E-308 (or even smaller). So, when working with very small values, an explicit epsilon would also be required.

5
  • hmm yes that make sense, cumulative rounding error can outgrow the default epsilon. thanks!
    – zeus
    Sep 28 at 14:44
  • 1
    Although this A makes perfect sense, it doesn't address all issues related to SameValue. Remember that SameValue(x, y, ε) is not defined as simply Abs(x - y) <= ε but also takes into account the magnitudes of x and y. So very interesting things can happen if x and y are of different orders of magnitude and/or very close to zero. Sep 29 at 10:01
  • @AndreasRejbrand I'm currently looking at the source code of System.Math.SameValue of Delphi 11.3, and it is defined as (basically) Abs(x - y) <= ε . (Instead of using Abs, they are inverting operand to ensure positive value, i.e x-y or y-x). So I'm not sure what you are referring to. Sep 29 at 14:03
  • 1
    @KenBourassa: Sorry, I meant to write that the default (omitted) epsilon causes such a consideration. You're absolutely right that you get a plain Abs(x - y) <= ε if you specify your own (positive) epsilon. If you don't specify it explicitly, you get Epsilon := Max(Min(Abs(A), Abs(B)) * ExtendedResolution, ExtendedResolution); which may or may not be suitable. Sep 29 at 14:13
  • @AndreasRejbrand Good point. But, from my understanding, it's just really an issue if both values are very small. Added some details in that regard. Sep 29 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.