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

fix: panic on negative exponent in ipow #758

Merged
merged 2 commits into from
Jan 8, 2025
Merged

fix: panic on negative exponent in ipow #758

merged 2 commits into from
Jan 8, 2025

Conversation

ss2165
Copy link
Member

@ss2165 ss2165 commented Jan 8, 2025

No description provided.

@ss2165 ss2165 requested a review from a team as a code owner January 8, 2025 11:06
@ss2165 ss2165 requested review from mark-koch and croyzor and removed request for mark-koch January 8, 2025 11:06
@codecov-commenter
Copy link

codecov-commenter commented Jan 8, 2025

Codecov Report

Attention: Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 92.94%. Comparing base (4ae3032) to head (58786dd).

Files with missing lines Patch % Lines
guppylang/std/builtins.py 25.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #758      +/-   ##
==========================================
- Coverage   92.97%   92.94%   -0.03%     
==========================================
  Files          71       71              
  Lines        8222     8224       +2     
==========================================
  Hits         7644     7644              
- Misses        578      580       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -362,7 +362,8 @@ def __pos__(self: int) -> int: ...
@no_type_check
def __pow__(self: int, other: int) -> int:
# only positive exponents are supported
# TODO add panic for negative exponents once #756 is resolved
if other < 0:
panic("Negative exponent not supported in __pow__.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not supported for now? Or does guppy disallow it? I think using negative exponents to take roots is the kind of freaky thing physicists would do so I'm inclined to say we should support it. It is very awkward however, the right behaviour is like:

def __pow(self: int, other: int) -> int | float:
    if other < 0:
        return float(self) ** float(other)
    else:
        # do the ipow thing, return an int

but I think the float | int return type is problematic? I tried to do it quickly and I'm not quite sure what goes wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guppy does not support unions and probably never will, we will support algebraic data types at some point but it is unclear whether we can make them behave like unions (especially with regards to type inference).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a message saying "cast to float first" though?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work - make sure to say it's the base that needs cast to a float

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting the exponent should also work due to __rpow__

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting the exponent should also work due to __rpow__

Nice, I didn't realise!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that work as written here, or does it require __pow__ to use ReversingChecker?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work as written. The ReversingChecker is on __rpow__

@ss2165 ss2165 requested a review from croyzor January 8, 2025 11:17
@ss2165 ss2165 enabled auto-merge January 8, 2025 11:18
@ss2165 ss2165 added this pull request to the merge queue Jan 8, 2025
Merged via the queue into main with commit 821771a Jan 8, 2025
3 checks passed
@ss2165 ss2165 deleted the ss/ipow-panic branch January 8, 2025 11:29
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.

4 participants