You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: clongdouble(str) parses string to float64 and loses precision in string conversion. #26701
When given a string, it looks like np.clongdouble() is parsing the numeric values as float64 and losing precision in the conversion. For example, in the following, I expect np.longdouble(s) and np.clongdouble(s).real to give the same result:
In [73]: s = "0.333333333333333333333333"
In [74]: np.longdouble(s)
Out[74]: np.longdouble('0.33333333333333333334')
In [75]: np.clongdouble(s).real
Out[75]: np.longdouble('0.33333333333333331483')
Instead, clongdouble() appears to be parsing to float64, c.f.
In [78]: np.longdouble(np.float64(s))
Out[78]: np.longdouble('0.33333333333333331483')
This is on a Linux machine where the long double type is 80 bit extended precision.
changed the title [-]BUG: `clongdouble(str)` to parses as `float64` and loses precision in string conversion.[/-][+]BUG: `clongdouble(str)` parses string to `float64` and loses precision in string conversion.[/+]on Jun 15, 2024
In windows system all of the above number types return the same truncated decimal value 0.3333333333333333. I tried both with string value and numeric value. Results are same.
In [4]: s = "0.333333333333333333333333"
In [5]: np.longdouble(s)
Out[5]: 0.3333333333333333
In [6]: p = 0.333333333333333333333333
In [7]: np.longdouble(p)
Out[7]: 0.3333333333333333
In [8]: np.clongdouble(s)
Out[8]: (0.3333333333333333+0j)
In [9]: np.clongdouble(p)
Out[9]: (0.3333333333333333+0j)
In [10]: np.longdouble(np.float64(s))
Out[10]: 0.3333333333333333
Thanks for checking, @Searchingdays, but to see the issue, you need to use a system where long double has higher precision than double. With MSVC, long double is the same as double.
I can reproduce this on Debian 12, with Python version 3.11.2 and numpy version 2.1.2
input:
import numpy as np
s = "0.333333333333333333333333"
a = np.longdouble(s)
b = np.clongdouble(s) #will outout complex form r+cj where r = real part and c = complex part
c = np.clongdouble(s).real #real part only
print(a)
print(a.dtype)
print(b)
print(b.dtype)
print(c)
print(c.dtype)
Activity
[-]BUG: `clongdouble(str)` to parses as `float64` and loses precision in string conversion.[/-][+]BUG: `clongdouble(str)` parses string to `float64` and loses precision in string conversion.[/+]Searchingdays commentedon Jun 16, 2024
python version: 3.11.4
numpy version: 1.25.2
Ipython version: 8.19.0
In windows system all of the above number types return the same truncated decimal value 0.3333333333333333. I tried both with string value and numeric value. Results are same.
In [4]: s = "0.333333333333333333333333"
In [5]: np.longdouble(s)
Out[5]: 0.3333333333333333
In [6]: p = 0.333333333333333333333333
In [7]: np.longdouble(p)
Out[7]: 0.3333333333333333
In [8]: np.clongdouble(s)
Out[8]: (0.3333333333333333+0j)
In [9]: np.clongdouble(p)
Out[9]: (0.3333333333333333+0j)
In [10]: np.longdouble(np.float64(s))
Out[10]: 0.3333333333333333
WarrenWeckesser commentedon Jun 16, 2024
Thanks for checking, @Searchingdays, but to see the issue, you need to use a system where
long doublehas higher precision thandouble. With MSVC,long doubleis the same asdouble.michelledeeney85 commentedon Nov 8, 2024
I can reproduce this on Debian 12, with Python version 3.11.2 and numpy version 2.1.2
input:
output:
The types output by dtype match the documentation (https://numpy.org/doc/stable/user/basics.types.html).
But the result of clongdouble is still wrong, despite the type appearing to be correct.