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

Username:   Password: 
 RegisterRegister   
 Overflow Problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
synopsis




PostPosted: Thu Jun 18, 2015 10:32 am   Post subject: Overflow Problem

What is it you are trying to achieve?
Trying to fix the overflow problem where the answer is outside the maxnat limits.

Basically the program is solving the Chess Board Problem https://en.wikipedia.org/wiki/Wheat_and_chessboard_problem but when I enter in a number greater than 30 it will crash since the number is greater than the maxnat. But if I change it to a real number it will accept decimals (0.5,etc.) which is not supposed to be allowed. I'm looking for a solution to limit the reply to under 31. Any help is appreciated!


What is the problem you are having?
Overflow problem.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:

var reply : string
var grains, grainstotal : real
var num : nat
loop
    loop
        cls
        put "This program determines the amount of grains of wheat on each chessboard tile"
        put "In order to determine the amount, how many squares are on your chessboard?"
        put "Keep in mind the program crashes if you enter a number greater than 30 due"
        put "to Turing's instability of integers and natural numbers."
        get reply
        if strnatok (reply) then
            num := strnat (reply)
            exit
        else
            put "Please enter a valid integer number greater than 0 and less than 31."
            delay (1000)
        end if
    end loop
    grainstotal := 2 ** num - 1
    put "Square          Grains"
    put "0               0"
    for i : 1 .. num
        put i, "          ", 2 ** (i - 1)
    end for
    put "The total sum of the grains on the chessboard is ", grainstotal
    put "A fun fact! The amount of grains on the 64th chessboard tile would be 9223372036854775808!"
    put "And the total number of grains would be 18446744073709551615 grains!"
    put "Press Q to quit or enter any letter if you wish to continue"
    get reply
    exit when reply = "Q" or reply = "q"
end loop

put "Thank you for using this program! Goodbye!"
delay (1000)
cls


Please specify what version of Turing you are using
Turing 4.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Jun 18, 2015 3:51 pm   Post subject: RE:Overflow Problem

Swap nat for int. I've never found a reason to use the nat type. In fact, you should probably not use it again.
DemonWasp




PostPosted: Thu Jun 18, 2015 5:51 pm   Post subject: RE:Overflow Problem

Unfortunately, Turing doesn't come with a numeric type that can represent numbers that large (Turing only supports up to 32-bit integers). If you were to try to compute the value for the complete chessboard, you would need to be able to store 2 ^ 64 ; the largest data type Turing supports only goes up to 2 ^ 32 - 1.

There is no easy way around this. You would have to write code to compute integer values larger than what you get for-free as part of the language, or else just write the values into your code like you did in the put block near the end.

@Insectoid: nat is just unsigned int. OP was correct to use that data type here, even if it didn't help much.
Dreadnought




PostPosted: Thu Jun 18, 2015 11:17 pm   Post subject: Re: Overflow Problem

Well, If you did use real you could get up to tile 53 without problem.

But 53 is still a pretty small number. As DemonWasp says you would have to write code to compute the values yourself. And its actually quite easy to do in this case. You will need the ability to, given the digits of a number (in some string or array), produce the digits of that number multiplied by 2 and the ability to subtract 1 from such a number.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: