In addition, I also notice that in Fortran
double precision, parameter :: ans = 2.0 / sqrt(3.0)
write(*, ‘(A, F20.17)’) 'Expected result: ', ans
! Expected result: 1.15470051765441895
In contrast, in C
printf(“2.0/sqrt(3.0) = %.20f\n”, 2.0 / sqrt(3.0));
Welcome @Lavender (to the world of confusing precision…)
Apart from precision, I am wondering how integral_fun in the C code is declared. Also, the output of ier in both Fortran and C is 1. Is this OK…? (the manual seems to say it should be 0 for success.)
In this case (call in Fortran), dqag indeed return ier=1. In my option, all the ouput should be same if I call it in C.
However, they give the different output which is confusing.
In Fortran, float literals are single precision by default (the opposite of C and C++). You need to write 2.0d0/sqrt(3.0d0) for double precision literals.
One reason for the difference will be that “1.0” etc in Fortran is single precision by default (so we need to write it as “1.0d0” etc), but I wonder why ier = 1 for the output…
This callback takes the input by value, but I’m guessing the Fortran callback takes it by reference. (Leaving aside other interoperability issues like name mangling, or the fact your Fortran compiler may use a different argument passing mechanism entirely.)
RE ier = 1, I’ve plotted the integrand, which seems pretty oscillatory in [0,1] (because of sin(10 pi x)). And, because qng() is a non-adaptive integrator (according to the following page), it may be simply that qng() does not provide sufficient accuracy for this integrand (unlike a globally adaptive one like qags()).
(I’ve recently used this quad() for my work, so just interested in the internal…)