| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I've got a program that's getting to be 250 lines and has growing pains. I'm starting to see errors that have everything to do with reaching inappropriate values for an sp integer or real. It's been a while since I've turned a program from sp to dp, and I wanted to check my premises. First, does this INTEGER, PARAMETER :: DP = KIND(1.0D0) ! define DP give dp for all of the following: integer(kind=dp), parameter:: trials = 300000 integer (kind=dp), dimension(trials)::F real (kind=dp):: harvest, tot, t1, t2, diff, t3, t4 ? Second, the place where the program runs into its first runtime as a function of the integer parameter trials is here: tot= sum(D) , where tot is a default real and D has size 300,000. This seems absurd to me, as I would believe that LHS is unable to match RHS. Third, the strategy whereby I turn *everything* dp seems to be a real loser. What is a portable way to declare an sp integer? -- Unquestionably, there is progress. The average American now pays out twice as much in taxes as he formerly got in wages. 1 H. L. Mencken |
|
#2
| |||
| |||
| Ron Ford wrote: > It's been a while since I've turned a program from sp to dp, and I wanted > to check my premises. First, does this > INTEGER, PARAMETER :: DP = KIND(1.0D0) ! define DP yes, as an appropriate KIND for REAL variables. > give dp for all of the following: > integer(kind=dp), parameter:: trials = 300000 > integer (kind=dp), dimension(trials)::F No, DP (or dp) should not be used for integer variables and parameters. > real (kind=dp):: harvest, tot, t1, t2, diff, t3, t4 Yes, like that. (snip) > Third, the strategy whereby I turn *everything* dp seems to be a real > loser. What is a portable way to declare an sp integer? It would be rare that you would need to change both INTEGER and REAL variables to double precision at the same time. The one case I can think of, is with EQUIVALENCE or COMMON if you needed them to always be the same size. KIND(0) will be the appropriate KIND for default (single precision) integer. (Though integer data is not usually described as single and double precision.) There may not be a standard way to generate a double length integer variable. -- glen |
|
#3
| |||
| |||
| glen herrmannsfeldt wrote: (snip) > KIND(0) will be the appropriate KIND for default > (single precision) integer. (Though integer data is > not usually described as single and double precision.) > > There may not be a standard way to generate a double > length integer variable. what about using selected_int_kind? $ cat int.f90 program int implicit none integer :: i do i = 1,20 print *, i, selected_int_kind(i) end do end program int This should show you what you need for a double length integer. Here, it gives: 1,2,4, and 8. 4 is the one you get with a default integer. Wim > > -- glen |
|
#4
| |||
| |||
| wim wrote: (I wrote) >>KIND(0) will be the appropriate KIND for default >>(single precision) integer. (Though integer data is >>not usually described as single and double precision.) >>There may not be a standard way to generate a double >>length integer variable. > what about using selected_int_kind? > $ cat int.f90 > program int > implicit none > integer :: i > do i = 1,20 > print *, i, selected_int_kind(i) > end do > end program int > This should show you what you need for a double length integer. > Here, it gives: 1,2,4, and 8. 4 is the one you get with a default > integer. That shows you something longer than a default integer, but doesn't guarantee it is twice as long. (A system might have integers of 16, 32, and 48 bits, for example.) C_SIZEOF(), when available, might be the only way to find the actual amount of storage taken up by a variable (including padding bits internal to the variable, or that are necessary between such variables). -- glen |
|
#5
| |||
| |||
| On Aug 26, 9:06*pm, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote: > wim wrote: > > (I wrote) > > > > > > >>KIND(0) will be the appropriate KIND for default > >>(single precision) integer. *(Though integer data is > >>not usually described as single and double precision.) > >>There may not be a standard way to generate a double > >>length integer variable. > > what about using selected_int_kind? > > $ cat int.f90 > > program int > > * implicit none > > * integer :: i > > * do i = 1,20 > > * * print *, i, selected_int_kind(i) > > * end do > > end program int > > This should show you what you need for a double length integer. > > Here, it gives: 1,2,4, and 8. 4 is the one you get with a default > > integer. > > That shows you something longer than a default integer, > but doesn't guarantee it is twice as long. *(A system > might have integers of 16, 32, and 48 bits, for example.) This is from the point of view of an engineer: Why do you want to know how much bits an integer occupies? Is it not a lot more useful to know the resulting numerical range that can be represented with the integer? > C_SIZEOF(), when available, might be the only way to find > the actual amount of storage taken up by a variable > (including padding bits internal to the variable, or > that are necessary between such variables). > > -- glen |
|
#6
| |||
| |||
| On Aug 25, 6:10*pm, Ron Ford <r...@example.invalid> wrote: > It's been a while since I've turned a program from sp to dp, and I wanted > to check my premises. *First, does this > INTEGER, PARAMETER :: DP = KIND(1.0D0) * *! define DP > give dp for all of the following: > integer(kind=dp), parameter:: trials = 300000 > integer (kind=dp), dimension(trials)::F > real (kind=dp):: harvest, tot, t1, t2, diff, t3, t4 I'm not sure exactly why you're concerned about single vs. double precision integers. However, there's a nice compiler option which I often use which forces reals to be double. Using gfortran, the options are: -fdefault-double-8 -fdefault-integer-8 -fdefault-real-8 I then just declare reals and then can switch between single and double at compile-time. |
|
#7
| |||
| |||
| On Tue, 26 Aug 2008 02:45:44 -0700 (PDT), wim posted: > glen herrmannsfeldt wrote: > > (snip) > >> KIND(0) will be the appropriate KIND for default >> (single precision) integer. (Though integer data is >> not usually described as single and double precision.) >> >> There may not be a standard way to generate a double >> length integer variable. > > what about using selected_int_kind? > > $ cat int.f90 > program int > implicit none > integer :: i > do i = 1,20 > print *, i, selected_int_kind(i) > end do > end program int > > This should show you what you need for a double length integer. > Here, it gives: 1,2,4, and 8. 4 is the one you get with a default > integer. Thanks, wim, I did this recently without thinking that I needed biggish integers, so didn't pay too much attention. For my machine, this output is: 1 1 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 3 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 -1 20 -1 Press RETURN to close window . . . Is 4 my default as well? It's confusing, because 4 could mean different things here. I'm stumbling on the implementation, as it is different than for reals. Don't I need to wrtie an executable staement to figure this out, such as: mp =selected_int_kind(13) If I execute a statement, I can't thereafter declare: integer (kind=mp) :: anything Does anyone know a portable way to declare, populate and print an integer with thirteen digits? -- Wealth - any income that is at least one hundred dollars more a year than the income of one's wife's sister's husband. 6 H. L. Mencken |
|
#8
| |||
| |||
| In article <1ekmpg9sbb6l6.dlg@example.invalid>, Ron Ford <ron@example.invalid> wrote: > > Don't I need to wrtie an executable staement to figure this out, such as: > mp =selected_int_kind(13) > > > If I execute a statement, I can't thereafter declare: > integer (kind=mp) :: anything > > Does anyone know a portable way to declare, populate and print an integer > with thirteen digits? I don't understand the above sequence of sentences. selected_int_kind(13) does what you want in a portable way, provided the compiler supports an integer that long. If your compiler does not support integers with 13 digits, then what you want to do is not portable to that compiler. integer, parameter :: i13 = selected_int_kind(13) integer(i13) ::long_int_with_at_least_13_digits long_int_with_at_least_13_digits = 1234567890123_i13 write(*,'(i14)') long_int_with_at_least_13_digits Of course, you are free to change the variable names. If your compiler does not support 13 digit integers, then the above results in an error at compile time, not run time. $.02 -Ron Shepard |
|
#9
| |||
| |||
| On Tue, 26 Aug 2008 20:34:34 -0500, Ron Shepard posted: > In article <1ekmpg9sbb6l6.dlg@example.invalid>, > Ron Ford <ron@example.invalid> wrote: > >> >> Don't I need to wrtie an executable staement to figure this out, such as: >> mp =selected_int_kind(13) >> >> >> If I execute a statement, I can't thereafter declare: >> integer (kind=mp) :: anything >> >> Does anyone know a portable way to declare, populate and print an integer >> with thirteen digits? > > I don't understand the above sequence of sentences. > selected_int_kind(13) does what you want in a portable way, provided > the compiler supports an integer that long. If your compiler does > not support integers with 13 digits, then what you want to do is not > portable to that compiler. > > integer, parameter :: i13 = selected_int_kind(13) > integer(i13) ::long_int_with_at_least_13_digits > long_int_with_at_least_13_digits = 1234567890123_i13 > write(*,'(i14)') long_int_with_at_least_13_digits > > Of course, you are free to change the variable names. If your > compiler does not support 13 digit integers, then the above results > in an error at compile time, not run time. > > $.02 -Ron Shepard Thanks, Ron, I'm past the first part of this now. I've never really cranked up on this side of fortran as I did with likewise constraints with C. That I made it to fifty thousand without nuancing with data types speakes to the strengths of the syntax that is fortran. 1 1 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 3 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 -1 20 -1 1234567890123 i13= 4 Press RETURN to close window . . . program int implicit none integer, parameter :: i13 = selected_int_kind(13) integer(i13) ::long_int_with_at_least_13_digits integer :: i do i = 1,20 print *, i, selected_int_kind(i) end do long_int_with_at_least_13_digits = 1234567890123_i13 write(*,'(i14)') long_int_with_at_least_13_digits print *, "i13= ", i13 end program int I think this is a counterexample to the notion that 4 might be the default int for conforming implementations. Tjas, -- We are here and it is now. Further than that, all human knowledge is moonshine. 3 H. L. Mencken |
|
#10
| |||
| |||
| In article <19u1j7r8by3vv.dlg@example.invalid>, Ron Ford <ron@example.invalid> wrote: > I think this is a counterexample to the notion that 4 might be the default > int for conforming implementations. Yes, integer kind 4 is definitely not specified by the fortran standard in any way, it is not even required to correspond to any integer kind at all. That is what the selected_xxx_kind() intrinsics are for. $.02 -Ron Shepard |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.