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

is // required to return Rational? Alternatively, an interface for rationals #55661

Open
nsajko opened this issue Sep 1, 2024 · 2 comments
Open
Labels
design Design of APIs or of the language itself docs This change adds or pertains to documentation rationals The Rational type and values thereof

Comments

@nsajko
Copy link
Contributor

nsajko commented Sep 1, 2024

I want to implement // for my own types. The return type would subtype Real and it'd also overload numerator and denominator, but it wouldn't subtype Rational. Would that be allowed?

The motivation is twofold:

  • (Regular) continued fractions seem to be a good choice for encoding rational numbers. It'd be nice if such a type/package could overload // to return the same type, instead of Rational.
  • Rational doesn't generally support type domain numbers, because it requires the numerator and denominator to have the same type. Thus a new, "type domain rational", type would be nice, and // would ideally return type domain numbers given type domain numbers as input.
@nsajko nsajko added docs This change adds or pertains to documentation rationals The Rational type and values thereof design Design of APIs or of the language itself labels Sep 1, 2024
@nhz2
Copy link
Contributor

nhz2 commented Sep 1, 2024

// can also return Complex, though there are still some overflow issues Ref #53435.

I also find it strange that there is a method of // for //(x::Number, y::Complex) but no method for //(x::Number, y::Real)

I think it might make sense to define a generic //(x::Number, y::Number) = rational(x) / rational(y) where rational would be a function to promote a number to any type that supports exact division.

@nhz2
Copy link
Contributor

nhz2 commented Sep 1, 2024

An important question is what should rational(x::Float64) do? Should it return an exact Rational{BigInt} or throw an error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself docs This change adds or pertains to documentation rationals The Rational type and values thereof
Projects
None yet
Development

No branches or pull requests

2 participants