# Faster string to double

**URL:** <https://fortran-lang.discourse.group/t/faster-string-to-double/2208>\
**Category:** Uncategorized\
**Created:** [October 31, 2021, 5:35pm UTC](https://fortran-lang.discourse.group/t/faster-string-to-double/2208 "2021-10-31T17:35:21Z")\
**Posts on this page:** 1\
**Showing post:** 80

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [February 1, 2023, 9:11pm UTC](https://fortran-lang.discourse.group/t/faster-string-to-double/2208/80 "2023-02-01T21:11:53Z")

</div>

I gave some thoughts to the last discussions regarding long strings and tried a small change to the algorithm (you can check out the full version in the [github](https://github.com/jalvesz/Fortran-String-to-Num/blob/main/src/str2num_m.f90)):

```auto
elemental subroutine str2real_unroll(s,r,p,stat)
...
! read whole and fractional number in a single integer
pP = p
int_wp = 0
do i = p, min(19+p-1,len(s)) !< stop at the max precision available for the integer(8)
     val = iachar(s(i:i))-digit_0
     if( val >= 0 .and. val <= 9 ) then
           int_wp = int_wp*10 + val
     else if( val == period ) then
           pP = i
     else
           exit
     end if
end do
p = i

```

With this modification, the following test will pass:

```auto
call check("0.123456789123456789123456789123456789")

```

as I’ll get the same read as formated read : “0.123456789123457”

The test suggested by @RonShepard won’t pass:

```auto
call check("1234567890123456789012345678901234567890-9")

```

as I’ll get : “1.23456789012346” , while intel or gfortran in windows or linux will give “1.2345679E+30”

Any ideas here? (maybe the second part of the algorithm can be tweaked without loosing generality and performance?)

The good thing is that so far I managed to stay within the same performance rate.

---

_[View the full topic](https://fortran-lang.discourse.group/t/faster-string-to-double/2208)._
