SameValue
Function SameValue( const A, B : Extended): Boolean
Example
procedure ScriptEvent(var Value: variant);
begin
Value := SameValue(10.0, 10.0);
end;
Usage
SameValue tests whether two Extended values differ by no more than Velox's calculated default tolerance.
Parameters
| Name | Type | Description |
|---|---|---|
A | Extended, const | First value. |
B | Extended, const | Second value. |
Returns
For ordinary finite operands, True when their absolute difference is less than or equal to Velox's calculated tolerance; otherwise False.
Behaviour
The tolerance scales from the smaller operand magnitude and has a platform-resolution floor. Equal finite values and positive/negative zero are same. A finite difference exactly on the tolerance boundary is also same.
Errors
The function does not raise merely because an operand is infinite or NaN; it returns the source-defined Boolean. A caller value that cannot convert to Extended can fail before entry.
Usage notes
Validate operands as finite when exceptional IEEE values are possible. Use SameValueEpsilon when the domain supplies an explicit allowed difference, and use decimal rounding/minor-unit rules for monetary equality.
Additional Technical Info
SameValue performs approximate equality for two Extended values using Delphi's calculated default tolerance. It is not bitwise equality and does not apply decimal rounding.
The example returns True. It is source-reviewed and is not executed by the documentation workflow.
Implementation
vxCommonNumber.SameValue calls the Extended overload of System.Math.SameValue(A, B). With its omitted/default Epsilon = 0, current Delphi source calculates:
Epsilon = max(min(abs(A), abs(B)) * ExtendedResolution,
ExtendedResolution)
It then subtracts the smaller value from the larger and uses an inclusive <= Epsilon comparison. ExtendedResolution is 1E-16 when Extended is a true 80-bit type and 1E-12 when it aliases Double.
Edge cases and quirks
- Win32 and Win64 can calculate different tolerances for the same source literals because
Extendedprecision andExtendedResolutiondiffer. - Near zero, the resolution floor can treat distinct small values as same.
- Any
NaNoperand returnsFalse; subtraction/comparison withNaNnever satisfies the inclusive boundary. - Equal same-signed infinities return
Falsebecause infinity minus itself isNaN. - Opposite infinities are a surprising default-tolerance case: both absolute magnitudes are infinite, so the calculated epsilon becomes positive infinity and the infinite difference satisfies
<= Epsilon. They can therefore returnTrue. - A finite value and an infinity return
Falsebecause the calculated tolerance is based on the finite magnitude. - Signed zero is not distinguished. Use a representation-level test if the sign bit matters.
Side effects
None.
Performance and concurrency
The operation is constant-time, allocates no managed data and uses no shared mutable state.
Related entries
SameValueEpsilonaccepts an explicit tolerance.CompareValueuses this same test before returning an ordering.IsZerocompares one value against zero with the resolution floor.
External references
- Embarcadero
System.Math.SameValue - Free Pascal
SameValue- compatibility context; current Delphi constants andExtendedrepresentation define Velox's exact tolerance.