forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Integer Arithmetic Overflow

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Plato
View previous topic :: View next topic  
Author Message
Honore



Joined: 03 May 2011
Posts: 3

PostPosted: Fri May 13, 2011 3:11 pm    Post subject: Integer Arithmetic Overflow Reply with quote



To prove that the roots of the above equation are x=2 and y=3, I wrote the following simple program and it works fine on a 64-bit Laptop-Ubuntu 10.10 with gfortran 95 compiler. In about 5 minutes or so, the compiler gives the results. However, Salford FTN95 Compiler (Plato3 IDE) on a 32-bit Desktop with Win XP/SP3 generates a run-time error stating "Integer Arithmetic Overflow" both at line 4 and 6.

Code:
      INTEGER X, Y
      DOUBLE PRECISION A, B
      DO 20 X=32767,1,-1
          A=X**(X**(X**X))+74
          DO 10 Y=32767,1,-1
             B=(19-Y**X)*(Y**(X**Y))
             IF (A-B) 10,30,10
   10 CONTINUE
   20 CONTINUE
   30 WRITE (6, 40) X,Y
   40 FORMAT(//'X=',I2,2X,'Y=',I2)       
      END


I would appreciate any suggestions or comments. Thanks and regards.
Back to top
View user's profile Send private message
brucebowler
Guest





PostPosted: Fri May 13, 2011 3:46 pm    Post subject: Reply with quote

How big is 32767**32767**32767**32767???

You might try real(x)**real(x)**real(x)**real(x)+74.0d0
Back to top
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Fri May 13, 2011 3:48 pm    Post subject: Reply with quote

Are you sure?

How can any compiler handle

X**(X**(X**X)) when X = 32767.

For 32 bit integers you get overflow at about X = 3 I think.
Back to top
View user's profile Send private message AIM Address
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Fri May 13, 2011 4:28 pm    Post subject: Reply with quote

Hehehe, sure sign it's Friday Very Happy

Honore, was the question "Prove that the roots of ... are ..."? In which case, you just need to compute the expression for x = 2 and y = 3 and show it is equal to 0.

If the question was "Find the roots of ...", then you need to search for integer solutions as you have done. But Paul is right, iterated exponentiation gets phenomenally large very quickly as the argument increases. I have no idea how your program worked before, I can only imagine that it was failing to report every overflow until it found something that made computational sense near the end of the double do loop Rolling Eyes
Back to top
View user's profile Send private message Send e-mail
Honore



Joined: 03 May 2011
Posts: 3

PostPosted: Fri May 13, 2011 4:53 pm    Post subject: Reply with quote

Thanks for the comments and critiques. Yes, the question was actually on finding the roots of the equation. The solution by hand is okay and I am trying to prove the solution using Fortran.

I changed the program a bit as follows and the output came out again as expected. What I more wonder is why Plato 3 cannot do some similar optimisation if any required.

Thanks and regards

Back to top
View user's profile Send private message
sparge



Joined: 11 Apr 2005
Posts: 371

PostPosted: Fri May 13, 2011 5:10 pm    Post subject: Reply with quote

I think you are failing to appreciate the mindboggling vastness of the numbers you think you are manipulating with your program.

You have set A and B to be selected_int_kind(18 ). That means they can be as big as 10 with 18 zeros after it (give or take).

How big is 32767**32767**32767**32767? Well, let's set our sights a little lower. How big is 10**10**10**10?

10**10 = 10 with 10 0s after it. That's 10,000,000
So 10**10**10 is 10 with 10**10 = 10,000,000 zeros after it

10,000,000 >>>>>>>>>>> 18
10 <<<<<<<<<<<<<<<< 32,767

Game comprehensively over.

Whatever else your other program is doing, it is not telling you that it is spending most of its time overflowing left right and centre.

Score one elementary point for FTN95. Subtract one whopping point for gfortran 95.
Back to top
View user's profile Send private message Send e-mail
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2388
Location: Yateley, Hants, UK

PostPosted: Fri May 13, 2011 5:39 pm    Post subject: Reply with quote

... and most of the satisfied FTN95 users are quietly congratulating themselves on not using a compiler that lets you make this big a mistake!

Eddie
Back to top
View user's profile Send private message
brucebowler
Guest





PostPosted: Fri May 13, 2011 5:59 pm    Post subject: Reply with quote

Also worth noting that it's an interesting mix of F9x and F2 (not 2xxx, just 2 :-) constructs.
Back to top
Honore



Joined: 03 May 2011
Posts: 3

PostPosted: Fri May 13, 2011 6:08 pm    Post subject: Reply with quote

Thanks for the comments. When I try the program using lower numbers such as 10,000, 5000, 500, etc instead of 32767, the result is generated almost immediately. For this issue, FTN95 is not satisfactory enough for me.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Sat May 14, 2011 3:39 am    Post subject: Reply with quote

Honore,

While you may find FTN95 is not satisfactory enough, I think you need to better understand the limits of real calculations. May I suggest that I find your understanding of real calculations not satisfactory enough.

Try the changed code below:
Code:
   real*10   x,y,a,b
   integer*4 ix,iy
!
      do ix = 200,254
         x = ix /100.0
         A = X**(X**(X**X))+74.0_3
         write (*,*) ix,x,a
      end do
!
      DO iX = 1,3
         x = ix
         A = X**(X**(X**X))+74.0_3
         DO iY = 1,4
            y = iy
            B = (19.0_3-Y**X)*(Y**(X**Y))
            write (*,*) x,a,y,b
         end do
      end do
!             IF (A-B) 10,30,10
!   10 CONTINUE
!   20 CONTINUE
   30 WRITE (*, 40) X,Y
   40 FORMAT(//'X=',f0.1,2X,'Y=',f0.1)       
      END
Back to top
View user's profile Send private message
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2388
Location: Yateley, Hants, UK

PostPosted: Sat May 14, 2011 3:45 pm    Post subject: Reply with quote

Suggest "now satisfactory" instead of "not satisfactory"?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Plato All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group