Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.fortran

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-15-2012, 02:31 PM
analyst41@hotmail.com
Guest
 
Posts: n/a
Default weird lahey bug

subroutine test
do j = 1,huge(0)
if (j > 10) stop
print *, j
enddo
stop
end


1
2
3
4
5
6
7
8
9
10
Program Completed
Press Enter to Continue.

subroutine test
if (0< huge(0)) print *, ' zero less than huge(0)'
do j = 0,huge(0)
if (j > 10) stop
print *, j
enddo
stop
end


zero less than huge(0)
Program Completed
Press Enter to Continue.

In other words, If I try to run the loop from 0 to huge(0) it never
enters the loop.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-15-2012, 02:48 PM
Louisa
Guest
 
Posts: n/a
Default Re: weird lahey bug

On Apr 16, 12:31*am, "analys...@hotmail.com" <analys...@hotmail.com>
wrote:
> * * * subroutine test
> * * * do j = 1,huge(0)
> * * * if (j > 10) stop
> * * * print *, j
> * * * enddo
> * * * stop
> * * * end
>
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> Program Completed
> Press Enter to Continue.
>
> * * * subroutine test
> * * * if (0< huge(0)) print *, ' zero less than huge(0)'
> * * * do j = 0,huge(0)
> * * * if (j > 10) stop
> * * * print *, j
> * * * enddo
> * * * stop
> * * * end
>
> *zero less than huge(0)
> Program Completed
> Press Enter to Continue.
>
> In other words, If I try to run the loop from 0 to huge(0) it never
> enters the loop.


Have you tried running this as a program?
i.e.,
program test
Reply With Quote
  #3 (permalink)  
Old 04-15-2012, 02:50 PM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: weird lahey bug

analyst41@hotmail.com <analyst41@hotmail.com> wrote:

(snip)
> subroutine test
> if (0< huge(0)) print *, ' zero less than huge(0)'
> do j = 0,huge(0)
> if (j > 10) stop
> print *, j
> enddo
> stop
> end


> zero less than huge(0)
> Program Completed
> Press Enter to Continue.


> In other words, If I try to run the loop from 0 to huge(0) it never
> enters the loop.


As far as I know, that is allowed.

The loop count, huge(0)-0+1, doesn't fit in the appropriate sized
variable.

You can also try

do j=huge(0)-10,huge(0)

which might also fail.

-- glen
Reply With Quote
  #4 (permalink)  
Old 04-15-2012, 02:52 PM
dpb
Guest
 
Posts: n/a
Default Re: weird lahey bug

On 4/15/2012 9:31 AM, analyst41@hotmail.com wrote:
....

> subroutine test
> if (0< huge(0)) print *, ' zero less than huge(0)'
> do j = 0,huge(0)
> if (j> 10) stop
> print *, j
> enddo
> stop
> end
>
>
> zero less than huge(0)
> Program Completed
> Press Enter to Continue.
>
> In other words, If I try to run the loop from 0 to huge(0) it never
> enters the loop.


C:\Temp> df /nologo test.f90
test.f90
test.f90(4) : Warning: Overflow occurred while evaluating constant
expression.
do i=0,huge(0)
--^

C:\Temp>

CVF 6.6c diagnoses a problem. I'll have to scratch head as to why,
precisely; it doesn't come to me otomh why the case w/ the lower
bound==1 is ok but 0 isn't (but I'm sure it'll be obvious when it does )

--

Reply With Quote
  #5 (permalink)  
Old 04-15-2012, 02:54 PM
dpb
Guest
 
Posts: n/a
Default Re: weird lahey bug

On 4/15/2012 9:31 AM, analyst41@hotmail.com wrote:
....

> In other words, If I try to run the loop from 0 to huge(0) it never
> enters the loop.


Yes, it occurred to me just as a pressed "SEND"

The compiler is computing the loop count and that count overflows by
being HUGE+1, not HUGE

--

Reply With Quote
  #6 (permalink)  
Old 04-15-2012, 02:59 PM
dpb
Guest
 
Posts: n/a
Default Re: weird lahey bug

On 4/15/2012 9:54 AM, dpb wrote:
> On 4/15/2012 9:31 AM, analyst41@hotmail.com wrote:
> ....
>
>> In other words, If I try to run the loop from 0 to huge(0) it never
>> enters the loop.

>
> Yes, it occurred to me just as a pressed "SEND"
>
> The compiler is computing the loop count and that count overflows by
> being HUGE+1, not HUGE


