Is there a maximum number of dimensions to an array?

New to FreeBASIC? Post your questions here.
Post Reply
RayTomes
Posts: 36
Joined: Mar 13, 2008 20:18
Location: Auckland, New Zealand
Contact:

Is there a maximum number of dimensions to an array?

Post by RayTomes »

To learn about freebasic I was trying out some programs that I wrote in CSC (Corel Script Language) which is very similar to QBASIC (withe knobs on).
I tried a program which has a 9 dimensional array (yes really 9) and it complained that I had too many subscripts (starting from the bottom of the program and working up). So I knocked out one dimension and it now works. Note that it had 18 nested FOR ... NEXT loops also.

I cannot see anything in the documentation about any limits on the number of dimensions. Does anyone know if there is a limit? If this had worked well I was thinking of going for about an 11 dimensional array. :-)
RayTomes
Posts: 36
Joined: Mar 13, 2008 20:18
Location: Auckland, New Zealand
Contact:

Post by RayTomes »

Oh, I just saw in the documentation that it says:
"The maximum number of dimensions of a multidimensional array is 8. "

Oh damn, why do they make limits like this?
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Post by integer »

Just curious, but what you are doing that requires 11 dimensions?

Most of the arrays I use rarely have more than three dimensions.
When more than five are required, the arrays become extremely sparse, and it is easier to tag the elements.

If you have a modest 10 elements per dimension, your 11 dimension array would require about 200 GIG of space (200 billion bytes).

If you really require more dimensions, then create your own UDT.
When you require a specific element, access it through your defined type.

It is unlikely that you have more than a 1000 gigabytes of random access memory, thus the necessity for file access.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

I never use an array more than one dimension. I hate multidimensional arrays with a passion. I don't mind arrays of arrays (ex Java), but multidimensional, sequential arrays?

I really prefer pointers and some simple math instead.


However! ... I don't like the artificial limits. I only like being limited when I absolutely have to be, due to technical challenges, for instance.
HD_
Posts: 215
Joined: Jun 10, 2006 12:15
Contact:

Post by HD_ »

Multi-dimensional arrays make code more readable, eg:
tiles(layer,x,y)
But 9 or 11 dimensions is just insane. If I was to try to expand the above to as many dimensions as I could:
tiles(universe,galaxy,planet,computer,program,room,layer,x,y,phosphor,atom)

There, 11 dimensions to fully describe a unit in a game on any computer in any world in any galaxy in any universe. What are you using it for?
RayTomes
Posts: 36
Joined: Mar 13, 2008 20:18
Location: Auckland, New Zealand
Contact:

Post by RayTomes »

Well, I am calculating the number of ways that each number with many factorizations can be factorized. I have done up to 10^53 so far but want to go a bit further. The easiest way is to represent each number as its prime factorization:

n = 2^a 3^b 5^c 7^d 11^e 13^f 17^g 19^h 23^i ...

The number of ways it can be factorized depends directly on the coefficients a,b,c,d,e,f,g,h,i ... so I have a table with that many dimensions. Some of the dimensions only have a small range but it still makes quite a big array.

See http://ray.tomes.biz/maths.html if you want to know why I do this. :-)
actuall HD_'s description is too far out. ;-)

I can combine several indexes into one and still do what I want.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Is there a maximum number of dimensions to an array?

Post by Luis Babboni »

Hello,

I´m in the same trouble. I need up to 10 dimensions for making an stat about chess.
Cause may be I will need tht stat in real time, is important for me the time access I will have and using Allocate seems is by far much slower than using arrays..... so I ´m perplexed :-/
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Is there a maximum number of dimensions to an array?

Post by St_W »

I don't know whether that's a technical limit or just a defined limit, but you may try setting the following #define to some higher value and recompile fbc:

Code: Select all

' in src/compiler/fb.bi
const FB_MAXARRAYDIMS       = 8
see https://github.com/freebasic/fbc/blob/m ... /fb.bi#L33

dkl can probably give a hint whether that would work or not and whether this causes any side-effects.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Is there a maximum number of dimensions to an array?

