Comments (4)
I can confirm the behavior described above. Some similar cases:
In [24]: 1/(cmath.inf + cmath.infj)
Out[24]: (nan+nanj)
In [25]: cmath.inf * 1j
Out[25]: (nan+infj)
But numpy shows the same behaviour as current cpython main
In [47]: import cmath
...: import numpy as np
...: print(np.__version__)
...: x = np.array([np.inf], dtype=np.complex128)
...: y = np.array([cmath.infj])
...: print(1. / (x + y))
1.26.4
[nan+nanj]
C:\Users\eendebakpt\AppData\Local\Temp\ipykernel_10856\2212785139.py:6: RuntimeWarning: invalid value encountered in divide
print(1. / (x + y))
from cpython.
I agree. If the numerator is finite and the denominator is infinite, the result should be zero. If the numerator is also infinite or contains a NaN, the result should contain a NaN (but not always both component should be NaN). Do you mind to create a PR? Pay attention to the sign of zeros, in some cases it can be clearly defined.
from cpython.
Some similar cases
Please note that this issue was restricted to true division and (nan+nanj)
output.
1/(cmath.inf + cmath.infj)
That's similar to my example: CPython's complex arithmetic has no mixed-mode rules (e.g. for float x complex), so it's an equivalent of (1+0j)/(cmath.inf + cmath.infj)
. Should be 0j
.
cmath.inf * 1j
Seems to be valid: inf * 1j == (inf+0j)*(0+1j)=(inf*0-0*1)+(inf*1+0*0)j=(nan+infj)
. You might expect infj
output, per C11 Annex G, but this will imply mentioned above mixed-mode rules for complex arithmetic (and special imaginary data type, e.g. for 1j
).
But we have some cases, when CPython math give (nan+nanj)
for multiplication and C code - something different. If this worth an issue - it should be a separate one.
an example
>>> complex('(inf+1j)')*complex('(nan+1j)')
(nan+nanj)
#include <complex.h>
#include <stdio.h>
#include <math.h>
int
main()
{
complex double z;
z = CMPLX(INFINITY, 1) * CMPLX(NAN, 1);
printf("%f%+fi\n", creal(z), cimag(z));
return 0;
}
$ cc -std=c11 a.c -lm && ./a.out
-nan+infi
But numpy shows the same behaviour as current cpython main
I guess, implementation copied from CPython.
Do you mind to create a PR?
@serhiy-storchaka, yes, see above diff.
from cpython.
FYI, PR #119457 is ready for review.
from cpython.
Related Issues (20)
- pickling & deepcopy of rruleset and rrule with cache HOT 3
- urllib.parse.urlsplit has trouble with basic auth passwords that contain hashtags HOT 4
- `test_datetime` does not respect custom `setUpClass` and `tearDownClass`
- `re.sub()` leaks references
- Enhancement for OrderedDict: Add `reorder_by_values` method HOT 2
- Add a `commands` argument to `pdb.Pdb` so the user can feed commands from source code
- Windows Python help raises NameError because of missing underscore HOT 1
- .ass files give incorrect MIME types HOT 3
- `_utcfromtimestamp` is undefined in `datetimetester`
- ``date.fromtimestamp(None)`` behaves differently between ``_pydatetime`` and ``_datetime``
- Python 3.13 beta 2 fails to build when --enable-experimental-jit is attempted -- FIXED HOT 5
- Fix incorrect `email.header.Header` `maxlinelen` default. HOT 1
- ‘resource_tracker.py’ warns for multiple leaked objects even if only single leak during multiprocessing
- asyncio.Runner explicitly checks for coroutine HOT 8
- Intel-suffixed free-threading macOS universal builds missing HOT 1
- macOS: python3t shortcuts missing(?) HOT 3
- Use After Free in initContext(_lsprof.c) HOT 5
- [3.13] shell version of `python-config` started using bashisms
- Python.h doesn't follow C99 standard. HOT 14
- ``fnctlmodule.fcntl_ioctl_impl`` has outdated format string HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpython.