Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 "Overflow in Integer expression"- how do i fix this?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
riveryu




PostPosted: Sat Feb 16, 2008 4:24 pm   Post subject: "Overflow in Integer expression"- how do i fix this?

I wrote an answer program to a question that says -- user inputs numbers from 1 - 30 , program outputs the factorials( defined 1x2x4x5 .. the number) .

I found that when the user enters anything bigger than 12! = 479 001 600 will result in "Overflow in Integer expression" (cuz Turing suckz...). How do i fix this problem?
( I dont need an answer to the question, i need a solution to the integer overflow problem)

Maybe my teacher asked too much, since im only supposed to have learned about loops, for, var :int, real, nat, string, cls, put get... etc. No arrays, fcns o proc etc...
Anyways, juz letting u know; assuming if this have anything to do with solving the problem.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Sat Feb 16, 2008 4:28 pm   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

The problem is that 13! is greater than 2^31 - 1, which is the upper limit to integers in Turing. If you want anything bigger, you're going to have to start thinking outside the box I'm afraid.
BigBear




PostPosted: Sat Feb 16, 2008 4:29 pm   Post subject: Re: "Overflow in Integer expression"- how do i fix this?

Well if you post your code we might be able to tell why it is happening.

I don't remember even having that error.
Saad




PostPosted: Sat Feb 16, 2008 4:30 pm   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

You can use real's for 30! since reals have a bigger range, however you will loose precision in the number
BigBear




PostPosted: Sat Feb 16, 2008 4:43 pm   Post subject: Re: "Overflow in Integer expression"- how do i fix this?

What is the difference ?
Reals and every number and Integers and only whole numbers and since he is working with whole number integers should work.
Clayton




PostPosted: Sat Feb 16, 2008 4:59 pm   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

Try the following two programs:

Turing:
fcn fact (num : int) : real
    if num <= 1 then
        result 1
    else
        result num * fact (num - 1)
    end if
end fact

put fact (13)


That one was with reals.

Turing:
fcn fact (num : int) : int
    if num <= 1 then
        result 1
    else
        result num * fact (num - 1)
    end if
end fact

put fact (13)


That one was with ints. Notice the only difference causes one to fail, and the other to still run?
Saad




PostPosted: Sat Feb 16, 2008 5:11 pm   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

Unlike in like life, a computer cannot store an infinite number of digits, this is dependant on the number of bytes you have and how its stored.

An integer is stored in a regular binary format. The defined size of an integer is 4 bytes giving us the range of -2 ^ 31 and 2^31 - 1.

However a real number is stored in a different way. I'm not sure how they're represent in turing, but in general its stored in scientific notation. Which allows for a greater range
OneOffDriveByPoster




PostPosted: Sat Feb 16, 2008 5:52 pm   Post subject: Re: "Overflow in Integer expression"- how do i fix this?

Teachers in certain subjects do tend to make assignments that are more involved than they expected. You could try doing multiplication using strings.
riveryu




PostPosted: Sat Feb 16, 2008 6:05 pm   Post subject: Re: "Overflow in Integer expression"- how do i fix this?

I am aware that you can solve this question with recursive function, which u can use real numbers.
However, heres the cache to this problem, i can only use forloops, therefore only integers.

i will post my code here to show u :

Turing:
%user inputs number, outputs factorial, user can choose when to quit
% user can only go up to 12!, since the numbers are too large after 12!.
% 30! is 2.6525286x10**32, which is quite large

var num : int  %input number
var fac : int := 1 %factorial
var ans : string
loop
    put "Enter a number for its factorial."
    get num
    exit when num > 30
    for i : 1 .. num
        cls
        fac := i * fac
        % everytime it loops, i is multiplied with the previous result
        % since i timing 1 is equal to i at first
        % this is equivalent to i timing it self
        % everytime the it loops, 1*2*3*4.. number
       
        put num, "! = ", fac
         delay ( 200) % this is here to show u the problem in slow motion, error comes after 9 digits
    end for
    fac := 1 %changes fac back to 1 to clear the  amount acuminated earlier
    put "Do you wish to find out more factorials? Y/N"
    get ans
    exit when ans = "N" or ans = "n"
end loop


*cough* Im "beginner" cuz i juz started grade 10 programming for 2 weeks, im not suppose to know about fcns and procs *cough*
zylum




PostPosted: Sat Feb 16, 2008 6:13 pm   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

why can you only use integers in for loops?? try changing your fac variable to a real.. It wont give you an exact answer but at least it wont crash.
riveryu




PostPosted: Sat Feb 16, 2008 6:46 pm   Post subject: Re: "Overflow in Integer expression"- how do i fix this?

O, oops...Embarassed
Thx zylumm, Clayton and Saad, and ppl.
I misunderstood something in message the Turing debugger gave me while i was adjusting the program which messed me up... and got the impression that u can't use int for the forloop.
Sry

Im not sure if this is the place to say this but...

Pehaps this problem should be addressed in one of the tutorials, as in the difference of real and int in respect to scientific notation

The Turing Walkthrough only mentioned this:

Real Numbers:
Real numbers can contain decimal places. Turing supports up to 16 decimal places for real numbers. Real numbers contain the realms of the positive, the negative, and zero.

Integers:
We should all know what an integer is, given some fundamental math. Just to make sure, an integer is a whole number; it has no decimals; it is a fraction whose denominator is 1; it can be negative, zero, or positive.

Maybe this is too obvious for some ppl but i dont think it is for beginners who nvr touched a programming language before, espicially for ppl who learn Turing since Ontario (CA) use Turing for all grade 10 - the first standard CS(not Counter Strike) course a student encounters.
LaZ3R




PostPosted: Sun Feb 17, 2008 11:54 am   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

Real can hold values up to 306 exponents at most, using it for extremely large numbers is your best bet. Though you won't be able to display every digit (without thinking outside the box), you at least won't get an overflow.
riveryu




PostPosted: Sun Feb 17, 2008 1:41 pm   Post subject: Re: "Overflow in Integer expression"- how do i fix this?

Im not very familiar with the meanig of "outside the box", I think thats sort too broad for some ppl... I played around with Clayton's functions - is this one the things outside that box, ? (look at last line in the code)

Turing:
fcn fact (num : real) : real
    if num <= 1 then
        result 1
    else
        result num * fact (num - 1)
    end if
end fact

put fact (30) : 0 : 0 % making turing display decimals, simultaneously displaying all numbers preceding the decimals
                      % the first : 0 means your not trying to assign space
                      % the second is assigning decimal places for Turing to display, usually used for rounding though.


Is there any other things outside the box, plz tell or r u not allowed?(im new here to the forum)
Mackie




PostPosted: Sun Feb 17, 2008 2:16 pm   Post subject: RE:"Overflow in Integer expression"- how do i fix this?

Outside the box is a saying. It just means to think unconventionally.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: