Quansight-Labs/numpy.net

byteswap() not working for np.Float32

Closed this issue · 2 comments

Hi

Picked up a possible issue: it doesn't look like np.byteswap() works for np.Float32 or np.Float64. However it works fine for ints.

For int (works correctly):

[Test]
public void ByteSwap_ReturnsCorrectValues_ForInt16()
{
    // Python:
    // A = np.array([1, 256, 8755], dtype=np.int16)
    // B = A.byteswap(False)
    // # B = array([  256,     1, 13090], dtype=int16)

    ndarray sample = np.array(new short[] { 1, 256, 8755 }, np.Int16);
    ndarray swapped = sample.byteswap();

    Assert.That(swapped[0], Is.EqualTo(256));
    Assert.That(swapped[1], Is.EqualTo(1));
    Assert.That(swapped[2], Is.EqualTo(13090));
}

But for floats it looks like the values remain unchanged:

[Test]
public void ByteSwap_ReturnsCorrectValues_ForFloat32()
{
    // Python:
    // A = np.array([1.0, 256.0, 8755.0], dtype=np.float32)
    // B = A.byteswap(False)
    // # B = array([4.6006030e-41, 4.6011635e-41, 1.8737409e-38], dtype=float32)

    ndarray sample = np.array(new float[] { 1.0f, 256.0f, 8755.0f }, np.Float32);
    ndarray swapped = sample.byteswap();

    // At this point swapped contains the original values of A, not the swapped values.
    Assert.That(swapped[0], Is.EqualTo(4.6006030e-41));
    Assert.That(swapped[1], Is.EqualTo(4.6011635e-41));
    Assert.That(swapped[2], Is.EqualTo(1.8737409e-38));
}

I suspect this will affect newbyteorder() as well.

I worked around it without to much problem, but thought I'd log it as well just in case.

Cheers
Greg

0.9.86.5 implements byteswap for floats and doubles.

Thanks, looks good!