-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add pi_interval for Float32 and Float16 #337
Conversation
1 similar comment
I haven't tested it for performance much yet, but in #271 I planned to achieve that through const Interval{T}(::Irrational{:π}) where T = atomic(Interval{T}, π)
const Interval(::Irrational{:π}) = atomic(Interval{Float64}, π) With that you are able to get If you think it fits your needs, I can look into having this as a separate PR. |
Do the const actually do anything there? The idea is definitely not to be recomputing this each time. |
I don't remember if the |
@Kolaru My main point for this PR was achieving a fix for #336, so if you propose a PR, which introduces a working set of trigonometric functions for Actually using |
@Kolaru That would be great if it works, but it's not clear to me that it will. (And no time to check right now.) |
julia> @btime Interval{Float16}(π)
2.299 ns (0 allocations: 0 bytes)
[3.14062, 3.14258]
julia> @btime Interval{Float32}(π)
0.001 ns (0 allocations: 0 bytes)
[3.14159, 3.1416]
julia> @btime Interval{Float64}(π)
0.001 ns (0 allocations: 0 bytes)
[3.14159, 3.1416] Somehow it looks like it works fine for julia> @code_warntype Interval{Float16}(π)
Body::Interval{Float16}
1 ─ %1 = invoke IntervalArithmetic.atomic(Interval{Float16}::Type{Interval{Float16}}, IntervalArithmetic.π::Irrational{:π})::Interval{Float16}
└── return %1
julia> @code_warntype Interval{Float32}(π)
Body::Interval{Float32}
1 ─ return [3.14159, 3.1416]
julia> @code_warntype Interval{Float64}(π)
Body::Interval{Float64}
1 ─ return [3.14159, 3.1416] That's very mysterious to me, especially since the definition of both the constructor and of the EDIT: The output is slighty different with Julia 1.3.0 (I was still on 1.1.0), but with the same conclusion, that is the compiler fail to infer the result is a constant for julia> @code_warntype Interval{Float16}(π)
Variables
#self#::Core.Compiler.Const(Interval{Float16}, false)
#unused#::Core.Compiler.Const(π, false)
Body::Interval{Float16}
1 ─ %1 = Core.apply_type(IntervalArithmetic.Interval, $(Expr(:static_parameter, 1)))::Core.Compiler.Const(Interval{Float16}, false)
│ %2 = IntervalArithmetic.atomic(%1, IntervalArithmetic.π)::Interval{Float16}
└── return %2
julia> @code_warntype Interval{Float32}(π)
Variables
#self#::Core.Compiler.Const(Interval{Float32}, false)
#unused#::Core.Compiler.Const(π, false)
Body::Interval{Float32}
1 ─ %1 = Core.apply_type(IntervalArithmetic.Interval, $(Expr(:static_parameter, 1)))::Core.Compiler.Const(Interval{Float32}, false)
│ %2 = IntervalArithmetic.atomic(%1, IntervalArithmetic.π)::Core.Compiler.Const([3.14159, 3.1416], false)
└── return %2
julia> @code_warntype Interval{Float64}(π)
Variables
#self#::Core.Compiler.Const(Interval{Float64}, false)
#unused#::Core.Compiler.Const(π, false)
Body::Interval{Float64}
1 ─ %1 = Core.apply_type(IntervalArithmetic.Interval, $(Expr(:static_parameter, 1)))::Core.Compiler.Const(Interval{Float64}, false)
│ %2 = IntervalArithmetic.atomic(%1, IntervalArithmetic.π)::Core.Compiler.Const([3.14159, 3.1416], false)
└── return %2 |
The problem comes from the fact julia use specific mechanism to convert @generated function Interval{T}(x::Irrational) where T
res = atomic(Interval{T}, x())
return :($res)
end This works as expected, precomputing the value the first time the function is called and subsequently reusing it. I'll thus be able to come with a PR to superseed the current one. |
Can you clarify a bit more why it works with generated functions? |
Generated functions work by allowing you to compute the body of your functions from the types that are passed to it. In our case that allow us to to the following:
So for each type, this define the function to simply return a constant. |
Thanks a lot for the explanation! |
The test suite is currently testing for thing like Interval{Float64}(π) == Interval{Float64}(float(π)) which fails with my proposed definition. Can I go forward and delete this kind if tests or is there a reason to have it that way ? |
Yes please go ahead and change whatever you need to. Thanks! |
You're welcome. I'm happy to have started the discussion ;). |
Partially deals with #336