python
data_error
ai_generated
true
RuntimeWarning: invalid value encountered in sqrt
ID: python/numpy-invalid-value-encountered
80%Fix Rate
88%Confidence
0Evidence
2024-11-11First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
np.sqrt applied to negative numbers, producing NaN.
generic中文
对负数应用 np.sqrt,产生 NaN。
Workarounds
-
95% success
np.sqrt(np.clip(arr, 0, None))
-
90% success
np.where(arr >= 0, np.sqrt(arr), np.nan)
Dead Ends
Common approaches that don't work:
-
80% fail
Hides NaN; downstream results wrong
-
70% fail
Changes math silently; loses sign info