NthRoot
function NthRoot(const aN: Integer; const aValue: Extended): Extended;
Example
procedure ScriptEvent(var Value: variant);
begin
Value := NthRoot(3, 125); // approximately 5
end;
Usage
NthRoot computes an nth root of a positive Extended value through base-10 logarithm and exponentiation.
Parameters
| Name | Type | Description |
|---|---|---|
aN | Integer, const | Root degree. Must be non-zero. Positive values produce a root and negative values produce its reciprocal. The degree is the first argument. |
aValue | Extended, const | Radicand. Velox requires a strictly positive value because it calls Log10 first. |
Returns
A positive Extended approximation of aValue raised to 1 / aN when aValue > 0 and aN <> 0.
| Call | Intended result |
|---|---|
NthRoot(2, 81) | approximately 9 |
NthRoot(3, 125) | approximately 5 |
NthRoot(-2, 81) | approximately 1 / 9 |
NthRoot(1, X) | approximately X, potentially with avoidable rounding |
Additional Technical Info
NthRoot calculates an nth root for a positive Extended value. A positive aN requests a root; a negative aN requests the reciprocal of that root.
The implementation is logarithmic rather than a dedicated root algorithm. It evaluates Power(10, Log10(aValue) / aN), which creates important domain and precision constraints. The example is fictional and source-reviewed only.
Implementation
uPSI_vxCommonNumberdeclares the function in the Math group and registers the nativevxCommonNumber.NthRootaddress directly.NthRootcalls DelphiSystem.Math.Log10(aValue).- It divides that logarithm by the integer
aNusing floating-point division. - It calls Delphi
System.Math.Power(10, quotient)and returns the result unchanged.
There is no explicit argument validation and no alternative path for zero or negative radicands.
Domain and selection guidance
- Use only when
aValue > 0andaN <> 0. - Prefer
Sqrtfor square roots.Sqrtdirectly supports zero and avoids the extra logarithm/exponentiation round trips. - This implementation cannot calculate an odd root of a negative number. For example,
NthRoot(3, -125)reachesLog10(-125)and is invalid even though the real mathematical cube root is-5. - If a negative odd-root result is required, validate the input and explicitly apply the sign around
NthRoot(aN, Abs(aValue)).
Edge cases and quirks
aN = 0divides a floating-point logarithm by zero. ForaValue = 1, the intermediate operation is0 / 0; for other valid values it is a non-zero value divided by zero. Either is outside the supported domain.aValue = 0callsLog10(0)before exponentiation and can signal divide-by-zero or produce negative infinity under a masked floating-point environment. Do not rely on a downstream apparent zero result.aValue < 0callsLog10outside its real domain and can signal invalid operation or produce NaN.aValue = 1returns 1 for supported non-zero degrees, subject to floating-point processing.- Even
aN = 1takes the logarithm and power path, so the returned bits need not equal the input bits. - Very large or very small values can accumulate logarithm, division and exponentiation error, and can overflow or underflow.
Errors and side effects
The function is pure and allocates no Velox objects. Floating-point domain, division, overflow and underflow conditions are not caught. Depending on the active Delphi floating-point exception mask, a condition can raise into the script or produce an infinity/NaN/zero special result. Scripts should validate the domain rather than depend on a particular mask.
Related entries
Sqrtis the preferred direct square-root operation.Poweraccepts only an integer exponent and therefore cannot directly express1 / aN.Abscan support an explicitly sign-managed odd-root calculation.
External references
Created 2026-07-15