Coder Social home page Coder Social logo

Comments (4)

eendebakpt avatar eendebakpt commented on July 17, 2024

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.

serhiy-storchaka avatar serhiy-storchaka commented on July 17, 2024

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.

skirpichev avatar skirpichev commented on July 17, 2024

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.

skirpichev avatar skirpichev commented on July 17, 2024

FYI, PR #119457 is ready for review.

from cpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.