Skip to content

Convert NaN to correct type in sqrt #64

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

Merged
merged 2 commits into from
Jan 14, 2023
Merged

Convert NaN to correct type in sqrt #64

merged 2 commits into from
Jan 14, 2023

Conversation

DanielVandH
Copy link
Contributor

No description provided.

@DanielVandH
Copy link
Contributor Author

DanielVandH commented Jan 10, 2023

Ah, I'm guessing the integer issues are a reason for not converting it initially? Unless perhaps

sqrt(x::T) where {T<:Number}

and

sqrt(x::Integer)

ought to be separated? The point being for floats to at least be able to avoid e.g.

julia> @code_warntype NaNMath.sqrt(-0.3)
MethodInstance for NaNMath.sqrt(::Float64)
  from sqrt(x::Real) in NaNMath at C:\Users\licer\.julia\packages\NaNMath\MNJRI\src\NaNMath.jl:21
Arguments
  #self#::Core.Const(NaNMath.sqrt)
  x::Float64
Body::Float64
1%1 = (x < 0.0)::Bool
└──      goto #3 if not %1
2return NaNMath.NaN
t(::Float32)
  from sqrt(x::Real) in NaNMath at C:\Users\licer\.julia\packages\NaNMath\MNJRI\src\NaNMath.jl:21
Arguments
  #self#::Core.Const(NaNMath.sqrt)
  x::Float32
Body::Union{Float32, Float64}
1%1 = (x < 0.0)::Bool
└──      goto #3 if not %1
2return NaNMath.NaN
3%4 = Base.getproperty(NaNMath.Base, :sqrt)::Core.Const(sqrt)
│   %5 = (%4)(x)::Float32
└──      return %5

This doesn't happen for the other functions like log since the methods for Floats and others are separated into

f(x::Float64) = ...
f(x::Float32) = ...
f(x::Real) = f(float(x))

which could also be done here, though you still get Unions as above.

@mlubin
Copy link
Collaborator

mlubin commented Jan 13, 2023

For which types T is T(NaN) supposed to work? I'd guess T <: AbstractFloat is ok, but we'd need to check to be sure.

@DanielVandH
Copy link
Contributor Author

I believe only AbstractFloats, based on this definition of isnan from Base

isnan(x::AbstractFloat) = (x != x)::Bool
isnan(x::Number) = false

checking only AbstractFloats.

@mlubin
Copy link
Collaborator

mlubin commented Jan 14, 2023

sqrt(x::T) where {T<:AbstractFloat} = x < 0.0 ? T(NaN) : Base.sqrt(x)
sqrt(x::Real) = sqrt(float(x))

seems reasonable then

@codecov
Copy link

codecov bot commented Jan 14, 2023

Codecov Report

Base: 97.02% // Head: 97.05% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (2727977) compared to base (65a5928).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #64      +/-   ##
==========================================
+ Coverage   97.02%   97.05%   +0.02%     
==========================================
  Files           1        1              
  Lines         101      102       +1     
==========================================
+ Hits           98       99       +1     
  Misses          3        3              
Impacted Files Coverage Δ
src/NaNMath.jl 97.05% <100.00%> (+0.02%) ⬆️

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.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mlubin mlubin merged commit 7715b68 into JuliaMath:master Jan 14, 2023
@DanielVandH DanielVandH deleted the patch-1 branch January 14, 2023 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants