Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# DeepDiff Change log

- Unreleased
- Fixed `number_to_string` raising `ValueError` for complex numbers with a negative imaginary part

- v9-1-0
- Added multiprocessing support for DeepDiff: parallel distance computation and parallel subtree diffing with aggregated worker stats, deterministic ordering, and automatic fallback to serial when unsafe (e.g. `custom_operators`, `*_obj_callback`, `ignore_order_func`)
- Added wildcard/glob pattern support for `exclude_paths` and `include_paths` thanks to [akshat62](https://github.com/akshat62)
Expand Down
26 changes: 14 additions & 12 deletions deepdiff/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,19 +496,21 @@ def number_to_string(number: Any, significant_digits: int, number_format_notatio
number = int(number)
elif isinstance(number, only_complex_number): # type: ignore
# Case for complex numbers.
real = number_to_string(
number=number.real, # type: ignore
significant_digits=significant_digits,
number_format_notation=number_format_notation
)
imag = number_to_string(
number=number.imag, # type: ignore
significant_digits=significant_digits,
number_format_notation=number_format_notation
)
# A negative imaginary part already carries its own sign. Adding another
# one produces a malformed string such as "1.0+-1.0j" that complex() rejects.
sign = '' if str(imag).startswith('-') else '+'
number = number.__class__(
"{real}+{imag}j".format( # type: ignore
real=number_to_string(
number=number.real, # type: ignore
significant_digits=significant_digits,
number_format_notation=number_format_notation
),
imag=number_to_string(
number=number.imag, # type: ignore
significant_digits=significant_digits,
number_format_notation=number_format_notation
)
) # type: ignore
"{real}{sign}{imag}j".format(real=real, sign=sign, imag=imag) # type: ignore
)
else:
number = round(number=number, ndigits=significant_digits) # type: ignore
Expand Down
4 changes: 4 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ def test_number_to_string_decimal_digits(self, t1, t2, significant_digits, numbe
(-0j, 0.2j, 5, "e", ('0.00000e+0', '0.00000e+0+2.00000e-1j')),
(-0j, 0.2j, 0, "f", True),
(-0j, 0.2j, 0, "e", True),
(1-1j, 1-1j, 1, "f", True),
(1-1j, 1.2-1.2j, 1, "f", ('1.0-1.0j', '1.2-1.2j')),
(1-1j, 1.2-1.2j, 1, "e", ('1.0e+0-1.0e+0j', '1.2e+0-1.2e+0j')),
(-1-1j, -1.2-1.2j, 1, "f", ('-1.0-1.0j', '-1.2-1.2j')),
])
def test_number_to_string_complex_digits(self, t1, t2, significant_digits, number_format_notation, expected_result):
st1 = number_to_string(t1, significant_digits=significant_digits, number_format_notation=number_format_notation)
Expand Down