Computer Science Canada "Overflow in Integer expression"- how do i fix this? |
Author: | riveryu [ 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. |
Author: | Clayton [ 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. |
Author: | BigBear [ 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. |
Author: | Saad [ 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 |
Author: | BigBear [ 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. |
Author: | Clayton [ Sat Feb 16, 2008 4:59 pm ] | ||||
Post subject: | RE:"Overflow in Integer expression"- how do i fix this? | ||||
Try the following two programs:
That one was with reals.
That one was with ints. Notice the only difference causes one to fail, and the other to still run? |
Author: | Saad [ 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 |
Author: | OneOffDriveByPoster [ 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. |
Author: | riveryu [ 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 :
*cough* Im "beginner" cuz i juz started grade 10 programming for 2 weeks, im not suppose to know about fcns and procs *cough* |
Author: | zylum [ 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. |
Author: | riveryu [ Sat Feb 16, 2008 6:46 pm ] |
Post subject: | Re: "Overflow in Integer expression"- how do i fix this? |
O, oops... ![]() 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. |
Author: | LaZ3R [ 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. |
Author: | riveryu [ 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)
Is there any other things outside the box, plz tell or r u not allowed?(im new here to the forum) |
Author: | Mackie [ 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. |