accdig(3f) - [M_framework__approx] compare two numbers only up to a specified number of digits (LICENSE:PD)
Synopsis
Description
Examples
Notes
References
Dependencies
Authors
License
elemental impure subroutine accdig(x,y,digio,acurcy,ind)
class(*),intent(in) :: X class(*),intent(in) :: Y class(*),intent(in) :: DIGI0 real,intent(out) :: acurcy integer,intent(out) :: ind
This procedure is used to check how closely two numbers agree.
call accdig(X,Y,DIGI0,ACURCY,IND)The values X and Y are the numbers to compare, and DIGI0 is the threshold number of digits to consider significant in returning IND.
If X and Y are considered equal within DIGI0 relative tolerance,
IND = 0, if tolerance is satisfied. = 1, if tolerance is not satisfied.The result ACURCY gives a measure of the number of leading digits in X which are the same as the number of leading digits in Y.
ACURCY=-log10((X-Y)/Y) if X != Y and Y != 0 ACURCY=-log10(X-Y) if X != Y and Y = 0 ACURCY=8 if X=YTOLERANCE ... X and Y are considered equal within DIGI0 relative tolerance, if ACURCY is greater than DIGI0.ACURCY is never less than -8 or greater than 8 for 32-bit REAL values
For example, Take some numbers and compare them to 1.2345678 ... > ================================================ > A number | ACURCY | ACURCY > | 1.2345678=Y | 1.2345678=X > ================================================ > 1.234680 | 3.7900571 | 3.7901275 > 1.2345378 | 4.6144510 | 4.6144404 > 2.2234568 | 0.096367393 | 0.35188114 > 1.2345678 | 8.0000000 | 8.0000000 > 1.2345679 | 7.0732967 | 7.0731968 > -1.2345678 | -0.30103000 | -0.30103000 > 76.234567 | -1.7835463 | 0.0070906729 > 2.4691356 | 0.0 | 0.3010300 > 0.0 | 0.0 | -0.91514942.Due to the typical limits of the log function, the number of significant digits in the result is best considered to be three.
Notice that 1.2345678=Y produces different values than 1.2345678=X
A negative result indicates the two values being compared either do not agree in the first digit or they differ with respect to sign. An example of two numbers which do not agree in their leading digit (and actually differ in order of magnitude) is given above by X=76.234567 and Y=1.2345678; the accuracy reported is -1.7835463. An example of two numbers which do not agree in sign in X=-1.2345678 and Y=1.2345678; here the accuracy reported is -0.30103000.
Example program:
program demo_accdig ! fortran 90 example use M_framework__approx, only : accdig implicit none integer :: digi doubleprecision :: a, b integer :: i10, i20, i30 integer :: ind, ind1, ind2 real :: acurcy, acurcy1, acurcy2 doubleprecision :: vals(9) data vals/ & &1.234680d0, 1.2345378d0, 2.2234568d0, 1.2345678d0, & &1.2345679d0, -1.2345678d0, 76.234567d0, 2.4691356d0, & &0.0d0/ write(*,*)========================= do i10=0,16 a=1.0d0 b=a+1.0d0/(10.0d0**i10) call accdig(a,b,8.0,acurcy,ind) write(*,*)i10,a,b,acurcy,ind enddo write(*,*)========================= digi=16 do i20=0,digi a=1.0d0 b=a+1.0d0/(10.0d0**i20) call accdig(a,b,dble(digi),acurcy,ind) write(*,*)i20,a,b,acurcy,ind enddo write(*,*)========================= do i30=1,9 call accdig(1.2345678d0,vals(i30),8.0,acurcy1,ind1) call accdig(vals(i30),1.2345678d0,8.0,acurcy2,ind2) write(*,*)i30,vals(i30),acurcy1,acurcy2,ind1,ind2 enddo end program demo_accdig
based on ...
NBS OMNITAB 1980 VERSION 6.01 1/ 1/81. dp_accdig V 7.00 2/14/90. ** David Hogben, Statistical Engineering Division, Center for Computing and Applied Mathematics, A337 Administration Building, National Institute of Standards and Technology, Gaithersburg, MD 20899 TELEPHONE 301-975-2845 ORIGINAL VERSION - October, 1969. CURRENT VERSION - February, 1990. JSU VERSION - February, 1991.
o M_framework__journal(), log10(), abs(1)
David Hogben, John S. Urban
Public Domain
Nemo Release 3.1 | accdig (3) | February 23, 2025 |