Skip to content

[BUG] ndarray_from_mp_obj() needs inclusive check of negative max values #722

Closed
@sfe-SparkFro

Description

@sfe-SparkFro

Describe the bug

if(ivalue > -128) {
dtype = NDARRAY_INT8;
} else {
dtype = NDARRAY_INT16;
}

ivalue > -128 should be ivalue >= -128

Similar here:

if((ivalue < -32767) || (ivalue > 32767)) {
// the integer value clearly does not fit the ulab integer types, so move on to float
ndarray = ndarray_new_linear_array(1, NDARRAY_FLOAT);
mp_float_t *array = (mp_float_t *)ndarray->array;
array[0] = (mp_float_t)ivalue;
} else {

ivalue < -32767 should be ivalue < -32768

ulab version: 6.7.4-4D-c

To Reproduce

from ulab import numpy as np

foo = np.ones((1), dtype=np.int8) * -128
print("should be int8:", foo)

foo = np.ones((1), dtype=np.int8) * -32768
print("should be int16:", foo)

Expected behavior

Should print:

should be int8: array([-128], dtype=int8)
should be int16: array([-32768.0], dtype=int16)

Instead prints:

should be int8: array([-128], dtype=int16)
should be int16: array([-32768.0], dtype=float32)

It incorrectly promotes the dtype to int16 and float respectively.

Additional context

Testing on my end, making the suggested changes fixes the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions