Add one to a decimal digit string, growing it on carry out.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(inout) | :: | rounded | |||
| integer(kind=default_int), | intent(inout) | :: | n_rounded | |||
| integer(kind=default_int), | intent(out) | :: | bump |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| logical, | private | :: | carry | ||||
| integer(kind=default_int), | private | :: | i | ||||
| integer(kind=default_int), | private | :: | successor |
pure subroutine increment_digits(rounded, n_rounded, bump) !! Add one to a decimal digit string, growing it on carry out. character(len=*), intent(inout) :: rounded integer(default_int), intent(inout) :: n_rounded integer(default_int), intent(out) :: bump integer(default_int) :: i, successor logical :: carry bump = 0_default_int carry = .true. i = n_rounded do while (carry .and. i >= 1_default_int) if (rounded(i:i) == "9") then rounded(i:i) = "0" else successor = index(ALL_DIGITS, rounded(i:i)) + 1_default_int rounded(i:i) = ALL_DIGITS(successor:successor) carry = .false. end if i = i - 1_default_int end do if (carry) then ! Every retained digit was a nine and is now a zero, so the result is ! a leading one followed by that many zeros. Order matters when ! `n_rounded` is zero: both assignments then address position one. rounded(n_rounded + 1:n_rounded + 1) = "0" rounded(1:1) = "1" n_rounded = n_rounded + 1_default_int bump = 1_default_int end if end subroutine increment_digits