BTW,

program test
integer*8 :: i
do i=1,huge(0)
write(*,*) i
if (i>3) stop
enddo
end

C:\Temp> df /nologo test.f90
test.f90

C:\Temp> test
1
2
3
4

C:\Temp>

hth...

--

Reply With Quote
  #7 (permalink)  
Old 04-15-2012, 03:03 PM
dpb
Guest
 
Posts: n/a
Default Re: weird lahey bug

On 4/15/2012 9:59 AM, dpb wrote:
....

> BTW,
>
> program test
> integer*8 :: i
> do i=1,huge(0)

....

Which also works (of course) w/ the intended demonstration case of

program test
integer*8 :: i
do i=0,huge(0)

--
Reply With Quote
  #8 (permalink)  
Old 04-15-2012, 03:18 PM
analyst41@hotmail.com
Guest
 
Posts: n/a
Default Re: weird lahey bug

On Apr 15, 10:31*am, "analys...@hotmail.com" <analys...@hotmail.com>
wrote:
> * * * subroutine test
> * * * do j = 1,huge(0)
> * * * if (j > 10) stop
> * * * print *, j
> * * * enddo
> * * * stop
> * * * end
>
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> Program Completed
> Press Enter to Continue.
>
> * * * subroutine test
> * * * if (0< huge(0)) print *, ' zero less than huge(0)'
> * * * do j = 0,huge(0)
> * * * if (j > 10) stop
> * * * print *, j
> * * * enddo
> * * * stop
> * * * end
>
> *zero less than huge(0)
> Program Completed
> Press Enter to Continue.
>
> In other words, If I try to run the loop from 0 to huge(0) it never
> enters the loop.


Thanks all. 0,huge(0) - 1 did the trick.
Reply With Quote
  #9 (permalink)  
Old 04-15-2012, 04:17 PM
Ron Shepard
Guest
 
Posts: n/a
Default Re: weird lahey bug

In article <jmeo0h$l09$4@speranza.aioe.org>, dpb <none@non.net>
wrote:

> On 4/15/2012 9:59 AM, dpb wrote:
> ...
>
> > BTW,
> >
> > program test
> > integer*8 :: i
> > do i=1,huge(0)

> ...
>
> Which also works (of course) w/ the intended demonstration case of
>
> program test
> integer*8 :: i
> do i=0,huge(0)


I don't know how the latest languages standards have addressed this
issue, but with f90 and f95 it was not necessary for this version to
work either. The reason is that the trip count was often computed
or stored with default precision, regardless of the KINDs of the
loop index, lower bound, upper bound, or increment. I remember
testing several compilers in the late 1990's and early 2000's with
variations of

integer(i8) :: i, i1, i2
i1 = -huge(0_i4) ! or = 0
i2 = huge(0_i4)
do i = i1, i2


and they did not work correctly. Even with the trip count evaluated
entirely with 64-bit arithmetic, the counter itself was still stored
in a 32-bit register, which defeated the purpose of the exercise.
sometimes it was not the trip count that was the problem it was the
result of the trip count being optimized away in favor of one of the
address ranges of an array, so the limit was related to the
underlying address limitations of the hardware.

Some of the compilers had options to change the default integer
kind, and this did fix the problem in some cases but not others.
This was during the time when addressing was done with 32 bits, or
when extended addressing was first available in hardware but the OS
did not yet fully support it (e.g. MacOSX in 2003). Since it was
not required to work by the standard, the compiler vendors could
claim conformance and that this was a quality of implementation
issue.

I do not know if or how the later standard revisions addressed this
issue, but I do know that both hardware and OS support for extended
addressing is more common now, and it would surprise me if there
were not some kind of workaround to achieve the above on most of the
currently supported compilers. It might require using nondefault
KINDS for the trip count computation, or it might require changing
the default kind with a compiler option, but there almost certainly
is some way to get the above to work now.

It would sort of surprise me if simply changing the KIND of the loop
index alone is sufficient, but I could see how some compilers might
use that as a guideline. It is really the stuff on the *other* side
of the equals sign (lower and upper bound and the increment) that
would seem to be the most important in this process. The loop index
itself is often just optimized away (e.g. if it is not used in a
complicated expression or as an actual argument or something).

$.02 -Ron Shepard
Reply With Quote
  #10 (permalink)  
Old 04-15-2012, 06:04 PM
dpb
Guest
 
