Skip to content

fix inexact conversion of Rational to Integer exception type #55758

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nsajko
Copy link
Contributor

@nsajko nsajko commented Sep 12, 2024

Fixes #55756

@nsajko nsajko added error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages rationals The Rational type and values thereof labels Sep 12, 2024
Copy link
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

Probably need to make the same fix here?

nameof(T) === ex.func || print(io, T, ", ")

@@ -147,9 +147,9 @@ end
Rational(x::Rational) = x

Bool(x::Rational) = x==0 ? false : x==1 ? true :
throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity
throw(InexactError(:convert, Bool, x)) # to resolve ambiguity
Copy link
Member

Choose a reason for hiding this comment

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

I think this can stay as :Bool?

(::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num)::T :
throw(InexactError(nameof(T), T, x)))
throw(InexactError(:convert, T, x)))
Copy link
Member

Choose a reason for hiding this comment

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

We could put a sentinel value here like Symbol(""), in which case the printer for InexactError knows to show the type as the called object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages rationals The Rational type and values thereof
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MethodError thrown while preparing to throw InexactError in conversion of Rational to Integer or Complex to Real
3 participants