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
| @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.
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:660But 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.
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.
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
:NaNwithoftype($x, NaN).Additionally, this pull-request fixes the issue with the
ldexprule from #88 which is similar in nature.