Re: increasing width




"Steve Lionel" <steve.lionel@xxxxxxxxx> wrote in message
news:1173566584.177955.156320@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Mar 10, 5:00 pm, "Lane Straatman" <inva...@xxxxxxxxxxx> wrote:
The error is from the line:
real(8) :: total, term
, which I get every time I want to declare real(8). The warning that it
issues would seem to point to the way out. How do I make a real variable
with 'SELECTED_REAL_KIND(6,37)'?

A hard-coded KIND specifier, which the (8) is, is non-portable, Most
Fortrans use kind 8 for double precision but not all. What your
compiler helpfully recommends is this:

real(KIND=SELECTED_REAL_KIND(6,37)) :: varname

This says "give me the kind of REAL that has at least 6 decimal digits
of precision and a range of at least 10**+-37" It is portable to any
Fortran 90 or later implementation.
I thought I would ask for 13 decimal digits and let 37 be:

program numsci1
IMPLICIT NONE
real(KIND=SELECTED_REAL_KIND(13,37)), external :: factorial
integer :: iterations, i, p, x
real(KIND=SELECTED_REAL_KIND(13,37)) :: total, term
iterations = 20
total = 0
x = 7
p = 5
do i = 0, iterations - 1 ! i is maxed at iterations
term = ((-1)**i)*((x/2.0)**(2*i+p))
term = term /factorial(i)
term = term /factorial(i+p)
write (*, '(i2,f20.16)') i, term
total = total + term
end do
write (*, '(a, i2)') 'x is ', x
write (*, '(a, i2)') 'p is ', p
write (*, '(a, f20.16)') 'total = ', total
write (*, '(a)' ) 'sought: 0.34789632475118328514'
end program numsci1
real(KIND=SELECTED_REAL_KIND(13,37)) function factorial(ii)
integer :: ii, i
real(KIND=SELECTED_REAL_KIND(13,37)):: fact
fact=1.
do i = 1,ii
fact=fact*float(i)
end do
factorial = fact
end function factorial

0 4.3768229166666668
[...]
19 0.0000000000000000
x is 7
p is 5
total = 0.3478965884322742
sought: 0.34789632475118328514
It seems I have single precision. What gives?
--
LS


.



Relevant Pages

  • Re: increasing width
    ... is single precision. ... using a contained procedure and eliminated implicit type conversions ... integer:: iterations, i, p, x ... end program numsci1 ...
    (comp.lang.fortran)
  • Re: exact fixed-point inverse square root
    ... I add more iterations than nessesary I always got biten by the precision loss. ... For inverse square root, many values ... ones I know of are two iterations for single precision, ...
    (comp.arch.arithmetic)
  • Re: increasing width
    ... integer:: iterations, i, p, x ... end program numsci2 ... The calculation of fact is otherwise done in single precision, ...
    (comp.lang.fortran)