Author Topic: More numeric data types  (Read 10698 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10753
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: More numeric data types
« Reply #15 on: 2010-Oct-28 »
that's what I fixed. Also the operators work now.

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 496
    • View Profile
Re: More numeric data types
« Reply #16 on: 2010-Oct-28 »
Thanks Gernot!!
I'm waiting for the next update.
:booze:

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2687
    • View Profile
Re: More numeric data types
« Reply #17 on: 2010-Nov-08 »
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!  :)

I kinda missed this, its a valuable insight into the inner workings of GLB I think.

Many thanks,


Ian
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline S. P. Gardebiter

  • Mr. Polyvector
  • ***
  • Posts: 245
    • View Profile
    • Tile 44 Interactive
Re: More numeric data types
« Reply #18 on: 2010-Nov-25 »
I love it but actually it's making too much trouble to use it yet D;
If I define a variable as a "char" I can't use the function "Readbyte" with it. Readbyte wants "DGNat", I want to use a char though :S

What about unsigned shorts, integers and longs?
"Byte" is not working for me :/ Only "Char".
And "unsigned char" isn't working too, it says "Syntax error".
Any idea? o:

« Last Edit: 2010-Nov-25 by S. P. Gardebiter »
~ Cave Story rules! ~

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 496
    • View Profile
Re: More numeric data types
« Reply #19 on: 2010-Nov-25 »
In the last version, you can use numeric C types like: short (signed), WORD (unsigned short), char (signed), BYTE (unsigned char), double (floating 64 bits). These types are case sensitive: Byte isn't the same as BYTE.

Code: (glbasic) [Select]
LOCAL mapb[] AS BYTE  // works ok
DIM mapb[100]
mapb[0] = 123

Quote
If I define a variable as a "char" I can't use the function "Readbyte" with it. Readbyte wants "DGNat", I want to use a char though
I think you can use a integer(%) variable for READBYTE, and then assign the value to a BYTE variable using this: byteval = (num%).  Curiously using () works ok =D.
« Last Edit: 2010-Nov-25 by hardyx »

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 932
  • KodeSource
    • View Profile
    • KodeSource
Re: More numeric data types
« Reply #20 on: 2011-Jan-17 »
Old thread, but I have a problem.

In this code:
Code: (glbasic) [Select]
TYPE TLevel
level_id%
map[] AS BYTE  // Map array, 3 dimmensions, [y][z][x]
//map%[]       // This works
ENDTYPE

I get this error:
Code: (glbasic) [Select]
user type is not defined : BYTE
Am I using it wrong, or do these new data types not work in TYPEs?

BTW, I'm using GLBasic 8.200
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 496
    • View Profile
Re: More numeric data types
« Reply #21 on: 2011-Jan-17 »
I think TYPES are declared in other header file where define BYTE is not accesible. Using "char" (signed byte) or "short" (signed word) works good, because they are C types.
« Last Edit: 2011-Jan-17 by hardyx »

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 932
  • KodeSource
    • View Profile
    • KodeSource
Re: More numeric data types
« Reply #22 on: 2011-Jan-17 »
Thanks!  :booze:
You are probably right as 'char' seems to be working, and in my case, it should be good enough.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 932
  • KodeSource
    • View Profile
    • KodeSource
Re: More numeric data types
« Reply #23 on: 2011-Jan-17 »
Oh, so close!
Code: (glbasic) [Select]
CONSTANT DIR_X  AS char = 0
CONSTANT DIR_N  AS char = 1
CONSTANT DIR_E  AS char = 2
CONSTANT DIR_S  AS char = 4
CONSTANT DIR_W  AS char = 8

MazePart_AddSurface (TILE_0__, QUAD_XX, DIR_N + DIR_S + DIR_W + DIR_E)

gives me this error:
Code: (glbasic) [Select]
"LevelDraw.gbas"(589) warning : probably unassigned variable : DIR_N+ DIR_S + DIR_W + DIR_E
"LevelDraw.gbas"(589) error : variable is not explicitly defined : DIR_N+ DIR_S + DIR_W + DIR_E

You can't add 'chars' together?!?
Is it just a simple operator overload problem (ie: the '+' operator for char)?

[Edit]
But this seems to work (with the extra ()'s ):
Code: (glbasic) [Select]
MazePart_AddSurface (TILE_0__, QUAD_XX, (DIR_N) +  (DIR_S) +  (DIR_W) +  (DIR_E))
« Last Edit: 2011-Jan-17 by Slydog »
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

MrTAToad

  • Guest
Re: More numeric data types
« Reply #24 on: 2011-Jan-19 »
Dont think Gernot has updated operators to use the new types yet

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10753
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: More numeric data types
« Reply #25 on: 2011-Jan-19 »
The problem is, that the precompiler handles them as TYPEs, thus it does not know about any operators other than =, <> and == (which is = in GLBasic, too)

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 932
  • KodeSource
    • View Profile
    • KodeSource
Re: More numeric data types
« Reply #26 on: 2011-Jan-19 »
No problem, I reverted back to using simple integers (%), which have less compatibility problems!
I was just trying to save a little memory, which is no big deal.  (I have 4 integer arrays the same size, up to 50x50x10)
(I still may be able to, using integers, if I split the integer into 4 bytes by ANDing and ORing the bits I need in/out).
Thanks.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

MrTAToad

  • Guest
Re: More numeric data types
« Reply #27 on: 2011-Jan-19 »
Could always use INLINE C