8000 BUG: wrong dtype computing with 0-D teensor · Issue #22823 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: wrong dtype computing with 0-D teensor #22823
Closed
@HydrogenSulfate

Description

@HydrogenSulfate

Describe the issue:

I guess result dtype should be same as input dtype, but code below is not.

import numpy as np

x = np.array([0.04399043, -0.26885903])
y = np.array([-0.99222845, -0.1244299])
x = x.astype("float32")
y = y.astype("float32")
z = np.dot(x, y)
print(z.ndim) # 0
print(z.shape) # ()
print(z.dtype) # float32
print((z**2).dtype) # float64

and when z is reshaped to [1], dtype is consistent with input as expected

import numpy as np

x = np.array([0.04399043, -0.26885903])
y = np.array([-0.99222845, -0.1244299])
x = x.astype("float32")
y = y.astype("float32")
z = np.dot(x, y).reshape([1]) # reshape to [1]
print(z.ndim) # 1
print(z.shape) # (1,)
print(z.dtype) # float32
print((z**2).dtype) # float32

Reproduce the code example:

import numpy as np

x = np.array([0.04399043, -0.26885903])
y = np.array([-0.99222845, -0.1244299])
x = x.astype("float32")
y = y.astype("float32")
z = np.dot(x, y)
print(z.ndim) # 0
print(z.shape) # ()
print(z.dtype) # float32
print((z**2).dtype) # float64
import numpy as np

x = np.array([0.04399043, -0.26885903])
y = np.array([-0.99222845, -0.1244299])
x = x.astype("float32")
y = y.astype("float32")
z = np.dot(x, y).reshape([1]) # reshape to [1]
print(z.ndim)
print(z.shape)
print(z.dtype)
print((z**2).dtype)

Error message:

No response

NumPy/Python version information:

1.21.6 could reproduce this BUG.

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0