From 35dcffeb7b267a29a28d791b697e51e7c76b4765 Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Thu, 11 Jun 2026 21:48:29 -0500 Subject: [PATCH] fixed bug in diff module with the sign function. This was apparently a mistranslation of the original algol code. See https://github.com/jacobwilliams/diff/issues/2 some formatting/typo fixes --- src/cache_module.f90 | 6 +++--- src/diff_module.f90 | 10 +++++----- src/utilities_module.f90 | 2 +- tests/test2.f90 | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/cache_module.f90 b/src/cache_module.f90 index 18e8b60..fb4cd0b 100644 --- a/src/cache_module.f90 +++ b/src/cache_module.f90 @@ -56,7 +56,7 @@ subroutine initialize_cache(me,isize,n,m,chunk_size) class(function_cache),intent(inout) :: me integer,intent(in) :: isize !! the size of the hash table - integer,intent(in) :: n !! number of independant variables (x) + integer,intent(in) :: n !! number of independent variables (x) integer,intent(in) :: m !! number of functions (f) integer,intent(in),optional :: chunk_size !! chunk size to speed up reallocation !! of arrays. A good value is a guess for @@ -127,7 +127,7 @@ subroutine get_from_cache(me,x,ifs,i,f,xfound,ffound) implicit none class(function_cache),intent(inout) :: me - real(wp),dimension(:),intent(in) :: x !! independant variable vector + real(wp),dimension(:),intent(in) :: x !! independent variable vector integer,dimension(:),intent(in) :: ifs !! elements of `f` needed integer,intent(out) :: i !! index in the hash table real(wp),dimension(:),intent(out) :: f !! `f(x)` from the cache (if it was found) @@ -178,7 +178,7 @@ subroutine put_in_cache(me,i,x,f,ifs) class(function_cache),intent(inout) :: me integer,intent(in) :: i !! index in the hash table - real(wp),dimension(:),intent(in) :: x !! independant variable vector (dimension `n`) + real(wp),dimension(:),intent(in) :: x !! independent variable vector (dimension `n`) real(wp),dimension(:),intent(in) :: f !! function vector `f(x)` (dimension `m`) integer,dimension(:),intent(in) :: ifs !! elements of `f` to add (should all be `>0, <=m`) diff --git a/src/diff_module.f90 b/src/diff_module.f90 index 85c40ac..116d57f 100644 --- a/src/diff_module.f90 +++ b/src/diff_module.f90 @@ -7,7 +7,7 @@ !## Authors ! * J. Oliver, "An algorithm for numerical differentiation of a function ! of one real variable", Journal of Computational and Applied Mathematics -! 6 (2) (1980) 145–160. [Algol 60 source in original paper] +! 6 (2) (1980) 145-160. [Algol 60 source in original paper] ! * David Kahaner, Fortran 77 code from ! [NIST](ftp://math.nist.gov/pub/repository/diff/src/DIFF) ! * Jacob Williams : 2/17/2013 : Converted to modern Fortran. @@ -834,7 +834,7 @@ subroutine faccur(me,h0,h1,facc,x0,twoinf,f0,f1,ifail) end if end if if (abs(h1) >= 32.0_wp*twoinf) h1 = h1/8.0_wp - if (16.0_wp*abs(h1) > abs(h0)) h1 = sign(h1,1.0_wp)*abs(h0)/16.0_wp + if (16.0_wp*abs(h1) > abs(h0)) h1 = sign(1.0_wp,h1)*abs(h0)/16.0_wp f001 = me%f(x0+h0-h1) if (me%stop) then ifail = -1 @@ -854,7 +854,7 @@ subroutine faccur(me,h0,h1,facc,x0,twoinf,f0,f1,ifail) end do h1 = 8.0_wp*h1 else - h1 = sign(h1,1.0_wp)*abs(h0)/16.0_wp + h1 = sign(1.0_wp,h1)*abs(h0)/16.0_wp end if else if (256.0_wp*twoinf <= abs(h0)) then @@ -868,9 +868,9 @@ subroutine faccur(me,h0,h1,facc,x0,twoinf,f0,f1,ifail) h1 = h1/2.0_wp end do h1 = 8.0_wp*h1 - if (16.0_wp*abs(h1) > abs(h0)) h1 = sign(h1,1.0_wp)*abs(h0)/16.0_wp + if (16.0_wp*abs(h1) > abs(h0)) h1 = sign(1.0_wp,h1)*abs(h0)/16.0_wp else - h1 = sign(h1,1.0_wp)*abs(h0)/16.0_wp + h1 = sign(1.0_wp,h1)*abs(h0)/16.0_wp end if end if else diff --git a/src/utilities_module.f90 b/src/utilities_module.f90 index 779220c..c7c6625 100644 --- a/src/utilities_module.f90 +++ b/src/utilities_module.f90 @@ -179,7 +179,7 @@ function unique_real(vec,chunk_size) result(ivec_unique) implicit none - real(wp),dimension(:),intent(in) :: vec !! a vector of integers + real(wp),dimension(:),intent(in) :: vec !! a vector of reals integer,intent(in) :: chunk_size !! chunk size for adding to arrays real(wp),dimension(:),allocatable :: ivec_unique !! unique elements of `ivec` diff --git a/tests/test2.f90 b/tests/test2.f90 index ad9682c..803306d 100644 --- a/tests/test2.f90 +++ b/tests/test2.f90 @@ -74,7 +74,7 @@ program test2 font_size = 30, & xlabel = 'Finite Difference Perturbation Step Size $h$',& ylabel = 'Finite Difference Derivative Error',& - title = 'Derivative of $x + \sin(x)$ at $x=1$ '//real_kind_str,& + title = 'Derivative of $x + \\sin(x)$ at $x=1$ '//real_kind_str,& legend = .true., & legend_fontsize = 10,& usetex = .true.)