For Programmers: Free Programming Magazines  


Home > Archive > Fortran > March 2008 > KIND function question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author KIND function question
utab

2008-03-24, 8:12 am

Dear all,

I was reading about the portability of Fortran programs on different
platforms by using the SELECTED_INT_KIND and ..._REAL

Using the SELECTED_INT_KIND(4) will be used for integer values that
are in
the range 10^-4 to 10^4

However the below program is used to take a look at the default kind
values returned by the KIND function

PROGRAM ch0805
IMPLICIT NONE
INTEGER :: i
INTEGER(selected_int_kind(4)) :: j
REAL :: r
CHARACTER*1 :: c
LOGICAL :: l
COMPLEX ::
cp
-4 4
PRINT *,' Integer ',KIND(i) ! on my machine by default 4, it
means 10 to 10
PRINT *,' Real ',KIND(r)
PRINT *,' Char ',KIND(c)
PRINT *,' Logical ',KIND(l)
PRINT *,' Complex ',KIND(cp)
i=1000000
j=1000_4
print *, i, j
END PROGRAM ch0805


the value of i is larger then the default range of integer values.
Even if I assign "100000000" to i, it still is valid and prints out
the value. I guess I am missing sth important and some clarification
is appreciated. On the j case, it is not ok to assign for instance
100000 to the j variable because it is already constrained in the
specification part.

If I apply this as well, I am getting with ifort

fortcom: Warning: ch0805.f90, line 15: The INTEGER(KIND=2) value is
out-of-range. [100000]
j=100000
----^
and the output is
Integer 4
Real 4
Char 1
Logical 4
Complex 4
100000000 -31072

Herman D. Knoble

2008-03-24, 8:12 am

You might glance at:

http://ftp.aset.psu.edu/pub/ger/for...KindExample.f90
http://ftp.aset.psu.edu/pub/ger/for...rDPrecision.f90

and maybe:

http://ftp.aset.psu.edu/pub/ger/for...dk/machparm.f90

Regards.
Skip Knoble

On Mon, 24 Mar 2008 03:49:59 -0700 (PDT), utab <umut.tabak@gmail.com> wrote:

-|Dear all,
-|
-|I was reading about the portability of Fortran programs on different
-|platforms by using the SELECTED_INT_KIND and ..._REAL
-|
-|Using the SELECTED_INT_KIND(4) will be used for integer values that
-|are in
-|the range 10^-4 to 10^4
-|
-|However the below program is used to take a look at the default kind
-|values returned by the KIND function
-|
-|PROGRAM ch0805
-|IMPLICIT NONE
-|INTEGER :: i
-|INTEGER(selected_int_kind(4)) :: j
-|REAL :: r
-|CHARACTER*1 :: c
-|LOGICAL :: l
-|COMPLEX ::
-|cp
-|-4 4
-| PRINT *,' Integer ',KIND(i) ! on my machine by default 4, it
-|means 10 to 10
-| PRINT *,' Real ',KIND(r)
-| PRINT *,' Char ',KIND(c)
-| PRINT *,' Logical ',KIND(l)
-| PRINT *,' Complex ',KIND(cp)
-| i=1000000
-| j=1000_4
-| print *, i, j
-|END PROGRAM ch0805
-|
-|
-|the value of i is larger then the default range of integer values.
-|Even if I assign "100000000" to i, it still is valid and prints out
-|the value. I guess I am missing sth important and some clarification
-|is appreciated. On the j case, it is not ok to assign for instance
-|100000 to the j variable because it is already constrained in the
-|specification part.
-|
-|If I apply this as well, I am getting with ifort
-|
-|fortcom: Warning: ch0805.f90, line 15: The INTEGER(KIND=2) value is
-|out-of-range. [100000]
-| j=100000
-|----^
-| and the output is
-| Integer 4
-| Real 4
-| Char 1
-| Logical 4
-| Complex 4
-| 100000000 -31072

Richard Maine

2008-03-24, 7:27 pm

I'm not sure what this question has to do with KIND. Although there is
some use of KIND in the prigram, none of it has to do with i, which is
what you were asking about.

utab <umut.tabak@gmail.com> wrote:

> INTEGER :: i

....
> i=1000000


> the value of i is larger then the default range of integer values.
> Even if I assign "100000000" to i, it still is valid and prints out
> the value. I guess I am missing sth important and some clarification
> is appreciated.


Seems to me that the most important clarification is one of arithmetic.
Namely, no 1000000 is *NOT larger than the default range for integer.
Neither is 100000000. The default range for integers on most compilers
goes up to 2**31-1, which is slighly over 2 billion. The largest of the
numbers you show is only 100 million.

The second point is that, even if you did have a number that was out of
range, this would not be among the kinds of errors that the compiler is
required to catch. Just because something compile, and even runs, and
even produces the expected output, that does not necessarily mean that
it is "valid."

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com