Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

precision

Status
Not open for further replies.

Pfor

Technical User
Aug 11, 2010
22
IT
Hallo everybody,
I am using a code (compiled with gfortran)
which uses the selected_real_kind statement, however
since I had problems I tried to implement a very basic code like:

!-----------------------------------------------
program real_kinds
implicit none
integer,parameter :: p16 = selected_real_kind(p =16)
real ( kind = p16 ) :: b,c

! 12345678901
b = 0.33333333222
c = 0.33333333999

if (c.ne.b) write(*,*) 'not equal'
if (c.eq.b) write(*,*) ' equal'

end program
!-----------------------------------------------------------------
when I run this program the result is that c and b are equal.
In my understanding of "selected_real_kind" statement they should be different.
I also tried to have a quadruple precision with
real ( kind = 16 ) :: b,c
but the compiler (gfortran) returns an error:
Error: Kind 16 not supported for type REAL at (1)

I suppose my error is very trivial however I don't understand what's going on! How do I implement higher precision?
thank you for the help,
cheers,
Paolo

 
Since you are only comparing 11 digits, just make it 12 instead of 16. Also, you need to suffix the numbers otherwise they default to single precision. It depends on whether your machine supports triple precision. If it does, then it might support p=16.
Code:
program real_kinds
   implicit none
   integer,parameter :: p16 = selected_real_kind(p =12)
   real ( kind = p16 ) :: b,c

!        12345678901          
   b = 0.33333333222_p16
   c = 0.33333333999_p16 

   print *, p16
   if (c.ne.b) write(*,*) 'not equal' 
   if (c.eq.b) write(*,*) '    equal'        

 end program
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top