-
Notifications
You must be signed in to change notification settings - Fork 186
Base 2 logarithm #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you, Steve. While it may be trivial for you, I suspect it's not as easy for the average Fortran programmer, and especially not for a novice. We're targeting newcomers to the language as well, to make working with Fortran as easy as possible for them. It took you (an expert) 15 minutes, but it could take somebody else 30 minutes or even an hour. If stdlib helps even only 100 people not write their own No matter how simple the implementation is, it's useful to have an off-the-shelf solution that you can just import and be done with it. Would you like to contribute your implementation to stdlib? I agree we should mine |
Steve, thanks, that's music to my ears. And if you agree, we can indicate in the commit message that it is co-authored by you, which GitHub will pick up and attribute proper credit. I will soon open an issue dedicated to mining |
The "trivial" implementation also does not give equivalent results to module m_libm
interface
function libm_log2(x) bind(C, name="log2")
use, intrinsic :: iso_c_binding
real(c_double) :: libm_log2
real(c_double), intent(in) :: x
end function libm_log2
function libm_log(x) bind(C, name="log")
use, intrinsic :: iso_c_binding
real(c_double) :: libm_log
real(c_double), intent(in), value :: x
end function libm_log
end interface
end module m_libm
module m_log2
contains
real(8) function my_log2(x)
real(8), intent(in) :: x
real(8), parameter :: invln2 = 1 / log(2.e0_8)
my_log2 = exponent(x) + log(fraction(x)) * invln2
end function my_log2
end module m_log2
program test
use m_libm
use m_log2
implicit none
integer, parameter :: n = 10000
real(8), parameter :: xmax = 1000.0_8
integer :: i
real(8) :: x,y1,y2,d
do
call random_number(x)
x = x * xmax
#ifdef LOG2
y1 = libm_log2(x)
y2 = my_log2(x)
#elif LOG
y1 = libm_log(x)
y2 = log(x)
#endif
d = y1 - y2
if (d /= 0.0_8) then
write(*,*) x, y1, y2
end if
end do
end program test Very quickly produces differences:
The same test with
edit: add missing |
Thanks for the detailed reply (including pointing out some errors on my part). Hopefully this points to the utility of a high quality implementation of |
The previous comments show that the implementation is not trivial to get a high quality solution, but focusing on implementing |
By this logic there is no point in having any features in fortran that exist in any C standard library. I don't know the full history of including a variety of Bessel functions, etc, but it does seem odd to have such an otherwise full set of special functions that isn't a full superset of what you get with @trippalamb there isn't much logic in specifying edit2: to be clear, I don't actually feel very strongly one way or another about this feature/item in case the above seems too argumentative |
Looks like we lost some replies to this issue. I will attempt to summarize some of the points made so they are not lost:
|
Implement base 2 logarithm function
log2
. Proposed by @brandongc in j3-fortran/fortran_proposals#222. This looks like it belongs instdlib_math
.Prior art
numpy.log2
log2
Base.log2
The text was updated successfully, but these errors were encountered: