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 conversion Irrational => Rational #16527

Merged
merged 1 commit into from
Jun 27, 2016
Merged

Conversation

simonbyrne
Copy link
Contributor

Fixes #16513.

@@ -13,7 +13,22 @@ promote_rule{s,T<:Number}(::Type{Irrational{s}}, ::Type{T}) = promote_type(Float
convert(::Type{AbstractFloat}, x::Irrational) = Float64(x)
convert(::Type{Float16}, x::Irrational) = Float16(Float32(x))
convert{T<:Real}(::Type{Complex{T}}, x::Irrational) = convert(Complex{T}, convert(T,x))
convert{T<:Integer}(::Type{Rational{T}}, x::Irrational) = convert(Rational{T}, Float64(x))

@generated function convert{T<:Integer}(::Type{Rational{T}}, x::Irrational)
Copy link
Sponsor Member

@KristofferC KristofferC May 23, 2016

Choose a reason for hiding this comment

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

Maybe I am mistaken but this doesn't seem to return an expression like a @generated function should. It also doesn't really seem to need to be a @generated function at all. nvm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning a value is okay (it just gets spliced in as an expression).

The aim of the @generated is to get it to perform the calculation at compile time, as the result will always be constant. This is exactly the same as the usage in the function directly below.

Technically, an @pure annotation would be sufficient , once #14324 is sorted out.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I think putting @pure on this function would just work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, it didn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@simonbyrne simonbyrne added the domain:rationals The Rational type and values thereof label May 23, 2016
p = 256
while true
setprecision(BigFloat, p)
bx = BigFloat(x())
Copy link
Contributor

Choose a reason for hiding this comment

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

how does this work? I get

julia> pi()
ERROR: MethodError: objects of type Irrational{:π} are not callable

Copy link
Member

Choose a reason for hiding this comment

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

It seems that x == typeof(pi):

julia> @generated function foo(x) x end
foo (generic function with 1 method)

julia> foo(pi)
Irrational{:π}

julia> foo(pi)()
π = 3.1415926535897...

Is that a bug?

Copy link
Contributor

Choose a reason for hiding this comment

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

right, no, this is a generated function, this is how they're supposed to work which I just keep forgetting

@TotalVerb
Copy link
Contributor

Going to crosspost this from #16513.

Is this operation meaningful? Non-exact operations on Rationals either return Float64 or throw OverflowError. This would be a departure from that.

Also, converting from an Irrational to a Rational seems weird in principle.

@ivarne
Copy link
Sponsor Member

ivarne commented May 26, 2016

@TotalVerb This pr does not depart from anything. This only fixes a roundoff error (on a dubious conversion).

It would be a different issue to just remove the conversion (with a InexactError, or MethodError), but I'd be against that because Irrational isn't a computation type.

@simonbyrne Given the confusions about @generated it would be nice to have a comment above the method explaining why it is written as it is written.

(Maybe something like)

# This is a @generated function because it allows the actual calculation to be performed compile-time 
# Note that it returns a Rational constant that get's spliced into the AST of the caller

@simonbyrne
Copy link
Contributor Author

At the moment, the convert docs say:

If T is a AbstractFloat or Rational type, then it will return the closest value to x representable by T.

@simonbyrne simonbyrne force-pushed the sb/rational-irrational branch 2 times, most recently from da37da9 to e2100a0 Compare June 25, 2016 18:06
Fixes #16513. Changes generated macro to pure macro, so as to avoid breaking in future compiler changes.
@simonbyrne
Copy link
Contributor Author

Using @generated was vetoed, so this has been changed to @pure.

Should be ready to merge.

@StefanKarpinski StefanKarpinski merged commit 861100c into master Jun 27, 2016
@StefanKarpinski StefanKarpinski deleted the sb/rational-irrational branch June 27, 2016 16:47
mfasi pushed a commit to mfasi/julia that referenced this pull request Sep 5, 2016
Fixes JuliaLang#16513. Changes generated macro to pure macro, so as to avoid breaking in future compiler changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:rationals The Rational type and values thereof
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants