Skip to content

Commit faf1153

Browse files
authored
Merge pull request #74 from ngoldbaum/float-casts
Add casts between string and float dtypes
2 parents 41d428c + 1acd44e commit faf1153

File tree

10 files changed

+253
-33
lines changed

10 files changed

+253
-33
lines changed

asciidtype/asciidtype/src/asciidtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyInit__asciidtype_main(void)
2222
return NULL;
2323
}
2424

25-
if (import_experimental_dtype_api(12) < 0) {
25+
if (import_experimental_dtype_api(13) < 0) {
2626
return NULL;
2727
}
2828

metadatadtype/metadatadtype/src/metadatadtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyInit__metadatadtype_main(void)
2121
if (_import_array() < 0) {
2222
return NULL;
2323
}
24-
if (import_experimental_dtype_api(12) < 0) {
24+
if (import_experimental_dtype_api(13) < 0) {
2525
return NULL;
2626
}
2727

mpfdtype/mpfdtype/src/mpfdtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyInit__mpfdtype_main(void)
2222
if (_import_array() < 0) {
2323
return NULL;
2424
}
25-
if (import_experimental_dtype_api(12) < 0) {
25+
if (import_experimental_dtype_api(13) < 0) {
2626
return NULL;
2727
}
2828

quaddtype/quaddtype/src/quaddtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PyInit__quaddtype_main(void)
2323
return NULL;
2424

2525
// Fail to init if the experimental DType API version 5 isn't supported
26-
if (import_experimental_dtype_api(12) < 0) {
26+
if (import_experimental_dtype_api(13) < 0) {
2727
PyErr_SetString(PyExc_ImportError,
2828
"Error encountered importing the experimental dtype API.");
2929
return NULL;

stringdtype/meson.build

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ incdir_numpy = run_command(py,
1414
check: true
1515
).stdout().strip()
1616

17+
cc = meson.get_compiler('c')
18+
19+
npymath_path = incdir_numpy / '..' / 'lib'
20+
npymath_lib = cc.find_library('npymath', dirs: npymath_path)
21+
inc_np = include_directories(incdir_numpy)
22+
np_dep = declare_dependency(include_directories: inc_np)
23+
1724
includes = include_directories(
1825
[
1926
incdir_numpy,
@@ -47,5 +54,6 @@ py.extension_module(
4754
srcs,
4855
install: true,
4956
subdir: 'stringdtype',
50-
include_directories: includes
57+
include_directories: includes,
58+
dependencies: [np_dep, npymath_lib]
5159
)

stringdtype/stringdtype/src/casts.c

Lines changed: 218 additions & 26 deletions
Large diffs are not rendered by default.

stringdtype/stringdtype/src/casts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define NO_IMPORT_ARRAY
99
#include "numpy/arrayobject.h"
1010
#include "numpy/experimental_dtype_api.h"
11+
#include "numpy/halffloat.h"
1112
#include "numpy/ndarraytypes.h"
1213

1314
PyArrayMethod_Spec **

stringdtype/stringdtype/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ PyInit__main(void)
9292
if (_import_array() < 0) {
9393
return NULL;
9494
}
95-
if (import_experimental_dtype_api(12) < 0) {
95+
if (import_experimental_dtype_api(13) < 0) {
9696
return NULL;
9797
}
9898

stringdtype/tests/test_stringdtype.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,25 @@ def test_unsized_integer_casts(dtype, typename, signed):
408408
np.testing.assert_array_equal(ainp, ainp.astype(dtype).astype(idtype))
409409

410410

411+
@pytest.mark.parametrize("typename", ["float64", "float32", "float16"])
412+
def test_float_casts(dtype, typename):
413+
inp = [1.1, 2.8, -3.2, 2.7e4]
414+
ainp = np.array(inp, dtype=typename)
415+
np.testing.assert_array_equal(ainp, ainp.astype(dtype).astype(typename))
416+
417+
fi = np.finfo(typename)
418+
419+
inp = [1e-324, fi.smallest_subnormal, -1e-324, -fi.smallest_subnormal]
420+
eres = [0, fi.smallest_subnormal, -0, -fi.smallest_subnormal]
421+
res = np.array(inp, dtype=typename).astype(dtype).astype(typename)
422+
np.testing.assert_array_equal(eres, res)
423+
424+
inp = [2e308, fi.max, -2e308, fi.min]
425+
eres = [np.inf, fi.max, -np.inf, fi.min]
426+
res = np.array(inp, dtype=typename).astype(dtype).astype(typename)
427+
np.testing.assert_array_equal(eres, res)
428+
429+
411430
def test_take(dtype, string_list):
412431
sarr = np.array(string_list, dtype=dtype)
413432
out = np.empty(len(string_list), dtype=dtype)

unytdtype/unytdtype/src/unytdtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyInit__unytdtype_main(void)
2121
if (_import_array() < 0) {
2222
return NULL;
2323
}
24-
if (import_experimental_dtype_api(12) < 0) {
24+
if (import_experimental_dtype_api(13) < 0) {
2525
return NULL;
2626
}
2727

0 commit comments

Comments
 (0)