/* Copyright (c) 1996 by R. A. Vowels, from "Introduction to PL/I, Algorithms, and */ /* Structured Programming". Permission is given to reproduce and to use these procedures */ /* as part of a program, and to include them as part of a larger work to be sold for profit. */ /* However, the user is not permitted to sell the procedures separately. Provided always */ /* that these procedures and this copyright notice are reproduced in full. */ /* This generic function returns the smallest value which, when added to 1.0, yields */ /* a value greater than 1.0. */ /* Modification to the specific calls may be required to suit a particular machine. */ DECLARE EPSILON GENERIC ( EPSILON6 WHEN (FLOAT DECIMAL (1)), EPSILON6 WHEN (FLOAT DECIMAL (2)), EPSILON6 WHEN (FLOAT DECIMAL (3)), EPSILON6 WHEN (FLOAT DECIMAL (4)), EPSILON6 WHEN (FLOAT DECIMAL (5)), EPSILON6 WHEN (FLOAT DECIMAL (6)), EPSILON15 WHEN (FLOAT DECIMAL (7)), EPSILON15 WHEN (FLOAT DECIMAL (8)), EPSILON15 WHEN (FLOAT) ); EPSILON6: PROCEDURE (X) OPTIONS (ORDER) RETURNS (FLOAT DECIMAL); DECLARE X FLOAT DECIMAL; DECLARE (Sum, Eps) FLOAT DECIMAL; DECLARE Small FLOAT DECIMAL STATIC INITIAL (0); IF Small ^= 0 THEN /* Once we know Epsilon, we don't need */ RETURN (Small); /* to determine it again. */ Eps = 1; DO UNTIL (Sum = 1.0); Small = Eps; /* Required on loop termination. */ Eps = Eps/2; Sum = 1.0 + Eps; END; RETURN (Small); END EPSILON6; EPSILON15: PROCEDURE (X) OPTIONS (ORDER) RETURNS (FLOAT DECIMAL (15) ); DECLARE X FLOAT DECIMAL (15); DECLARE (Sum, Eps) FLOAT DECIMAL (15); DECLARE Small FLOAT DECIMAL (15) STATIC INITIAL (0); IF Small ^= 0 THEN /* Once we know Epsilon, we don't need */ RETURN (Small); /* to determine it again. */ Eps = 1; DO UNTIL (Sum = 1.0); Small = Eps; /* Required on loop termination. */ Eps = Eps/2; Sum = 1.0 + Eps; END; RETURN (Small); END EPSILON15;