The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....



GreatestCommonDivisor


Unit: SDL_math1
Class: None
Declaration: [1] function GreatestCommonDivisor (v1, v2: integer): integer;
[2] function GreatestCommonDivisor (values: TIntArray): integere;
[3] function GreatestCommonDivisor (v1, v2: double; Precision: integer): double;
[4] function GreatestCommonDivisor (values: TDoubleArray; Precision: integer): double;

The function GreatestCommonDivisor calculates the greatest common divisor (GCD), either of two values (versions [1] and [3]) or of an array of values (versions [2] and [4]). Further, the function not only supports the calculation of the GCG for integer values (varsions [1] and [2]), but also for floating point values (version [3] and [4]). In the case of floating point numbers you have to specify the number of decimal places to which the calculation is performed.

For versions [1] and [3] the parameters v1 and v2 specify the two numbers to be processed, for versions [2] and [4] the open array values contains the numeric values for which the GCD is to be calculated.

The parameter Precision may assume values between 0 and 12 and defines the number of decimal places to be taken into account when finding the greatest common divisor.

The function returns the GCD. If there is no GCD (except the trivial one) the returned value is 1 in the case of versions[1] and [2] or 10-Precision for versions [3] and [4].

 

Hint: Please note that setting the Precision to meaningful values is crucial in order to get correct results. This is especially important if the values of the numeric array (version [4]) are noisy. As 10-Precision defines the maximum value of the remainder which is accepted the Precision should be set such that the noise is not included. For example, if the noise is in the 3rd decimal place, Precision should be set to a value of 2. In any case, it is recommended to experiment with the particular data array in order to find a suitable compromise.



Last Update: 2024-Jan-18