Skip to content

fix: handle negative imaginary parts in number_to_string - #616

Open
Sanjays2402 wants to merge 1 commit into
qlustered:masterfrom
Sanjays2402:fix/complex-negative-imaginary-number-to-string
Open

fix: handle negative imaginary parts in number_to_string#616
Sanjays2402 wants to merge 1 commit into
qlustered:masterfrom
Sanjays2402:fix/complex-negative-imaginary-number-to-string

Conversation

@Sanjays2402

Copy link
Copy Markdown

Closes #551

number_to_string joined the rendered real and imaginary parts with a hardcoded +, so a negative imaginary part produced a malformed literal like 1.0+-1.0j, which complex() rejects — number_to_string(1-1j, significant_digits=1) raised ValueError, and so did any diff/hash comparison involving such a number. The separator is now omitted when the rendered imaginary part is already negative.

Four cases added to the existing test_number_to_string_complex_digits parametrize list in tests/test_helper.py; they fail on master and pass with the fix.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

number_to_string built the normalized complex string by unconditionally
joining the real and imaginary parts with '+'. When the imaginary part is
negative the rendered part already carries its own sign, producing a
malformed literal such as '1.0+-1.0j', which complex() rejects with
ValueError. Any comparison involving such a number crashed.

The sign separator is now omitted when the rendered imaginary part is
already negative. Regression cases added to the existing
test_number_to_string_complex_digits parametrize list.

Closes qlustered#551
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.

Common ValueError raised when formatting complex numbers

1 participant