LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

SmileBasic Handling Large Numbers Interestingly

Root / General / [.]

PtcguyCreated:
When defining a recursive factorial (as in the in-game manual): factorial 31 gives 738197504, factorial 32 and 33 give -2147483648, and factorial 34 and beyond give 0 until I get a stack overflow error somewhere around 10000. It probably has something to do with overflow that's not causing an error so it just prints 0. Also, when multiplying 10 000 by 10, it starts out normal for a few iterations, then one gets:
10000000000000000000000
99999999999999992000000
and subsequent numbers that have nines. Although, later on, if I let this continue, eventually the numbers have a 1, followed by a bunch of zeroes, a 1 or 2, and some more zeroes until inf shows up. Adding produces a similar effect. EDIT: The code used for factorial is:
DEF FACTORIAL(N)
IF N==1 THEN RETURN 1
RETURN N*FACTORIAL(N-1)
END
Tell me if this is on the wrong thread and I'll move it.

?FACTORIAL(32) will show a negative result. V=32:?FACTORIAL(V) will show a positive value (unless OPTION DEFINT). Or, if you replace RETURN 1 with RETURN 1.0, then ?FACTORIAL(32) will show a positive value. Operators behave differently when both operators are of 'integer' type, than when one is not. Although SmileBasic has an error Overflow error, it does not happen whenever there's an overflow. Overflows are responsible for the code generating incorrect values for every factorial above 12. It's an issue which is orthogonal to the stack. As for the powers of 10 not all being 1000..., that's a limitation of floating-point representations only having fixed, finite precision. It is 'interesting'. But, these particular issues are not particular to SmileBasic particularly; you will have to learn 'interesting' things like this whenever you approach the limits of any programming language.

It is 'interesting'. But, these particular issues are not particular to SmileBasic particularly; you will have to learn 'interesting' things like this whenever you approach the limits of any programming language.
okay.

Operators behave differently when both operators are of 'integer' type, than when one is not. Although SmileBasic has an error Overflow error, it does not happen whenever there's an overflow. Overflows are responsible for the code generating incorrect values for every factorial above 12. It's an issue which is orthogonal to the stack.
I've gotten an Overflow before, but I believe it was when I tried to assign a too-big value to an int. Don't remember. Someone fool around with this.

I have only seen Overflow error when a floating-point number outside the range of the integer type is put in an integer variable. Overflows for operations involving only integers happen silently, without an error, and other overflows yield -inf, inf, or possibly nan, also without an error. It only happens for assignments. Trying to assign inf or -inf to an integer variable gives the error. Trying to assign nan to an integer variable succeeds - the value 0 is used.