[Python] numpy: how to keep datatype of half precision array scalar input when adding a number

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a half precision input that may be an array scalar x = np.float32(3). I'd like to add one to it: y = x + 1. However y will be float64 instead of float32.

    I can only think of two workarounds:

    • convert the input to 1d array: x = np.float32([3]) so that y = x + 1 is float32
    • convert 1 into lower precision: y = np.float32(3) + np.float16(1) is float32

    However, I have a lot of functions, so the above fixes require me to add if-else statements to each function... Are there any better ways? Thanks!

    Continue reading...

Compartilhe esta Página