Skip to content

Commit c9f8f65

Browse files
committed
py: Add support for float/double arrays in array module.
Addresses issue adafruit#981.
1 parent 9d1ca65 commit c9f8f65

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

py/binary.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ int mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
6161
size = 8; break;
6262
case 'P': case 'O': case 'S':
6363
size = sizeof(void*); break;
64+
case 'f':
65+
size = sizeof(float); break;
66+
case 'd':
67+
size = sizeof(double); break;
6468
}
6569
break;
6670
case '@': {
@@ -90,6 +94,12 @@ int mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
9094
case 'P': case 'O': case 'S':
9195
align = alignof(void*);
9296
size = sizeof(void*); break;
97+
case 'f':
98+
align = alignof(float);
99+
size = sizeof(float); break;
100+
case 'd':
101+
align = alignof(double);
102+
size = sizeof(double); break;
93103
}
94104
}
95105
}
@@ -252,10 +262,10 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v
252262
switch (typecode) {
253263
#if MICROPY_PY_BUILTINS_FLOAT
254264
case 'f':
255-
((float*)p)[index] = mp_obj_float_get(val_in);
265+
((float*)p)[index] = mp_obj_get_float(val_in);
256266
break;
257267
case 'd':
258-
((double*)p)[index] = mp_obj_float_get(val_in);
268+
((double*)p)[index] = mp_obj_get_float(val_in);
259269
break;
260270
#endif
261271
default:

tests/float/float_array.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from array import array
2+
3+
def test(a):
4+
print(a)
5+
a.append(1.2)
6+
print(len(a), '%.3f' % a[0])
7+
a.append(1)
8+
a.append(False)
9+
print(len(a), '%.3f %.3f' % (a[1], a[2]))
10+
a[-1] = 3.45
11+
print('%.3f' % a[-1])
12+
13+
test(array('f'))
14+
test(array('d'))

0 commit comments

Comments
 (0)