-
Notifications
You must be signed in to change notification settings - Fork 38
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
Preserve input types for various rules #89
Conversation
Codecov ReportBase: 97.31% // Head: 97.31% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## master #89 +/- ##
=======================================
Coverage 97.31% 97.31%
=======================================
Files 3 3
Lines 186 186
=======================================
Hits 181 181
Misses 5 5
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Generally, |
Ok I reverted the However for the |
src/rules.jl
Outdated
@@ -85,7 +85,7 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x) | |||
@define_diffrule Base.atan(x, y) = :( $y / ($x^2 + $y^2) ), :( -$x / ($x^2 + $y^2) ) | |||
@define_diffrule Base.hypot(x, y) = :( $x / hypot($x, $y) ), :( $y / hypot($x, $y) ) | |||
@define_diffrule Base.log(b, x) = :( log($x) * inv(-log($b)^2 * $b) ), :( inv($x) / log($b) ) | |||
@define_diffrule Base.ldexp(x, y) = :( exp2($y) ), :NaN | |||
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y)) ), :(oftype(float($x), NaN)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we even just use
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y)) ), :(oftype(float($x), NaN)) | |
@define_diffrule Base.ldexp(x, y) = :( oftype($x, exp2($y) ), :NaN |
? At least it seems the definitions in Base already assume that x
is a floating point number:
julia> methods(ldexp)
# 7 methods for generic function "ldexp":
[1] ldexp(x::Float16, q::Integer) in Base.Math at math.jl:826
[2] ldexp(x::T, e::Integer) where T<:Union{Float16, Float32, Float64} in Base.Math at math.jl:783
[3] ldexp(x::BigFloat, n::Int64) in Base.MPFR at mpfr.jl:648
[4] ldexp(x::BigFloat, n::Union{Int16, Int32, Int64, Int8}) in Base.MPFR at mpfr.jl:658
[5] ldexp(x::BigFloat, n::UInt64) in Base.MPFR at mpfr.jl:653
[6] ldexp(x::BigFloat, n::Union{UInt16, UInt32, UInt64, UInt8}) in Base.MPFR at mpfr.jl:659
[7] ldexp(x::BigFloat, n::Integer) in Base.MPFR at mpfr.jl:660
But maybe
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y)) ), :(oftype(float($x), NaN)) | |
@define_diffrule Base.ldexp(x, y) = :( oftype(float($x), exp2($y) ), :NaN |
is safer - although maybe even safer would be something like oftype(ldexp($x, $y), ...
(or something similar without evaluating the primal) in case the arguments are promoted in some way in some other, non-Base definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to agree for the first option. I would be worried about including the primal in the calculation since that is a much more expensive operation than exp2(y) which is just some bit shuffling since y
is a integer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
This pull request ensures that the input type is preserved for various rules.
Previously there were potentially a few places where 64 bit NaN's would always be produced
regardless of the input. To fix this I replaced any instance of
:NaN
withoftype($x, NaN)
.Additionally, this pull-request fixes the issue with the
ldexp
rule from #88 which is similar in nature.