Fixes [Bug #19004]: Complex.polar
handles complex singular abs
argument
#6568
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes [Bug #19004] [ruby-core:109879]
Link to issue https://bugs.ruby-lang.org/issues/19004
The issue
Complex.polar
accepts Complex values as arguments for the polar form (abs, arg), as long as the value of the complex has no imaginary part (ie it is 'real' in a mathematical sense, and according tonucomp_real_p
, but not necessarily real in the Ruby Numeric sense of#real?
). Eg:In
f_complex_polar
this is handled by extracting the real part of the arguments.However, in the case
Complex.polar
is called with only a single argument (ie the absolute value is given only):The Complex is created directly without applying a check or attempting to extract the real part of abs. Thus it is possible to create a Complex where the real part is itself an instance of a Complex.
Reproduction
compare it to:
Proposed fix
The proposed change removes the short circuit for the single argument case, meaning the real part extraction is performed by
f_complex_polar
as in the 2 argument case. The fact the polar argument is zero is then handled early on here inf_complex_polar
anyway.Also the switch/case is removed as it seems in other situations of a 2 argument method with optional second argument, mostly a conditional is simply used.
The spec example
Added a spec to
spec/ruby/core/complex/polar_spec.rb
which exposes the issue and validates the fix.The spec aims to more generically test the specified behaviour of Complex rather than checking its internal form, hence the test creates a Complex with
Complex.polar
using complex arguments & then validates that the computed parts of the resulting complex number are#real?
As my first contribution please let me know if something needs changes or improvement.
Thanks for Ruby & everything you have all put into it ❤️