digitalmars.D.learn - array cast from float[16] to float[4][4]
- Spacen Jasset (7/7) Jan 10 2008 If I have:
- Christopher Wright (10/20) Jan 10 2008 A float[4][4] is an array of four float[4]s.
- Spacen Jasset (4/28) Jan 10 2008 I see yes. I will develop a class I think instead of trying to quickly
- Jarrett Billingsley (4/8) Jan 10 2008 Actually, float[4][4] is 16 consecutive floats in memory. Static arrays...
- Spacen Jasset (3/16) Jan 10 2008 I see, that is what I wondered. I will however do something more
-
Don Clugston
(7/27)
Jan 10 2008
No it isn't.
It's a array of four floats, a simple block of memory. - torhu (14/21) Jan 10 2008 That will work just fine, since those arrays have the same memory
If I have: float[16] a; can I cast it like this cast(float[4][4])a and pass it into a function expecting a float[4][4] or will this not work properly. It seems there is a problem doing so. This is for working with matricies so I should most probably write some sort of class to allow row and columns access
Jan 10 2008
Spacen Jasset wrote:If I have: float[16] a; can I cast it like this cast(float[4][4])a and pass it into a function expecting a float[4][4] or will this not work properly. It seems there is a problem doing so. This is for working with matricies so I should most probably write some sort of class to allow row and columns accessA float[4][4] is an array of four float[4]s. A float[4] is a (length, pointer) pair. If multidimensional arrays are added (not just arrays of arrays), you'd get the syntax float[4,4], most likely. If you want to keep the same memory, you can do: float[16] flat; float[4][4] square = [flat[0..4], flat[4..8], flat[8..12], flat[12..$]]; This isn't very pretty, but it grows at sqrt(N) for an original array of N elements, so it might be doable for your stuff.
Jan 10 2008
Christopher Wright wrote:Spacen Jasset wrote:I see yes. I will develop a class I think instead of trying to quickly do the aforementioned cast. Thanks.If I have: float[16] a; can I cast it like this cast(float[4][4])a and pass it into a function expecting a float[4][4] or will this not work properly. It seems there is a problem doing so. This is for working with matricies so I should most probably write some sort of class to allow row and columns accessA float[4][4] is an array of four float[4]s. A float[4] is a (length, pointer) pair. If multidimensional arrays are added (not just arrays of arrays), you'd get the syntax float[4,4], most likely. If you want to keep the same memory, you can do: float[16] flat; float[4][4] square = [flat[0..4], flat[4..8], flat[8..12], flat[12..$]]; This isn't very pretty, but it grows at sqrt(N) for an original array of N elements, so it might be doable for your stuff.
Jan 10 2008
"Christopher Wright" <dhasenan gmail.com> wrote in message news:fm53jh$1r5k$1 digitalmars.com...A float[4][4] is an array of four float[4]s. A float[4] is a (length, pointer) pair. If multidimensional arrays are added (not just arrays of arrays), you'd get the syntax float[4,4], most likely.Actually, float[4][4] is 16 consecutive floats in memory. Static arrays of static arrays are treated as rectangular arrays.
Jan 10 2008
Jarrett Billingsley wrote:"Christopher Wright" <dhasenan gmail.com> wrote in message news:fm53jh$1r5k$1 digitalmars.com...I see, that is what I wondered. I will however do something more 'proper' for what I want to do finally.A float[4][4] is an array of four float[4]s. A float[4] is a (length, pointer) pair. If multidimensional arrays are added (not just arrays of arrays), you'd get the syntax float[4,4], most likely.Actually, float[4][4] is 16 consecutive floats in memory. Static arrays of static arrays are treated as rectangular arrays.
Jan 10 2008
Christopher Wright wrote:Spacen Jasset wrote:No it isn't. <g> It's a array of four floats, a simple block of memory. The cast should be fine. PROVIDED that you stick to static arrays (ie, all dimensions specified at compile time).If I have: float[16] a; can I cast it like this cast(float[4][4])a and pass it into a function expecting a float[4][4] or will this not work properly. It seems there is a problem doing so. This is for working with matricies so I should most probably write some sort of class to allow row and columns accessA float[4][4] is an array of four float[4]s. A float[4] is a (length, pointer) pair.If multidimensional arrays are added (not just arrays of arrays), you'd get the syntax float[4,4], most likely. If you want to keep the same memory, you can do: float[16] flat; float[4][4] square = [flat[0..4], flat[4..8], flat[8..12], flat[12..$]];You're thinking of float [][4] square = [flat[0..4], flat[4..8], flat[8..12], flat[12..$]]; With float[4][4] there's no problem.
Jan 10 2008
Spacen Jasset wrote:If I have: float[16] a; can I cast it like this cast(float[4][4])a and pass it into a function expecting a float[4][4] or will this not work properly. It seems there is a problem doing so.That will work just fine, since those arrays have the same memory layout. But you might want to avoid such casts, since the compiler won't verify that the size of the array you're casting from actually matches the size of what you're casting to. Static (meaning fixed-length) arrays are implicitly converted to dynamic arrays when used as an argument to a function that wants a dynamic array. Then you can get hold of the true length of the argument inside the function, and do assert(a.length == 16). Then you'll know it's safe to cast it to float[4][4]. But this is getting messy, so maybe a different way altogether is better. Like wrapping a one-dimensional array in a struct with opIndex and opIndexAssign operators. Scroll down to Rectangular Arrays on this page: http://www.digitalmars.com/d/arrays.html
Jan 10 2008