Skip to content

Fix ZeroDivisionError in moment analysis - #449

Open
AlexKurek wants to merge 3 commits into
lofar-astron:masterfrom
AlexKurek:Fix-ZeroDivisionError-in-moment-analysis
Open

AlexKurek wants to merge 3 commits into
lofar-astron:masterfrom
AlexKurek:Fix-ZeroDivisionError-in-moment-analysis

Conversation

@AlexKurek

Copy link
Copy Markdown
Contributor

The original implementation computed the 4-quadrant position angle manually:

  1. atan(abs(y / x)) calculated a reference angle in $[0, \pi/2]$.
  2. atanproper mapped this angle into $[0, 2\pi)$ using conditional checks based on the signs of $x$ (m2[0] - m2[1]) and $y$ (2.0 * m11).

Replacing this with np.arctan2(y, x) % (2 * pi) is mathematically identical:

  • np.arctan2(y, x) computes the 4-quadrant angle in $[-\pi, \pi]$.
  • The % (2 * pi) modulo converts negative angles to $[0, 2\pi)$, matching the mapping of atanproper.

It prevents crashes when $x = 0$ (m2[0] == m2[1], as np.arctan2 handles zero denominators.

@gmloose gmloose left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to remove atanproper from bdsf/functions.py. I don't think it's being used outside PyBDSF, and this was the only place in this repository where it was used.

@AlexKurek

Copy link
Copy Markdown
Contributor Author

It is also used in gdist_pa.

@gmloose

gmloose commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

It is also used in gdist_pa.

Any reason why you cannot use math.atan2 in that method?

@AlexKurek

Copy link
Copy Markdown
Contributor Author

No, no reason, changed it.

@AlexKurek

Copy link
Copy Markdown
Contributor Author

The test crashed, I got an email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants