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

Base.literal_pow undesired floating-point conversion for base of ±1? #53735

Open
oscardssmith opened this issue Mar 14, 2024 · 4 comments
Open
Labels
maths Mathematical functions speculative Whether the change will be implemented is speculative

Comments

@oscardssmith
Copy link
Member

julia> Base.literal_pow(^, -1, Val(-1))
-1.0

julia> y = -1
-1

julia> (-1)^y
-1

This has been broken since at least 1.6

@oscardssmith oscardssmith added bug Indicates an unexpected problem or unintended behavior maths Mathematical functions labels Mar 14, 2024
@nhz2
Copy link
Contributor

nhz2 commented Mar 15, 2024

I don't think this is a bug because the docs for ^ says.

If y is a negative integer literal, then
Base.literal_pow transforms the operation to inv(x)^-y by default, where -y is
positive.

and inv(-1) is -1.0

I think the reason for this is so that functions like foo(x::Int) = x^-1 are type stable.

@oscardssmith
Copy link
Member Author

the problem here is that one of the fundamental properties of literal pow is that it doesn't change the results for inputs where x^y returns an answer.

@KlausC
Copy link
Contributor

KlausC commented Mar 15, 2024

IMO the problem is, that literal_pow(x, Val{-1}) cannot be type stable and at the same time behave like x^Int(-1), because the latter throws DomainError for x = 2 while the first is supposed to return 0.5.
In order to be type stable, it has to return 1.0 for x = 1 then.

@stevengj stevengj changed the title Base.literal_pow behaves incorrectly for base of -1. Base.literal_pow undesired floating-point conversion for base of -1? Apr 2, 2024
@stevengj stevengj added speculative Whether the change will be implemented is speculative and removed bug Indicates an unexpected problem or unintended behavior labels Apr 2, 2024
@stevengj
Copy link
Member

stevengj commented Apr 2, 2024

I'm removing the "bug" label since this is the intended behavior for negative literal powers, regardless of the base, and it's not clear whether anything can or should be changed here in a type-stable way.

@stevengj stevengj changed the title Base.literal_pow undesired floating-point conversion for base of -1? Base.literal_pow undesired floating-point conversion for base of ±1? Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions speculative Whether the change will be implemented is speculative
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants