stdlib
stdlib copied to clipboard
function to_num() does not convert "+1" correctly
Description
The function to_num() from str2num does not convert correctly if the number has a leading "+". See the following minimal working example:
program mwe_to_num
use stdlib_str2num, only: to_num
implicit none
character(len=5) :: tstr
integer :: k
tstr = "+1"
k = to_num(tstr, k)
print *, k ! prints "0" (wrong)
tstr = "1"
k = to_num(tstr, k)
print *, k ! prints "1" (correct)
tstr = "-1"
k = to_num(tstr, k)
print *, k ! prints "-1" (correct)
stop
end program
Output:
nmznsnch@namizono-desktop:~/Codes/stdlib$ ./mwe_to_num
0
1
-1
Expected Behaviour
I expect "+1" to be converted to (integer) 1, not 0
Version of stdlib
Downloaded june 2026
Platform and Architecture
PC, Ubuntu 24.04.4 LTS, Intel Core i5-6400, gfortran 13.3.0
Additional Information
No response
I just did git pull but the issue was NOT resolved.
Note the following: after git pull:
合計 136
drwxrwxr-x 2 nmznsnch nmznsnch 4096 7月 17 22:09 .
drwxrwxr-x 30 nmznsnch nmznsnch 4096 6月 27 20:48 ..
-rw-rw-r-- 1 nmznsnch nmznsnch 410 6月 27 20:48 CMakeLists.txt
-rw-rw-r-- 1 nmznsnch nmznsnch 24937 6月 27 20:48 stdlib_str2num.fypp
-rw-rw-r-- 1 nmznsnch nmznsnch 45092 7月 17 22:09 stdlib_string_type.fypp
-rw-rw-r-- 1 nmznsnch nmznsnch 1235 6月 27 20:48 stdlib_string_type_constructor.fypp
-rw-rw-r-- 1 nmznsnch nmznsnch 39768 6月 27 20:48 stdlib_strings.fypp
-rw-rw-r-- 1 nmznsnch nmznsnch 3882 6月 27 20:48 stdlib_strings_to_string.fypp
The file stdlib_str2num.fypp was NOT updated. Please advise.