Legacy GM Incorrect sum?

J

James T. Rex

Guest
So I've got this little script that generates a random integer with an amount of digits entered by the user. I draw out the sum as well as all its constituting components but when the amount of digits is 18 or higher (and sometimes 17 or 16) it doesn't add up correctly - it can be higher or lower; and with even higher digit counts the final few numerals often get locked at 0. Since its only with really high numbers I was wondering if its an integer overflow problem which I wouldn't know how to verify or work around if this was the case.


(script is called gen_digits)
//PREP
no=0;
base=(power(10,argument0))/10;//base becomes a multiplycation of 10 made of digits equaling argument0
ds_list_clear(list_10);

//FIRST DIGIT
temp=irandom_range(1,9)*base;
ds_list_add(list_10,temp);
no+=temp;

//DESCENDING BASES
if argument0>=2
{for(i=argument0-1;i>0;i-=1)
{base/=10;
temp=irandom(9)*base;
ds_list_add(list_10,temp);
no+=temp;
}
}

//end returning no (list ext-established)
return no;


Its being called by an object with the following code:

Create event:
list_10=ds_list_create();
generated_10=0;

Press <ENTER> event:
generated_10=gen_digits(get_integer("No. size in digits",""));

Draw event:
draw_text(10,10,generated_10);
draw_text(1000,10,ds_list_size(list_10));

if ds_list_size(list_10)>0
{for(i=0;i<ds_list_size(list_10);i+=1)
{draw_text(10,25+(i*15),ds_list_find_value(list_10,i));
}
}
 

TheouAegis

Member
I think 2 147 483 647. Is the highest value I have been able to set a variable before issues start occurring. Beyond that, I think game maker handles the highest bit differently depending on the operations.
 
J

James T. Rex

Guest
10^n / 10 is just 10^(n-1)
base=(power(10,argument0))/10; Its more then possible this is written terribly but it does what I want it to (base=1 if 1 is entered, 10 if 2 is entered, 100 if three is entered, 1000 if four is entered - etc)

I think 2 147 483 647. Is the highest value I have been able to set a variable before issues start occurring. Beyond that, I think game maker handles the highest bit differently depending on the operations.
Thanks for the tip. After reading up on it 2 147 483 647 is the maximum value for a 32 bit integer so I think that might be whats going on here.
 

TheouAegis

Member
base=(power(10,argument0))/10; Its more then possible this is written terribly but it does what I want it to (base=1 if 1 is entered, 10 if 2 is entered, 100 if three is entered, 1000 if four is entered - etc)
Yes, which can be calculated much faster by just using

power(10,argument0-1)

power(n,x)/n is always slower than power(n,x-1) because division is a slow operation for computers. It's best to avoid it when possible.
 
Top