Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A quirk? (perhaps well known...)

1 view
Skip to first unread message

Riverola, Josep

unread,
Mar 18, 1998, 3:00:00 AM3/18/98
to

I am getting this (curious?) result when changing the type of an array
item from 'float to 'single-float:

(let ((a (make-array 10 :element-type 'float))) (typep a 'simple-vector)) T
(let ((a (make-array 10 :element-type 'single-float))) (typep a 'simple-vector)) NIL

So, a vector whose elements type is 'single-float is not a simple
vector?. However, it is a simple-array

(let ((a (make-array 10 :element-type 'single-float))) (typep a 'simple-array))
T

where could I find the rationale for this?

Josep Riverola
IESE
Camino del Cerro del Aguila 10
Madrid 28023-Spain
mailto:rive...@iese.es

Josep Riverola

Alan Ruttenberg

unread,
Mar 20, 1998, 3:00:00 AM3/20/98
to

The most general type of array has elements which can be anything. If the contents are floats then a pointer to the float is stored in the array. This can be an inneficient use of space. For double floats which use 8 bytes of memory, you need 12 bytes instead of 8, since 4 bytes are used for the pointer. If you have an array of bytes it is even worse, you need 4 bytes instead of 1 byte per element.

To address this, MCL can create some array types which store elements of the same king in a more efficient way. What you have seen is that an array of floats is created as the more general type of vector. MCL needs to do this because a float can either be a single or double float (which are the same thing in MCL) - taking 8 bytes, or a short-float, which take 4 bytes. When you are asking for an array of short-float, MCL knows that each element take 4 bytes, and creates a specialized array accordingly. The same thing would happen if you made an array of double-floats (or equivalent single-floats). When you ask for something that could be both, MCL punts and just makes the most general type of array.

The reason one is of type 'simple-array but not 'simple-vector is that simple-array is a more general type than vector or simple-vector. A simple vector is an array when each element can be any lisp object.
You can see this by inspecting the class precedence list for (find-class 'simple-vector)

simple-vector
general-vector
simple-1d-array
vector
simple-array
array
sequence
t

So all simple-vectors are simple-arrays, but not all simple-arrays (even of 1 dimension) are simple-vectors. A little more understandable is

(setq a (make-array 10 :element-type 'single-float))
(typep a 'vector) -> t
(array-element-type a) -> double-float
(typep a 'simple-vector) -> nil ;; not simple because elements are only double-floats

(setq a (make-array 10 :element-type 'float))
(typep a 'vector) -> t
(array-element-type a) -> t
(typep a 'simple-vector) -> t ;; simple because MCL makes an array that can hold any value.

Hope this helps,
Alan

0 new messages