Posts: n/a
Default Re: weird lahey bug

On 4/15/2012 11:17 AM, Ron Shepard wrote:
....

> It would sort of surprise me if simply changing the KIND of the loop
> index alone is sufficient, but I could see how some compilers might
> use that as a guideline. It is really the stuff on the *other* side
> of the equals sign (lower and upper bound and the increment) that
> would seem to be the most important in this process. The loop index
> itself is often just optimized away (e.g. if it is not used in a
> complicated expression or as an actual argument or something).


Yeah, on retrospect I shouldn't have said "of course" in the comment on
how CVF handles it as there's no reason necessarily the count
computation needed to have been in the extended precision. But, it
appears that was what the developers did in that case (use the KIND of
the index variable as a hint, that is).

--

Reply With Quote
  #11 (permalink)  
Old 04-15-2012, 06:06 PM
dpb
Guest
 
Posts: n/a
Default Re: weird lahey bug

On 4/15/2012 10:18 AM, analyst41@hotmail.com wrote:
....

> Thanks all. 0,huge(0) - 1 did the trick.


You don't mean you actually _use_ that construct, do you!!!???

Surely there's a "more better" way (for what I presume is essentially
intended as a DO FOREVER construct until a condition of some sort breaks
it)?

--
Reply With Quote
  #12 (permalink)  
Old 04-15-2012, 10:23 PM
analyst41@hotmail.com
Guest
 
Posts: n/a
Default Re: weird lahey bug

On Apr 15, 2:06*pm, dpb <n...@non.net> wrote:
> On 4/15/2012 10:18 AM, analys...@hotmail.com wrote:
> ...
>
> > Thanks all. 0,huge(0) - 1 did the trick.

>
> You don't mean you actually _use_ that construct, do you!!!???
>
> Surely there's a "more better" way (for what I presume is essentially
> intended as a DO FOREVER construct until a condition of some sort breaks
> it)?
>
> --


cleanest way to count records from an external file. When you get out
the loop variable equals the number you read.
Reply With Quote
  #13 (permalink)  
Old 04-15-2012, 10:30 PM
Terence
Guest
 
Posts: n/a
Default Re: weird lahey bug

Try this change to narrow the problem down.
I have Lahey, but haven't used it for years, so I don't know what to expect
from 'huge(0)'.
A pure guess, but I suspect the original code would work if 'huge (0)' was
replaced by 99.

subroutine test
if (0< huge(0)) then
print *, ' zero less than huge(0)'
else
do j = 0,huge(0)
if (j > 10) stop
print *, j
enddo
endif
stop
end





Reply With Quote
  #14 (permalink)  
Old 04-15-2012, 10:35 PM
Terence
Guest
 
Posts: n/a
Default Re: weird lahey bug

Oops! More replies came in while I was researching it.
So I see the problem is the storing of 'huge(0)' and adding 1 to it and
getting either an overflow or a negative number result.


Reply With Quote
  #15 (permalink)  
Old 04-16-2012, 12:11 AM
Richard Maine
Guest
 
Posts: n/a
Default Re: weird lahey bug

analyst41@hotmail.com <analyst41@hotmail.com> wrote:

> On Apr 15, 2:06 pm, dpb <n...@non.net> wrote:
> > On 4/15/2012 10:18 AM, analys...@hotmail.com wrote:
> > ...
> >
> > > Thanks all. 0,huge(0) - 1 did the trick.

> >
> > You don't mean you actually _use_ that construct, do you!!!???
> >
> > Surely there's a "more better" way (for what I presume is essentially
> > intended as a DO FOREVER construct until a condition of some sort breaks
> > it)?


> cleanest way to count records from an external file. When you get out
> the loop variable equals the number you read.


Well, to each his own on the definition of "cleanest". To me, having an
arbitrary loop upper bound like huge(0) seems far less clean than what I
do, which is to use an actual "do forever" loop, having a record count
separate from any loop index, as in

i = 0
read_loop: do
read(...,iostat=iostat) ...
if (iostat /= 0) exit read_loop
... do any desired processing of the record data here.
end do read_loop

That doesn't introduce arbitrary loop bounds like huge(0). To me that
huge(0) is pretty "unclean" in that it's actual value presumably doesn't
matter. After all, if you ever actually hit that upper bound, something
has gone wrong. That's before getting into the fact that it is prone to
overflow problems such as the one you hit.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 07:36 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.