Skip to content

Split complex nan-containing values into two components for IO and casts #27011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

bradcray
Copy link
Member

@bradcray bradcray commented Mar 28, 2025

Prior to this PR, if a complex had a nan value in either its real or imaginary component, the whole value would be stringified as simply nan rather than nan + nani, say. This PR treats the two components separately w.r.t. file IO and string conversions, similar to how we treat complexes storing inf and/or floating point values today. It also fixes a bug in string to complex conversions in which we were falling afoul of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48760 and addresses it using the same fix as in #24184

The tests added here make sure that nan components in the real and/or imaginary slots are treated correctly for:

  • conversions from complex to string or bytes
  • conversions from string or bytes to complex
  • file input and output (using stdin and stdout)
  • conversions from param- and non-param strings to (non-param) complex (we don't currently support param string->float conversions)
  • conversions from param complex to param string

I also added a test of converting inf-oriented strings to real, complex, and imag that I wrote as background when I was researching this issue and updated a few existing tests whose output behavior improved as a result of this PR.

Resolves #27007

TODO:

I realized I should add some tests of complex values who were just nan
in one component or the other and found that the string-to-complex
casts didn't work in the event that the imaginary portion was nan.
But now I think this was another instance of
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48760 that could be fixed
using the same technique as in
chapel-lang#24184, so I did that and
then added a test to lock in the behavior, which didn't work prior to
this commit.

---
Signed-off-by: Brad Chamberlain <[email protected]>
---
Signed-off-by: Brad Chamberlain <[email protected]>
Prior to this, I was finding that the output would differ when
compiled on my Mac with clang vs. on linux with gcc, where the 'nani'
term would wiggle between '+' (on Mac) and '-' (on Linux).  My
understanding is that nans don't really have signs, to here, I'm
just always using '+' if the imaginary component is nan.

---
Signed-off-by: Brad Chamberlain <[email protected]>
Also back out a previous change that forced the sign bit to be + in
runtime conversions of complexes since we're deciding to print the
sign bit now

---
Signed-off-by: Brad Chamberlain <[email protected]>
@bradcray
Copy link
Member Author

The current plan for this effort is to enable the change using the proposed edition capability for breaking changes rather than a one-off flag, so this PR is on hold for the time being until someone has the opportunity to implement that. That said, there was also a decent amount of interest in having the feature and using this as a motivator to get it done.

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.

Feature request: complex values should read/write nan components separately
1 participant