increment_digits Subroutine

private pure subroutine increment_digits(rounded, n_rounded, bump)

Add one to a decimal digit string, growing it on carry out.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(inout) :: rounded
integer(kind=default_int), intent(inout) :: n_rounded
integer(kind=default_int), intent(out) :: bump

Called by

proc~~increment_digits~~CalledByGraph proc~increment_digits increment_digits proc~round_decimal round_decimal proc~round_decimal->proc~increment_digits proc~to_string_fixed_dp to_string_fixed_dp proc~to_string_fixed_dp->proc~round_decimal proc~to_string_sci_dp to_string_sci_dp proc~to_string_sci_dp->proc~round_decimal interface~to_string_fixed to_string_fixed interface~to_string_fixed->proc~to_string_fixed_dp proc~to_string_fixed_sp to_string_fixed_sp interface~to_string_fixed->proc~to_string_fixed_sp interface~to_string_sci to_string_sci interface~to_string_sci->proc~to_string_sci_dp proc~to_string_sci_sp to_string_sci_sp interface~to_string_sci->proc~to_string_sci_sp proc~to_string_fixed_sp->proc~to_string_fixed_dp proc~to_string_sci_sp->proc~to_string_sci_dp

Variables

Type Visibility Attributes Name Initial
logical, private :: carry
integer(kind=default_int), private :: i
integer(kind=default_int), private :: successor

Source Code

   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