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

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
More numeric data types
« on: 2010-Aug-15 »
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
« Last Edit: 2010-Aug-16 by hardyx »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10803
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: More numeric data types
« Reply #1 on: 2010-Aug-17 »
you can use V8 beta, and "try" to use:
LOCAL number AS short // char, float, double

Online Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5111
  • O Rly?
    • View Profile
Re: More numeric data types
« Reply #2 on: 2010-Aug-17 »
 :nw:
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: More numeric data types
« Reply #3 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.
« Last Edit: 2010-Aug-18 by hardyx »

MrTAToad

  • Guest
Re: More numeric data types
« Reply #4 on: 2010-Aug-17 »
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

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 932
  • KodeSource
    • View Profile
    • KodeSource
Re: More numeric data types
« Reply #5 on: 2010-Aug-17 »
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 '$'?
« Last Edit: 2010-Aug-17 by Slydog »
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: More numeric data types
« Reply #6 on: 2010-Aug-18 »
@MrTAToad: You can define the types, but not operate because the functions aren't implemented.

Quote from: Slydog
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?)
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: Slydog
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 '$'?
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.
« Last Edit: 2010-Aug-18 by hardyx »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10803
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: More numeric data types
« Reply #7 on: 2010-Aug-19 »
char is signed byte (-128 .. 127)
I'll have to overload a few more operators, I guess ;)

MrTAToad

  • Guest
Re: More numeric data types
« Reply #8 on: 2010-Aug-19 »
Yes, looks like it :)

Offline S. P. Gardebiter

  • Mr. Polyvector
  • ***
  • Posts: 247
    • View Profile
    • Tile 44 Interactive
Re: More numeric data types
« Reply #9 on: 2010-Sep-22 »
Is there an unsigned char? o;
~ Cave Story rules! ~

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: More numeric data types
« Reply #10 on: 2010-Sep-22 »
Is there an unsigned char? o;

Code: (glbasic) [Select]
LOCAL number AS BYTE
number = 255

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: More numeric data types
« Reply #11 on: 2010-Oct-27 »
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!  :)
« Last Edit: 2010-Oct-28 by hardyx »

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10803
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: More numeric data types
« Reply #12 on: 2010-Oct-28 »
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

  • Guest
Re: More numeric data types
« Reply #13 on: 2010-Oct-28 »
Did you expand operators for use with the new types ?

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: More numeric data types
« Reply #14 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.

Did you expand operators for use with the new types ?
Not yet, sorry. The operators are in the glb.h file.
« Last Edit: 2010-Oct-28 by hardyx »