compact_real_string Subroutine

private pure subroutine compact_real_string(string)

Compact a string representing a real number, so that the same value is displayed with fewer characters.

Arguments

Type IntentOptional AttributesName
character(len=*), intent(inout) :: string

string representation of a real number.

Called By

proc~~compact_real_string~~CalledByGraph proc~compact_real_string compact_real_string proc~str_r8p str_R8P proc~str_r8p->proc~compact_real_string proc~str_r16p str_R16P proc~str_r16p->proc~compact_real_string proc~str_r4p str_R4P proc~str_r4p->proc~compact_real_string proc~str_a_r8p str_a_R8P proc~str_a_r8p->proc~str_r8p interface~str str interface~str->proc~str_r8p interface~str->proc~str_r4p interface~str->proc~str_a_r8p proc~str_a_r4p str_a_R4P interface~str->proc~str_a_r4p proc~bctor_r8p bctor_R8P proc~bctor_r8p->interface~str program~compact_real compact_real program~compact_real->interface~str proc~bctoi_i1p bctoi_I1P proc~bctoi_i1p->interface~str proc~bctor_r4p bctor_R4P proc~bctor_r4p->interface~str proc~bctoi_i4p bctoi_I4P proc~bctoi_i4p->interface~str proc~stringify~2 stringify proc~stringify~2->interface~str proc~bctoi_i8p bctoi_I8P proc~bctoi_i8p->interface~str program~test_all test_all program~test_all->interface~str interface~bcton bcton program~test_all->interface~bcton proc~stringify stringify proc~stringify->interface~str proc~bctor_r16p bctor_R16P proc~bctor_r16p->interface~str proc~bctoi_i2p bctoi_I2P proc~bctoi_i2p->interface~str interface~bcton->proc~bctor_r8p interface~bcton->proc~bctoi_i1p interface~bcton->proc~bctor_r4p interface~bcton->proc~bctoi_i4p interface~bcton->proc~bctoi_i8p interface~bcton->proc~bctoi_i2p proc~str_a_r16p str_a_R16P proc~str_a_r16p->proc~str_r16p proc~str_a_r4p->proc~str_r4p
Help

Source Code


Source Code

  pure subroutine compact_real_string(string)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< author: Izaak Beekman
  !< date: 02/24/2015
  !<
  !< Compact a string representing a real number, so that the same value is displayed with fewer characters.
  !---------------------------------------------------------------------------------------------------------------------------------
  character(len=*),intent(inout) :: string      !< string representation of a real number.
  character(len=len(string))     :: significand !< Significand characters.
  character(len=len(string))     :: expnt       !< Exponent characters.
  character(len=2)               :: separator   !< Separator characters.
  integer(I4P)                   :: exp_start   !< Start position of exponent.
  integer(I4P)                   :: decimal_pos !< Decimal positions.
  integer(I4P)                   :: sig_trim    !< Signature trim.
  integer(I4P)                   :: exp_trim    !< Exponent trim.
  integer(I4P)                   :: i           !< counter
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  string = adjustl(string)
  exp_start = scan(string, 'eEdD')
  if (exp_start == 0) exp_start = scan(string, '-+', back=.true.)
  decimal_pos = scan(string, '.')
  if (exp_start /= 0) separator = string(exp_start:exp_start)
  if ( exp_start < decimal_pos ) then ! possibly signed, exponent-less float
    significand = string
    sig_trim = len(trim(significand))
    do i = len(trim(significand)), decimal_pos+2, -1 ! look from right to left at 0s, but save one after the decimal place
      if (significand(i:i) == '0') then
        sig_trim = i-1
      else
        exit
      endif
    enddo
    string = trim(significand(1:sig_trim))
  elseif (exp_start > decimal_pos) then ! float has exponent
    significand = string(1:exp_start-1)
    sig_trim = len(trim(significand))
    do i = len(trim(significand)),decimal_pos+2,-1 ! look from right to left at 0s
      if (significand(i:i) == '0') then
        sig_trim = i-1
      else
        exit
      endif
    enddo
    expnt = adjustl(string(exp_start+1:))
    if (expnt(1:1) == '+' .or. expnt(1:1) == '-') then
      separator = trim(adjustl(separator))//expnt(1:1)
      exp_start = exp_start + 1
      expnt     = adjustl(string(exp_start+1:))
    endif
    exp_trim = 1
    do i = 1,(len(trim(expnt))-1) ! look at exponent leading zeros saving last
      if (expnt(i:i) == '0') then
        exp_trim = i+1
      else
        exit
      endif
    enddo
    string = trim(adjustl(significand(1:sig_trim)))// &
             trim(adjustl(separator))// &
             trim(adjustl(expnt(exp_trim:)))
  !else ! mal-formed real, BUT this code should be unreachable
  endif
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine compact_real_string


add_clone add_clone add_id add_pointer add_pointer allocate_members assert_close_r32 assert_close_r32_1 assert_close_r64 assert_close_r64_1 assert_equal_i16 assert_equal_i16_1 assert_equal_i32 assert_equal_i32_1 assert_equal_i64 assert_equal_i64_1 assert_equal_i8 assert_equal_i8_1 assert_equal_l assert_equal_l_1 assert_equal_r32 assert_equal_r32_1 assert_equal_r64 assert_equal_r64_1 assert_positive_i16 assert_positive_i16_1 assert_positive_i32 assert_positive_i32_1 assert_positive_i64 assert_positive_i64_1 assert_positive_i8 assert_positive_i8_1 assert_positive_r32 assert_positive_r32_1 assert_positive_r64 assert_positive_r64_1 bctoi_I1P bctoi_I2P bctoi_I4P bctoi_I8P bcton bctor_R16P bctor_R4P bctor_R8P bit_size bit_size bit_size_chr bit_size_R16P bit_size_R4P bit_size_R8P bstr bstr_I1P bstr_I2P bstr_I4P bstr_I8P bstr_R16P bstr_R4P bstr_R8P byte_size byte_size_chr byte_size_I1P byte_size_I2P byte_size_I4P byte_size_I8P byte_size_R16P byte_size_R4P byte_size_R8P check_endian check_type compact_real_string creator ctoi_I1P ctoi_I2P ctoi_I4P ctoi_I8P cton ctor_R16P ctor_R4P ctor_R8P destroy destroy destroy destroy destroy destroy_content destroy_dictionary_node destroy_key dictionary_len digit digit_I1 digit_I2 digit_I4 digit_I8 finalize finalize finalize find_next_prime get_bucket_image_indexes get_clone get_clone get_pointer get_pointer get_pointer has_key has_key has_key hash hash hash_string hash_table_finalize hash_table_len id ids ids init initialize initialize initialize is_equal is_equal is_filled is_homogeneous is_initialized is_key_allowed is_prime iterator_max iterator_max key_base len len loop node penf_init penf_print print print_content_iterator print_content_iterator print_keys print_keys remove remove remove_by_pointer remove_id set set_buckets_number set_caf_dimensions set_clone set_homogeneous set_pointer str str_a_I1P str_a_I2P str_a_I4P str_a_I8P str_a_R16P str_a_R4P str_a_R8P str_bol str_I1P str_I2P str_I4P str_I8P str_R16P str_R4P str_R8P strf_I1P strf_I2P strf_I4P strf_I8P strf_R16P strf_R4P strf_R8P stringify stringify strz strz_I1P strz_I2P strz_I4P strz_I8P synchronize_images test_assert_equal test_assert_equal test_assert_equal test_dictionary_finalize tests_non_captured tests_non_captured traverse traverse traverse_iterator