Post by grindstone »

Luis Babboni wrote:Allocate seems is by far much slower than using arrays..... so I ´m perplexed :-/
It's not so surprising, for the array handling has been optimized for years.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Is there a maximum number of dimensions to an array?

Post by Luis Babboni »

What about this suggestion from St_W he made in the same question I did here:
http://www.freebasic.net/forum/viewtopi ... =2&t=10920
St_W wrote:I don't know whether that's a technical limit or just a defined limit, but you may try setting the following #define to some higher value and recompile fbc:

Code: Select all

' in src/compiler/fb.bi
const FB_MAXARRAYDIMS       = 8
see https://github.com/freebasic/fbc/blob/m ... /fb.bi#L33

dkl can probably give a hint whether that would work or not and whether this causes any side-effects.
Luis Babboni
Posts: 375
Joined: Mar 15, 2015 12:41

Re: Is there a maximum number of dimensions to an array?

Post by Luis Babboni »

Thanks guys, actually are the same question in two threads.
Interesting what St_W suggest! :-)
This answer, in some level, my question did in the other post grindstone :-)
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Is there a maximum number of dimensions to an array?

Post by fxm »

There is a similar limitation on the number of levels of pointer indirection:
Maximum is:
p[n1][n2][n3][n4][n5][n6][n7][n8]
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Is there a maximum number of dimensions to an array?

Post by sancho2 »

HD_ wrote:Multi-dimensional arrays make code more readable, eg:

tiles(layer,x,y)

But 9 or 11 dimensions is just insane. If I was to try to expand the above to as many dimensions as I could:

tiles(universe,galaxy,planet,computer,program,room,layer,x,y,phosphor,atom)



There, 11 dimensions to fully describe a unit in a game on any computer in any world in any galaxy in any universe. What are you using it for?
Haven't you described an object though?

Code: Select all

Type TileType
		layer As Integer
		x As Integer
		y As Integer 
End Type

Type SuperTile extends TileType
	universe As Any Ptr
	galaxy As Any Ptr
	planet As Any Ptr
	computer As Any Ptr
	program As Any Ptr
	room As Any Ptr
	phosphor As Any Ptr
	atom As Any Ptr
End Type

Dim myTile As SuperTile

myTile.Layer = 1
myTile.universe = ?
Even more readable?
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Is there a maximum number of dimensions to an array?

Post by RockTheSchock »

EDIT:
I would suggest to use x,y coordinates as relative coordinates. So your galaxy cordinates are relative to the universe center. Planet coords are relative to the galaxy center and so on.

Code: Select all

Type TileType
   title As String	
   x As Integer
   y As Integer
   parent As TileType Ptr
End Type


Type AtomType As Integer

Type PhosphorType extends TileType
	atom(Any) As AtomType
End Type

Type RoomType extends TileType
	phosphor(Any) As PhosphorType
End Type

Type ProgramType extends TileType
	room(Any) As RoomType
End Type

Type ComputerType extends TileType
	program(Any) As ProgramType	
End Type


Type PlanetType extends TileType
	computer(Any) As ComputerType	
End Type


Type GalaxyType extends TileType		
	planet(Any) As PlanetType	
End Type

Type UniverseType extends TileType	
	Declare Constructor() 				          'Create empty universe
	Declare Constructor(seed As Double=-1.0)   'Create random universe using seed / defaults to Randomize Timer
	galaxy(Any) As GalaxyType	
End Type

Constructor UniverseType()
	ReDim Preserve galaxy(1 To 100) ' create 100 empty galaxies
End Constructor

Constructor UniverseType(seed As Double=-1.0)
End Constructor

Dim universe(1 To 11) As UniverseType
sleep
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Is there a maximum number of dimensions to an array?

Post by sancho2 »

RockTheSchock wrote:EDIT:
I would suggest to use x,y coordinates as relative coordinates. So your galaxy cordinates are relative to the universe center. Planet coords are relative to the galaxy center and so on.
Very nice RockTheShock. It makes a guy want to continue the program.
Post Reply