forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Real representation

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Sat Nov 12, 2011 10:10 pm    Post subject: Real representation Reply with quote

I am using a routine to convert a character to real.

I have:

'7.500000000000000E-2' (character)

The routine which converts character to real can read only the first 20 characters.

Please advise how to represent only 20 characters in '7.500000000000000E-2' such that the correct real number is obtaned.

I do not have access to the routine which carries out the conversion - so please help howw to send the 20 characters such that the correct real number 7.5E-2 is obtain ed.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2555
Location: Sydney

PostPosted: Mon Nov 14, 2011 6:19 am    Post subject: Reply with quote

You could provide your own routine to read, such as adapting from the following example:
Code:
 character real_string*25, aa(3)*25
 real*10    x
 real*8     y
 integer*4 iostat,i
!
 aa(1) = '7.500000000000000E-2'
 aa(2) = '3.500000000000003400E-2'
 aa(3) = '3.500003400000000000E-20'
!
 do i = 1,3
   real_string = aa(i)
   read (real_string, fmt='(bn,f25.0)', iostat=iostat) x
   write (*,*) 'String = ',real_string
   write (*,*) 'X = ',x
   write (*,*) 'error = ',iostat
   y = x
   write (*,*) 'Y = ',y
   call trim_real_string (real_string)
   write (*,*) 'String = ',real_string
 end do
!
end
 
subroutine trim_real_string (real_string)
!
!  trims training 0 from real number string
!
 character real_string*(*)
!
 character real_part*25, exponent_part*10
 integer*4 ie,i
!
 ie = index(real_string, 'E')
!
 if (ie > 0) then
   real_part     = real_string(1:ie-1)
   exponent_part = real_string(ie:)
 else
   real_part     = real_string
   exponent_part = ' '
 end if
!
 do i = len_trim(real_part),1,-1
   if (real_part(i:i) /= '0') exit
 end do
!
 if (index(real_part, '.') > 0) real_string = real_part(:i) // exponent_part
!
end subroutine trim_real_string


John
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1887

PostPosted: Mon Nov 14, 2011 4:24 pm    Post subject: Re: Real representation Reply with quote

christyleomin wrote:

Please advise how to represent only 20 characters in '7.500000000000000E-2' such that the correct real number is obtained.


Neither 4-byte nor 8-byte IEEE floating point formats can represent 0.075 exactly.

In hex, the number is either Z'3BF5C28F' (32-bit) or Z'3FB3333333333333' (64-bit).

In general, when such numbers are read and printed from source code, compiled and run, there is no guarantee that the output number will match the input number in decimal representation.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group