' ============================================================ ' Test program for extended data types ' BYTE -> uint8_t, INTEGER -> int16_t, LONG -> int32_t, ' FLOAT -> float, DOUBLE -> double ' ============================================================ ' ---- BYTE (uint8_t): range 0 to 255 ---- PRINT "==== BYTE (uint8_t) ====" DIM b1 AS BYTE DIM b2 AS BYTE b1 = 100 b2 = 200 PRINT "100 + 200 (byte) = "; b1 + b2 b1 = 255 PRINT "Max uint8_t = "; b1 b2 = b1 + 1 PRINT "255 + 1 overflows to "; b2 ' BYTE arithmetic b1 = 50 b2 = 30 PRINT "50 - 30 = "; b1 - b2 PRINT "50 * 3 = "; b1 * 3 ' BYTE in array DIM byteArr(3) AS BYTE DIM bi AS BYTE FOR bi = 0 TO 3 byteArr(bi) = bi * 10 NEXT bi PRINT "BYTE array: "; FOR bi = 0 TO 3 PRINT byteArr(bi); " "; NEXT bi PRINT "" ' BYTE + INTEGER promotion DIM bv AS BYTE DIM iv AS INTEGER bv = 100 iv = 1000 PRINT "byte + int16: "; bv + iv ' BYTE function FUNCTION ByteMax(BYVAL x AS BYTE, BYVAL y AS BYTE) AS BYTE IF x > y THEN ByteMax = x ELSE ByteMax = y END IF END FUNCTION PRINT "ByteMax(100, 200) = "; ByteMax(100, 200) ' ---- INTEGER (int16_t): range -32768 to 32767 ---- PRINT "" PRINT "==== INTEGER (int16_t) ====" DIM i AS INTEGER DIM j AS INTEGER i = 100 j = 200 PRINT "100 + 200 = "; i + j i = 32000 PRINT "32000 as INTEGER = "; i ' Demonstrate int16_t overflow wrapping i = 32767 PRINT "Max int16_t = "; i j = i + 1 PRINT "32767 + 1 overflows to "; j ' Integer suffix % DIM count% AS INTEGER count% = 42 PRINT "count% = "; count% ' ---- LONG (int32_t): range -2147483648 to 2147483647 ---- PRINT "" PRINT "==== LONG (int32_t) ====" DIM a AS LONG DIM b AS LONG DIM big AS LONG a = 100000 b = 200000 PRINT "100000 + 200000 = "; a + b big = 1000000 big = big * 1000 PRINT "1000000 * 1000 = "; big ' LONG can hold values far beyond int16_t range big = 32767 big = big * 100 PRINT "32767 * 100 = "; big ' Arithmetic with LONGs a = 123456 b = 789012 PRINT "123456 + 789012 = "; a + b PRINT "789012 - 123456 = "; b - a PRINT "123456 * 2 = "; a * 2 ' LONG division and modulo a = 1000000 b = 7 PRINT "1000000 \\ 7 = "; a \ b PRINT "1000000 MOD 7 = "; a MOD b ' ---- FLOAT (float: ~7 digits precision) ---- PRINT "" PRINT "==== FLOAT ====" DIM f1 AS FLOAT DIM f2 AS FLOAT DIM fResult AS FLOAT f1 = 3.14 f2 = 2.71 PRINT "3.14 + 2.71 = "; f1 + f2 PRINT "3.14 * 2.71 = "; f1 * f2 PRINT "3.14 / 2.71 = "; f1 / f2 ' Float suffix ! DIM pi! AS FLOAT pi! = 3.14159 PRINT "pi! = "; pi! ' Float precision test f1 = 1.0 f2 = 3.0 fResult = f1 / f2 PRINT "1.0 / 3.0 (float) = "; fResult f1 = 123456.789 PRINT "123456.789 as float = "; f1 ' ---- DOUBLE (double: ~15 digits precision) ---- PRINT "" PRINT "==== DOUBLE ====" DIM d1 AS DOUBLE DIM d2 AS DOUBLE DIM dResult AS DOUBLE d1 = 3.14159265358979 d2 = 2.71828182845904 PRINT "pi = "; d1 PRINT "e = "; d2 PRINT "pi + e = "; d1 + d2 PRINT "pi * e = "; d1 * d2 ' Double suffix # DIM exact# AS DOUBLE exact# = 1.0 / 3.0 PRINT "1/3 (double) = "; exact# ' ---- Type promotion in expressions ---- PRINT "" PRINT "==== Type Promotion ====" ' INTEGER + LONG -> LONG DIM si AS INTEGER DIM sl AS LONG si = 100 sl = 100000 PRINT "int16 + int32: "; si + sl ' INTEGER + FLOAT -> FLOAT DIM sf AS FLOAT sf = 3.14 PRINT "int16 + float: "; si + sf ' INTEGER + DOUBLE -> DOUBLE DIM sd AS DOUBLE sd = 3.14159265358979 PRINT "int16 + double: "; si + sd ' LONG + FLOAT -> FLOAT PRINT "int32 + float: "; sl + sf ' LONG + DOUBLE -> DOUBLE PRINT "int32 + double: "; sl + sd ' FLOAT + DOUBLE -> DOUBLE PRINT "float + double: "; sf + sd ' ---- Mixed type functions ---- PRINT "" PRINT "==== Functions with New Types ====" FUNCTION AddLongs(BYVAL x AS LONG, BYVAL y AS LONG) AS LONG AddLongs = x + y END FUNCTION FUNCTION MultiplyFloat(BYVAL x AS FLOAT, BYVAL y AS FLOAT) AS FLOAT MultiplyFloat = x * y END FUNCTION FUNCTION LongFactorial(BYVAL n AS LONG) AS LONG IF n <= 1 THEN LongFactorial = 1 ELSE LongFactorial = n * LongFactorial(n - 1) END IF END FUNCTION FUNCTION FloatSqrt(BYVAL x AS FLOAT) AS FLOAT FloatSqrt = SQR(x) END FUNCTION PRINT "AddLongs(100000, 200000) = "; AddLongs(100000, 200000) PRINT "MultiplyFloat(3.14, 2.0) = "; MultiplyFloat(3.14, 2.0) PRINT "LongFactorial(12) = "; LongFactorial(12) PRINT "FloatSqrt(2.0) = "; FloatSqrt(2.0) ' ---- BYREF with new types ---- PRINT "" PRINT "==== BYREF with New Types ====" SUB SwapLongs(BYREF x AS LONG, BYREF y AS LONG) LOCAL t AS LONG t = x x = y y = t END SUB SUB ScaleFloat(BYREF val AS FLOAT, BYVAL factor AS FLOAT) val = val * factor END SUB DIM lx AS LONG DIM ly AS LONG lx = 100000 ly = 999999 PRINT "Before SwapLongs: "; lx; " "; ly CALL SwapLongs(lx, ly) PRINT "After SwapLongs: "; lx; " "; ly DIM fv AS FLOAT fv = 2.5 PRINT "Before ScaleFloat: "; fv CALL ScaleFloat(fv, 4.0) PRINT "After ScaleFloat(2.5, 4.0): "; fv ' ---- Arrays with new types ---- PRINT "" PRINT "==== Arrays with New Types ====" DIM longArr(5) AS LONG DIM fltArr(5) AS FLOAT DIM idx AS INTEGER FOR idx = 0 TO 5 longArr(idx) = (idx + 1) * 100000 fltArr(idx) = (idx + 1) * 1.5 NEXT idx PRINT "LONG array: "; FOR idx = 0 TO 5 PRINT longArr(idx); " "; NEXT idx PRINT "" PRINT "FLOAT array: "; FOR idx = 0 TO 5 PRINT fltArr(idx); " "; NEXT idx PRINT "" ' ---- STATIC with new types ---- PRINT "" PRINT "==== STATIC with New Types ====" FUNCTION LongCounter() AS LONG STATIC lc AS LONG lc = lc + 100000 LongCounter = lc END FUNCTION FUNCTION FloatAccum(BYVAL v AS FLOAT) AS FLOAT STATIC fs AS FLOAT fs = fs + v FloatAccum = fs END FUNCTION PRINT "LongCounter: "; FOR idx = 1 TO 5 PRINT LongCounter(); " "; NEXT idx PRINT "" PRINT "FloatAccum: "; DIM ftemp AS FLOAT ftemp = FloatAccum(1.1) PRINT ftemp; " "; ftemp = FloatAccum(2.2) PRINT ftemp; " "; ftemp = FloatAccum(3.3) PRINT ftemp; " "; PRINT "" ' ---- Mixing classic line numbers with new types ---- PRINT "" PRINT "==== Classic Lines with New Types ====" DIM counter AS LONG counter = 0 2000 counter = counter + 50000 2010 IF counter >= 250000 THEN GOTO 2030 2020 GOTO 2000 2030 PRINT "LONG counter reached: "; counter ' ---- Range demonstration ---- PRINT "" PRINT "==== Type Ranges ====" DIM maxInt AS INTEGER DIM maxLong AS LONG maxInt = 32767 maxLong = 2147483647 PRINT "Max INTEGER (int16_t): "; maxInt PRINT "Max LONG (int32_t): "; maxLong ' Show that LONG can handle what overflows INTEGER DIM intVal AS INTEGER DIM longVal AS LONG intVal = 200 longVal = 200 intVal = intVal * intVal longVal = longVal * longVal PRINT "200 * 200 as INTEGER: "; intVal PRINT "200 * 200 as LONG: "; longVal PRINT "" PRINT "==== ALL TYPE TESTS COMPLETE ===="