Skip to content
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

More accurate definition of Complex#sign #12242

Merged
merged 8 commits into from
Aug 29, 2022

Conversation

HertzDevil
Copy link
Contributor

Fixes #11700. Handles zeroes, infinities, and not-a-numbers correctly.

A minimal definition would be just zero? ? self : phase.cis. The NaN checks ensure we don't run into weird edge cases for the rest of the method's logic. The remaining lines are best illustrated with a code snippet:

# 6.12e-17 is `Math.cos(Math::PI / 2)`
# 1.22e-16 is `Math.sin(Math::PI)`
Complex.new(0, 1).phase.cis                  # => (6.123233995736766e-17 + 1.0i)
Complex.new(-1, 0).phase.cis                 # => (-1.0 + 1.2246467991473532e-16i)
Complex.new(1, Float64::INFINITY).phase.cis  # => (6.123233995736766e-17 + 1.0i)
Complex.new(-Float64::INFINITY, 1).phase.cis # => (-1.0 + 1.2246467991473532e-16i)

# `#sign` ensures these results are exact even when not doing `self / abs`
Complex.new(0, 1).sign                  # => (0.0 + 1.0i)
Complex.new(-1, 0).sign                 # => (-1.0 + 0.0i)
Complex.new(1, Float64::INFINITY).sign  # => (0.0 + 1.0i)
Complex.new(-Float64::INFINITY, 1).sign # => (-1.0 + 0.0i)

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric labels Jul 10, 2022
@straight-shoota straight-shoota added this to the 1.6.0 milestone Aug 13, 2022
@HertzDevil HertzDevil marked this pull request as draft August 16, 2022 15:19
@HertzDevil HertzDevil marked this pull request as ready for review August 18, 2022 13:32
@straight-shoota straight-shoota added this to the 1.6.0 milestone Aug 18, 2022
@straight-shoota straight-shoota merged commit de16b6d into crystal-lang:master Aug 29, 2022
@HertzDevil HertzDevil deleted the bug/complex-sign branch August 29, 2022 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:numeric
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Complex.zero returns unexpeceted #sign
2 participants