More numeric data types

Previous topic - Next topic

hardyx

Currently there are two types: Integer % (32 bits) and Float # (64/32 bits).

I'll like to see more numeric data types like other BASIC languages. This way we can optimize the amount of memory used for large arrays in portable machines.

I think short integer (16 bits) can be useful, indicated by "!" or other character.
Byte (8 bits) can be useful too, but less used.

Example:
Code (glbasic) Select
GLOBAL largemap![]
DIM largemap![500][500]  // this uses half space of an integer array

largemap[1][1] = -500
largemap[2][2] = 123
largemap[3][3] = 32767


Kitty Hello

you can use V8 beta, and "try" to use:
LOCAL number AS short // char, float, double

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

hardyx

#3
Quote from: Kitty Hello on 2010-Aug-17
you can use V8 beta, and "try" to use:
LOCAL number AS short // char, float, double
Is this possible? Oh my God!!

You can define short variables, but not short arrays. Because the function DIM for shorts "DIM(short int&, int)" not exists. Then, if you write "LOCAL numbers[] AS short" you get a syntax error.

MrTAToad

Hmm.  With the following code :

Code (glbasic) Select
LOCAL test AS short
LOCAL test2 AS char
LOCAL test3 AS float
LOCAL test4 AS double

test=1234
test2=256
DEBUG test+" "+test2+"\n"


I get the following errors :

"TestDDGui.gbas"(8) warning : probably unassigned variable : test+" "+test2+"\n"
"TestDDGui.gbas"(8) warning : implicitly created GLOBAL  : test+" "+test2+"\n"
Wordcount:3 commands
compiling:
In file included from C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_tempg.cpp:2:
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_temp.h:10: error: expected initializer before '+' token
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_temp.h:10: error: expected initializer before '+' token
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_tempg.cpp:7: error: expected initializer before '+' token
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_tempg.cpp:7: error: expected initializer before '+' token
In file included from C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_temp0.cpp:1:
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_temp.h:10: error: expected initializer before '+' token
C:\Users\Nicholas\AppData\Local\Temp\glbasic\gpc_temp.h:10: error: expected initializer before '+' token
*** FATAL ERROR - Please post this output in the forum

Slydog

#5
Do you still get the error if you add spacing around the '+' signs?
Code (glbasic) Select
DEBUG test + " " + test2 + "\n"

Could be a bug in the overloaded '+' operator.

Plus, the line: test2=256, is that allowed? ie. a direct integer to assign an ascii value?  And/or is it out of range? (255 max?)
(Never tried, so I don't know).
Or do you need to assigned it with test2 = CHR$(255).

[Edit]
Is a char type a numerical type, like a byte, or a string type?
If its a string type, do we need to use a '$'?
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

hardyx

#6
@MrTAToad: You can define the types, but not operate because the functions aren't implemented.

Quote from: SlydogPlus, the line: test2=256, is that allowed? ie. a direct integer to assign an ascii value?  And/or is it out of range? (255 max?)
In C you can assign 256 to a char, but only gets the lowest 8 bits.
256 = 100h (hexa), then the value of test2 will be 0.

Quote from: SlydogIs a char type a numerical type, like a byte, or a string type?
If its a string type, do we need to use a '$'?
The type char in C is a number, chars are 8 bits numbers.
In GCC compilers chars are signed by default (-127..128), others are unsigned.
char in not a string type, is an element of a string.
I think "char" type is difficult to implement in GLBasic.

Kitty Hello

char is signed byte (-128 .. 127)
I'll have to overload a few more operators, I guess ;)

MrTAToad


S. P. Gardebiter

Is there an unsigned char? o;
~ Cave Story rules! ~

hardyx

Quote from: S. P. Gardebiter on 2010-Sep-22
Is there an unsigned char? o;

Code (glbasic) Select
LOCAL number AS BYTE
number = 255


hardyx

#11
I have been writing same code to add new types to GLBasic using inline code, and here are the results. Maybe some functions aren't implemented for this types, but it's a beginning.

I defined two new integer types:
DGShort: uses 16 bits, range -32768..32767
DGByte: uses 8 bits, range 0..255

Add this to the end of the file glb.h file. This is part of GLBasic installation, and is located in "compiler\platform\include\glb.h" in the GLBasic folder. I don't like to change this file, but is the only way I know to define new base types. Using inline in the program doesn't work. If anybody knows other method, please say to me.

Code (glbasic) Select
// add this lines before the last #endif

// hardyx: new integer types
typedef short DGShort;
typedef DGArray<DGShort> DGShortArray;

typedef unsigned char DGByte;
typedef DGArray<DGByte> DGByteArray;


Now you can use arrays with bytes like this, and save many memory space:

Code (glbasic) Select
LOCAL tilemap[] AS DGByte
DIM tilemap[500][500]


Here is a GLB program to test the new types:
Code (glbasic) Select
// --------------------------------- //
// Project: Testshort v2
//

ALLOWESCAPE TRUE
SETSCREEN 320, 240, 0
LIMITFPS 30
GOSUB TestShort
GOSUB TestByte
SHOWSCREEN
KEYWAIT
END

SUB TestShort:
// this way works ok
LOCAL map[] AS DGShort
DIM map[100]

map[0] = -32000
map[1] = 1234
map[2] = 32767

PRINT "Test short array", 0,0
PRINT "map(0) = " + map[0], 0,10
PRINT "map(1) = " + map[1], 0,20
PRINT "map(2) = " + map[2], 0,30
ENDSUB

SUB TestByte:
// this way works ok
LOCAL mapb[] AS DGByte
DIM mapb[100]

mapb[0] = 0
mapb[1] = 123
mapb[2] = 255

PRINT "Test byte array", 0,50
PRINT "mapb(0) = " + mapb[0], 0,60
PRINT "mapb(1) = " + mapb[1], 0,70
PRINT "mapb(2) = " + mapb[2], 0,80
ENDSUB


I hope you like it!  :)

Kitty Hello

why aren't you using short and char instead of the typedef? Also, I fixed something with that in the next update. I have a working calculator app that uses double precision floats on iPhone. Works fine.

MrTAToad

Did you expand operators for use with the new types ?

hardyx

#14
Quote from: Kitty Hello on 2010-Oct-28
why aren't you using short and char instead of the typedef?
Because I notice that if you use C types the array isn't created in DIM instruction using DGArray<T>, it is created instead as short& and isn't compatible with the (already defined) DGArray functions. I think you manage this types in a different way. Change the example code using short and char and you'll see.

Quote from: MrTAToad on 2010-Oct-28
Did you expand operators for use with the new types ?
Not yet, sorry. The operators are in the glb.h file.