MCPcopy Create free account
hub / github.com/numpy/numpy / _calc_length

Function _calc_length

numpy/core/src/multiarray/ctors.c:3028–3130  ·  view source on GitHub ↗

* the formula is len = (intp) ceil((stop - start) / step); */

Source from the content-addressed store, hash-verified

3026 * the formula is len = (intp) ceil((stop - start) / step);
3027 */
3028static npy_intp
3029_calc_length(PyObject *start, PyObject *stop, PyObject *step, PyObject **next, int cmplx)
3030{
3031 npy_intp len, tmp;
3032 PyObject *zero, *val;
3033 int next_is_nonzero, val_is_zero;
3034 double value;
3035
3036 *next = PyNumber_Subtract(stop, start);
3037 if (!(*next)) {
3038 if (PyTuple_Check(stop)) {
3039 PyErr_Clear();
3040 PyErr_SetString(PyExc_TypeError,
3041 "arange: scalar arguments expected "\
3042 "instead of a tuple.");
3043 }
3044 return -1;
3045 }
3046
3047 zero = PyLong_FromLong(0);
3048 if (!zero) {
3049 Py_DECREF(*next);
3050 *next = NULL;
3051 return -1;
3052 }
3053
3054 next_is_nonzero = PyObject_RichCompareBool(*next, zero, Py_NE);
3055 if (next_is_nonzero == -1) {
3056 Py_DECREF(zero);
3057 Py_DECREF(*next);
3058 *next = NULL;
3059 return -1;
3060 }
3061 val = PyNumber_TrueDivide(*next, step);
3062 Py_DECREF(*next);
3063 *next = NULL;
3064
3065 if (!val) {
3066 Py_DECREF(zero);
3067 return -1;
3068 }
3069
3070 val_is_zero = PyObject_RichCompareBool(val, zero, Py_EQ);
3071 Py_DECREF(zero);
3072 if (val_is_zero == -1) {
3073 Py_DECREF(val);
3074 return -1;
3075 }
3076
3077 if (cmplx && PyComplex_Check(val)) {
3078 value = PyComplex_RealAsDouble(val);
3079 if (error_converting(value)) {
3080 Py_DECREF(val);
3081 return -1;
3082 }
3083 len = _arange_safe_ceil_to_intp(value);
3084 if (error_converting(len)) {
3085 Py_DECREF(val);

Callers 1

PyArray_ArangeObjFunction · 0.85

Calls 1

Tested by

no test coverage detected